Assertion Error for Get_Gen_Loss

Hello,
I seem to be stuck with an error I am not able to trace. The unit tests to my UNQ_C7 are failing for the get_gen_loss function.

Here is the code snippet of my steps:

    noise_input = get_noise(num_images, z_dim, device)

    fake_images = gen(noise_input)
    
    fake_pred = disc(fake_images.detach())
    
    fake_gnd = torch.zeros_like(fake_pred)
    
    gen_loss = criterion(fake_gnd,fake_pred)

What am I missing? Any pointers are appreciated.


AssertionError Traceback (most recent call last)
in
44
45 test_gen_reasonable(10)
—> 46 test_gen_loss(10)
47 print(“Success!”)

in test_gen_loss(num_images)
35
36 # Check that the loss is reasonable
—> 37 assert (gen_loss - 0.7).abs() < 0.1
38 gen_loss.backward()
39 old_weight = gen.gen[0][0].weight.clone()

AssertionError:

1 Like

Hi @dlunga!

Let’s compare the assignment’s instructions to what you’ve done:

#     These are the steps you will need to complete:
#       1) Create noise vectors and generate a batch of fake images. 
#           Remember to pass the device argument to the get_noise function.

Sounds good, you have your noise vectors (noise_input) and your batch of fake images (fake_images).

#       2) Get the discriminator's prediction of the fake image.

That’s your fake_pred alright, though I don’t think you need to detach fake_images here.

#       3) Calculate the generator's loss. Remember the generator wants
#          the discriminator to think that its fake images are real
#     *Important*: You should NOT write your own loss function here - use criterion(pred, true)!

Now for the final generator’s loss, we want the discriminator to think the fake images are real, i.e. we want the ground truth to be all ones. Remember this is the generator’s loss: the generator wants to fool the discriminator, which would mean the discriminator saying an image is real when in reality it is fake.

Also, mind the order of criterion's arguments: it should be the prediction first, then this ground truth, i.e. criterion(pred, true).

Hope this helps you pass the unit tests. Cheers, and welcome to the community!

3 Likes

Very helpful, thank you so much! I completely missed the hint “Remember the generator wants
the discriminator to think that its fake images are real”.

All works!

1 Like