Code Cell UNQ_C1: Unexpected error (IndexError

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