Code Cell UNQ_C1: Unexpected error (IndexError

Ok, that explains why you get all the real samples followed by all the fake ones. :nerd_face: Instead of using the “cat” approach, try setting the output to the real clones and then overwriting just the [filt] subset of the output with the fake clones. Also then you won’t need any reshapes.

I think that is exactly what that particular test is checking for. Notice that you passed all the previous tests. Note that one sets the fakes to be the same as the reals and then takes the difference between the combined and original, which should be zero. Because you have changed the order, that doesn’t work with your implementation.

1 Like

Thank you Paul. Did you mean the following?

    filt = torch.rand(len(real)) > p_real
    target_images = real.clone()
    #print(target_images.shape)
    target_images[filt] = fake.clone()
    #print(target_images.shape)

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-30-d0d5d7d7e38d> in <module>
      1 my_real = torch.ones(7,5) * 0.5
      2 my_fake = torch.ones(7,5) * -0.75
----> 3 my_output = combine_sample(my_real, my_fake, 0.4)
      4 
      5 print(f"my_output = {my_output}")

<ipython-input-29-35f872852bdb> in combine_sample(real, fake, p_real)
     16     target_images = real.clone()
     17     #print(target_images.shape)
---> 18     target_images[filt] = fake.clone()
     19     print(target_images.shape)
     20 

RuntimeError: shape mismatch: value tensor of shape [7, 5] cannot be broadcast to indexing result of shape [6, 5]

Yes, that’s exactly the idea that I was suggesting, but the problem is that you also need to filter the RHS of that assignment statement, right? Otherwise they are not the same size. That’s what that error message is telling you. The first rule of debugging is “believe the error message”. That’s your best hope of a clue as to what is wrong. If you don’t understand what it means, then that’s probably the first thing you need to remedy. :nerd_face:

1 Like

Hi Paul, I figured it out. Thank you for your patience! Wow! Could you please clarify one detail from your previous statement:

  1. “… try setting the output to the real clones and then overwriting just the [filt] subset of the output with the fake clones…”?

Why did you write fake clones? when there was no need to clone the fakes? Am I missing something here?

  1. You also wrote: “…but the problem is that you also need to filter the RHS of that assignment statement, right? …”

So my solution was target_images[filt] = fake[filt]. What exactly is happening here?

Thank you once again for your kind patience.

To understand the point of the “clone” operations on both the real and fakes, you need to think carefully about how object references work in python. You may be right that it doesn’t really matter to clone the fakes in this case, but it clearly matters for the reals, because just saying this:

target_images = reals

doesn’t copy anything: it just makes target_images another reference to the same global object. Since we’re about to overwrite some entries of target_images, that would be a big disaster without the clone(). Here’s a Discourse thread that talks about these issues in a slightly different context, but gives the basic picture. Please read the later posts in the thread as well. Of course to really fully understand this, we need to understand more deeply how pytorch tensors work. So I don’t really know if the examples distinguishing “deep” copies from shallow copies on that other thread apply in this case or not. In the “Optional hints” for this function, they do point out you don’t want to edit things in place, so point to “clone()”. It’s clear that would apply to the reals, but it’s not so clear it actually matters for the fakes. But at worst, it’s just a waste of a bit of cpu and memory in the fakes case.

The point of

target_images[filt] = fake[filt]

Is that you are assigning to only a subset of the entries of target_images, right? That’s the whole point. So anytime you have an assignment statement with indexing on the LHS, what is on the RHS needs to be of the same “shape” (in the general sense). That’s why your original version without the same indexing on the RHS “threw”: there were too many elements on the RHS to match the expression on the LHS.

1 Like

That makes a lot of sense now, but I have personally been stuck on this the the good part of a week. I suspect that wording of the instructions need to be much more explicit on this very specific point. They read:

you’ll first need to write some code to sample a combination of real and generated images.

without say that they MUST be intermixed and not just concatenated. Then it continues with the example:

For example, if your real images are a tensor of…

well, it is not that it just so happens that the outcome of the example is intermixed. It is a requirement, but it is not call out clearly anywhere in the instructions. The wording of the assert statement are not particularly useful in this case to figure out why one is failing.

This is a bit of a rant, but it is the first time in 10+ Coursera coding courses over the last year that I trip in poor instructions like these ones. The good news is that this happens infrequently on Coursera base on my experience.

Sorry, Giorgio, I feel your frustration. As you can see from the earlier part of this thread, you are not the only person to get tripped up by this aspect of the exercise. But to be fair, they did actually give you a very concrete example in the instructions for this section that shows exactly the point that the replacements with “fakes” happens “in place”. Here’s the relevant paragraph:

To start, you'll first need to write some code to sample a combination of real and generated images. Given a probability, p_real, you'll need to generate a combined tensor where roughlyp_realof the returned images are sampled from the real images. Note that you should not interpolate the images here: you should choose each image from the real or fake set with a given probability. For example, if your real images are a tensor of[[1, 2, 3, 4, 5]]and your fake images are a tensor of[[-1, -2, -3, -4, -5]], andp_real = 0.2, two potential random return values are[[1, -2, 3, -4, -5]]or[[-1, 2, -3, -4, -5]] .

1 Like