# C1W1 - Training GAN (doubts on the discriminator loss)

Hello,

In the first programming assignment in week1, we need to define the method “get_disc_loss” and compute the discriminator loss. I’m confused about how to calculate the discriminator loss.

The exercise instructions suggest to “Calculate the discriminator’s loss by averaging the real and fake loss and set it to disc_loss”
The real and fake loss are computed using the provided criterion and comparing the predictions to a “ground truth”, wich are zeros (fake) or ones (real). The result is a tensor with a loss for each image of the batch. Since disc_loss is a torch scalar, and the real and fake loss are tensors, I understand that the average of both can be computed in different ways.

``````noise_vect = ...
gen_images = ...
disc_preds_fake = disc(gen_images)
loss_fake = criterion(disc_preds_fake, ground truth for fakes)
disc_preds_real = disc(real)
loss_real = criterion(disc_preds_real, ground truth for reals)
disc_loss = (loss_fake+loss_real)/2
``````

the code as I’m presenting above returns an error because it was expecting a torch scalar and disc_loss is a tensor:

“assert (disc_loss - 0.68).abs() < 0.05
RuntimeError: Boolean value of Tensor with more than one value is ambiguous”

I’ve also tried
disc_loss = (loss_fake+loss_real).mean()
But in this case i get blocked on the test_disc_reasonable test:
assert torch.all(torch.abs(disc_loss.mean() - 0.5) < 1e-5)

Can you help me understand how to compute the discriminator loss? What Could be missing?

Thank you

The code you wrote should work, I would think, although you might be missing a “detach()” in there. But that will not affect the numeric results.

Note that they play some pretty sophisticated tricks in the test code here, taking advantage of the fact that we are passing functions to `disc_loss`. So whether the losses are scalars or vectors is all dependent on what they are using as the `criterion` function, right? Take a more careful look at the test code and you’ll see some examples in which `criterion` is a vector valued function.

Also are you using `torch.ones_like` and `torch.zeros_like` to compute the ground truth values? That is the recommended method.

If the above hints are not enough to get you to a solution, maybe it’s time to look at your actual code. I’ll send you a DM about how to proceed with that.

Hello, Paul!

Thank you for your suggestions. In private discussion we figured out that using 0.5 *(loss_real + loss_fake) instead of (loss_real + loss_fake)/2 or “mean()” fixed the problem.

1 Like

Great! But just for the record, the above solution also works for me.

1 Like