Complete the code to generate random noise input for the generator.
noise = torch.randn(batch_size, [1], device=device)The generator takes random noise vectors of size latent_dim as input to create fake images.
Complete the code to calculate the discriminator loss on real images.
output_real = discriminator(real_images)
loss_real = criterion(output_real, [1])Real images are labeled as 1 (true) when training the discriminator.
Fix the error in updating the generator by completing the missing code.
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()The generator tries to fool the discriminator, so fake images are labeled as real (ones) when computing generator loss.
Fill both blanks to compute discriminator loss on fake images and combine total discriminator loss.
output_fake = discriminator(fake_images.detach()) loss_fake = criterion(output_fake, [1]) loss_D = loss_real + [2]
Fake images are labeled as zeros for discriminator loss. Total discriminator loss is sum of real and fake losses.
Fill all three blanks to complete the GAN training step: zero gradients, backward pass, and optimizer step for discriminator.
optimizer_D.[1]() loss_D.[2]() optimizer_D.[3]()
Training step requires clearing gradients, computing gradients with backward, and updating weights with step.