Assignment passed but not working on training conditional GAN

I got passed on the assignment, but the GAN fails to train a meaningful generator after 5-10 attempts of training.

Below is my code:

Not sure if it would be the choice of detach() which is wrong or whatever, the generator keeps producing gray background.

Below is my result:

Hey @kewell_chong,
Welcome to the community. I am not sure if the code for the generator and discriminator has been changed, but in my version of the assignment, we implemented the forward method in both the generator and the discriminator. And in the training part, instead of using fake = gen(noise_and_labels), we used fake = gen.forward(noise_and_labels).

Similarly, we used disc_fake_pred = disc.forward(fake_image_and_labels) and disc_real_pred = disc.forward(real_image_and_labels). Try this once, perhaps this might be the issue :innocent:

@kewell_chong, your code in the screenshot looks good. It seems like the generator is not learning. Could there be something related to the generator in another part of the code that was inadvertently changed? For example, if the generator used the same fake_image_and_labels as is used with the discriminator, that could be a problem, since we don’t want the detached fake for the generator.

If you can’t see anything, you can DM your ipynb to me and I’d be happy to take a look.

Hmm… I was originally trying the following:

fake = gen(noise_and_labels).detach()

and

fake_image_and_labels = combine_vectors(fake, image_one_hot_labels)

and I was getting the same empty squares you’re getting with generator and discriminator losses diverging but once I changed them back to what you have (and restarted the notebook), my training started progressing normally. Not sure what went on here…

Hey @Xiaojian_Deng,
Welcome to the community. The issue lies with the position of calling the detach method, as you can easily see from your code.

I am assuming that you are well familiar with the concept of detach method, but if you are not, then let me give you a 1-line summary. We call this method when we don’t want to update the associated weights with an object. In this case, the object is the fake images, and the weights are of the generator.

Now if you carefully look at the code, you will find that we are using these fake/generated images, for updating the discriminator as well as the generator. Now, though we want to call detach on the fake images when we are updating the discriminator (since we don’t want to update the generator in this case), we don’t want the same thing to happen when we are updating the generator. And hence, when you changed the position of detach method, the generator didn’t update at all, and hence, led to empty squares.

Hope this helps :innocent:

1 Like

Hello, I encoutered the same problem with my assignment. Did you get the issue fixed? Could you give me some hints on how to?
Thank your for your time!

I also saw that on my version… IDK if there was a bug in my code or is it the lab that is having a issue :c

Hi @Zildjian240, did you get your code to work? As mentioned above, one important thing to check if your generator is not learning is to make sure you’re not using a detached fake for the generator case.