I have implemented the combining as I did in the assignment C1W3_WGAN_GP:

I created a tensor of booleans named epsilon by assigning the result of comparing a tensor of torch.rand (of real image size) to be less than the given probability of real images.

I then assigned to target_images the sum of real images times epsilon and fake images times logical not epsilon

I think that the assert “# Make sure that no mixing happened” might mean that tensor target_images is the correct length but I don’t know what “mixing” means.

I used torch.multinomial to randomly select a proportion of uniform index indicators (1s). This produced an index at random positions of the correct proportions. I then used the index to select a subset from the sample. I did this for the real and fake indices. I concatenated the selected samples. I also concatenated the selected indices. I sorted the combined indices in ascending order and used these to select the concatenated samples.

I am however, not getting the order right and am incurring the following assert error:

It may be that I’m simply not understanding what you mean, but that all sounds a bit worrying. The point is to do things “in place” and also “row-wise”, not “element-wise”. You clone the “reals” and then replace some of those samples (in-place) with fakes.

I also did not see any reference to torch.multinomial in the instructions. They do discuss using torch.rand or torch.bernoulli. I used torch.rand FWIW …

I had a look at the other thread and it makes much more sense now. It did say there were multiple ways to sort this, but what I did was wrong. I randomly sampled from each of real and fake and concatenated them in a way that the indexing wouldn’t have worked either. The solution described on the thread that you linked works. It swaps elements nicely preserving the order. Thanks again for your help.