Error Message Concerning Building the Training Code

I’ve been on it for 7 hours today, but I somehow can’t figure out A) What I’m doing wrong, and (after looking at it for that much time) B) if I’m actually calling things like the Generator the right way.

{moderator edit - solution code removed}

This code results into the following error message:

I also am a bit confused why the average of the discriminators is exactly 0.500. When I run the code piece by piece and print out different values the gen_output gives only zero’s and in a second run only one’s. I am not sure if this is correct, but that does indeed result in an average of 0.500, which itself isn’t smaller then 1e-5.

Am I calling the generator in the wrong way? Should I call it with Generator()?

Because if I call it using Generator, as seen in the image bellow as well (next to code block 3 ->line 34) I get a way more elaborate error message, yet one I can’t seem to wrap my head around.

I also tried to run the code blocks after with different varying errors, but I think it is best to start at the possible root of all problems. Is there anyone who sees the problem in my code? Because I have the feeling I’m just inexperienced and somehow am looking over a really easy-to-solve mistake

I understand btw if code needs to be deleted by the mods if it shows a partially correct answer. Please feel free to do so or tell me to do so :wink:

There are a number of problems with your code:

For starters, the detach() method is not an “in place” operation: it returns the detached tensor and leaves the base tensor unmodified. Google “pytorch detach” to learn more. So saying:


without assigning the return value to a variable is a NOP. But that error won’t cause any different values, it just causes gradients still to be generated.

Then you feed the real images directly to the criterion function. The point is to feed it the output of the discriminator on the real images, right?

The way you call criterion is also wrong for the generator output. You pass it the generator output, but you want to be passing it the discriminator output for the fake images, right? Also you are using ones as the target value, so what your code says is that you are trying to encourage the discriminator to label the fake images as real. That doesn’t seem like what you want, does it?

Sorry, there were too many questions there and I missed the one about how to invoke the generator. It is a mistake to directly call Generator in the way you show: take a look at the definition of that function and how it is used in the template code they give you. That is a function which has a function as its return value. They invoke the function to create the actual generator function and then pass that function to you as one of the arguments to the get_disc_loss function.

@paulinpaloalto thank you so much for the quick reaction. Indeed some really easy fixes that seem so obvious in hindsight! :grin:

I also found out I didn’t write ‘device=device’ when defining the noise in the return statement. Your comments + that insight fixed everything!