0
0
PyTorchml~10 mins

GAN training loop in PyTorch - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to generate random noise input for the generator.

PyTorch
noise = torch.randn(batch_size, [1], device=device)
Drag options to blanks, or click blank then click option'
Abatch_size
Blatent_dim
Cimage_size
Dnum_classes
Attempts:
3 left
💡 Hint
Common Mistakes
Using batch_size instead of latent_dim for noise vector size.
Using image_size which is for output images, not input noise.
2fill in blank
medium

Complete the code to calculate the discriminator loss on real images.

PyTorch
output_real = discriminator(real_images)
loss_real = criterion(output_real, [1])
Drag options to blanks, or click blank then click option'
Atorch.ones_like(output_real)
Bnoise
Creal_images
Dtorch.zeros_like(output_real)
Attempts:
3 left
💡 Hint
Common Mistakes
Using zeros for real images labels.
Passing images instead of labels to the loss function.
3fill in blank
hard

Fix the error in updating the generator by completing the missing code.

PyTorch
optimizer_G.zero_grad()
fake_images = generator(noise)
output_fake = discriminator(fake_images)
loss_G = criterion(output_fake, [1])
loss_G.backward()
optimizer_G.step()
Drag options to blanks, or click blank then click option'
Atorch.zeros_like(output_fake)
Breal_images
Cfake_images
Dtorch.ones_like(output_fake)
Attempts:
3 left
💡 Hint
Common Mistakes
Using zeros for fake images labels in generator loss.
Passing images instead of labels to the loss function.
4fill in blank
hard

Fill both blanks to compute discriminator loss on fake images and combine total discriminator loss.

PyTorch
output_fake = discriminator(fake_images.detach())
loss_fake = criterion(output_fake, [1])
loss_D = loss_real + [2]
Drag options to blanks, or click blank then click option'
Atorch.zeros_like(output_fake)
Bloss_fake
Ctorch.ones_like(output_fake)
Dloss_G
Attempts:
3 left
💡 Hint
Common Mistakes
Using ones for fake images labels in discriminator loss.
Adding generator loss instead of fake loss to loss_real.
5fill in blank
hard

Fill all three blanks to complete the GAN training step: zero gradients, backward pass, and optimizer step for discriminator.

PyTorch
optimizer_D.[1]()
loss_D.[2]()
optimizer_D.[3]()
Drag options to blanks, or click blank then click option'
Azero_grad
Bbackward
Cstep
Ddetach
Attempts:
3 left
💡 Hint
Common Mistakes
Calling step before backward.
Forgetting to zero gradients before backward.