Complete the code to create a random noise tensor for image generation.
noise = torch.randn([1], 100, 1, 1)
The batch size for generating images is commonly set to 32 for training stability and speed.
Complete the code to define the output shape of the generated image tensor.
output = generator(noise) print(output.shape) # Should be ([1], 3, 64, 64)
The output batch size matches the input noise batch size, which is 32 here.
Fix the error in the generator model's forward pass to correctly reshape the noise input.
def forward(self, input): x = input.view([1], 100, 1, 1) output = self.main(x) return output
input.size(0) correctly gets the batch size dimension to reshape the noise tensor.
Fill both blanks to create a dictionary comprehension that maps image labels to their counts.
label_counts = {label: labels.count([1]) for label in set([2])}We count how many times each label appears in the labels list by iterating over unique labels.
Fill all three blanks to complete the training loop snippet for the generator model.
for epoch in range(num_epochs): for i, data in enumerate(dataloader): noise = torch.randn([1], 100, 1, 1) fake_images = generator([2]) loss = criterion(fake_images, data[[3]]) optimizer.zero_grad() loss.backward() optimizer.step()
The noise tensor uses the batch size, the generator input is noise, and real images are at index 0 in data.