1. Learn
  2. /
  3. Courses
  4. /
  5. Deep Learning for Images with PyTorch

Connected

Exercise

Discriminator loss

It's time to define the loss for the discriminator. Recall that the discriminator's job is to classify images either real or fake. Therefore, the generator incurs a loss if it classifies generator's outputs as real (label 1) or the real images as fake (label 0).

Define the disc_loss() function that calculates the discriminator loss. It takes five arguments:

  • gen, the generator model
  • disc, the discriminator model
  • real, a sample of real images from the training data
  • num_images, the number of images in batch
  • z_dim, the size of the input random noise

Instructions

100 XP
  • Use the discriminator to classify fake images and assign the predictions to disc_pred_fake.
  • Compute the fake loss component by calling criterion on discriminator's predictions for fake images and the a tensor of zeros of the same shape.
  • Use the discriminator to classify real images and assign the predictions to disc_pred_real.
  • Compute the real loss component by calling criterion on discriminator's predictions for real images and the a tensor of ones of the same shape.