C3W3_Assignment - get_disc_loss (AssertionError)

Hello there,

I’m trying to code the ‘graded function’ get_disc_loss(real_X, fake_X, disc_X, adv_criterion), but I’m getting an assertion error and I can’t solve it, so can anyone help me?

I undertood that:
disc_loss = disc_loss_real_images + disc_loss_fake_images

Where:
– disc_loss_real_images = adv_criterion(disc_X(real_X), torch.ones_like(disc_X(real_X)))
– disc_loss_fake_images = adv_criterion(disc_X(fake_X.detach()), torch.zeros_like(disc_X(fake_X.detach())))

Doing that I obtain a loss=1318109.0.

I know that this fails since this loss must be <1e-6

Thanks in advance for the help.

Hi @MarcosMM,
You’ve got the basic concepts right - just missing one small thing the assignment is expecting. The unit tests are there to help with this. What I like to do in this situation is to look at the unit test to see what it is expecting and then compare that with what my code returns.

In this case, the test that is failing is:

assert torch.abs((get_disc_loss(test_real_X, test_fake_X, test_disc_X, test_adv_criterion)) - 659054.5000) < 1e-6

Breaking this down:

  • Checking if a value is < 1e-6 is checking that it is very small. Generally, this is used when you expect the value to be close to 0, maybe even exactly 0, but you’re giving a little wiggle room for rounding errors, etc.

  • If we’re expecting <some_value> - 659054.5 to be close to zero, we’re expecting <some_value> to be close to 659054.5

How does that value compare to the disc_loss you calculated, 1318109.0? Notice anything interesting about how the size of your value compares to the size of the expected value? :wink:

Another thing you can look at is to notice the text above this cell of the assignment that says this discriminator loss should be implemented like previous assignments, and then look back at previous assignments, like the week 2 Pix2Pix assignment, to see how they calculate discriminator loss to see how it differs from your current implementation on this assignment.

1 Like

Hi @Wendy ,

Thank you very much, as you suggested I have checked the Pix2Pix assignment and I have seen the ‘/2’…

The thing is that I don’t understand why to calculate the average of those two loss values. I don’t know if you could explain it to me. It would be a great help (I don’t like to do things without understanding them :sweat:).

1 Like

Hi @MarcosMM,
There’s not a big reason for it. The most important thing is that we want to minimize loss, and for that, it doesn’t really matter if you ‘/2’ or multiply by 2 or by any other constant.

But the ‘/2’ does have the advantage that it makes the discriminator loss the same scale as the generator loss (generator loss is based on 1 item vs discriminator based on sum of 2 items). Not hugely important, but it does make it easier to compare the two losses when you’re looking at how they’re doing.

I’m having a very similar problem, but the numbers are just not lining up for me as easily as this example. Here is my code, similar to the above:

< deleted >

But, my error is:

tensor(8633.)
tensor(682007.)
tensor(8051.)
tensor(682080.)
tensor(682043.5000)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-87-fcc0952fe01f> in <module>
      4 test_fake_X = torch.tensor(89.)
      5 test_adv_criterion = lambda x, y: x * 79 + y * 73
----> 6 assert torch.abs((get_disc_loss(test_real_X, test_fake_X, test_disc_X, test_adv_criterion)) - 659054.5000) < 1e-6

That is, it is expecting a loss near 659054 and my losses are near 682000 which means it’s off by a reasonable amount. Not sure what else could be going wrong here.

Oh wait. I caught the typo. Finally.

Hi @Nicholas_Chia,
Nice job finding the problem :muscle:

I deleted your code from the above cell since it’s against community guidelines to share your assignment code.

Thanks much. Didn’t mean to break guidelines.