0
0
PyTorchml~10 mins

Image generation basics 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 create a random noise tensor for image generation.

PyTorch
noise = torch.randn([1], 100, 1, 1)
Drag options to blanks, or click blank then click option'
A128
B32
C64
D16
Attempts:
3 left
💡 Hint
Common Mistakes
Using a batch size too large for memory causing out-of-memory errors.
Confusing the noise vector size with batch size.
2fill in blank
medium

Complete the code to define the output shape of the generated image tensor.

PyTorch
output = generator(noise)
print(output.shape)  # Should be ([1], 3, 64, 64)
Drag options to blanks, or click blank then click option'
A32
B16
C64
D128
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing image channels or spatial dimensions with batch size.
Using a batch size different from the noise input.
3fill in blank
hard

Fix the error in the generator model's forward pass to correctly reshape the noise input.

PyTorch
def forward(self, input):
    x = input.view([1], 100, 1, 1)
    output = self.main(x)
    return output
Drag options to blanks, or click blank then click option'
Ainput.shape[0]
Binput.shape[1]
Cinput.size(1)
Dinput.size(0)
Attempts:
3 left
💡 Hint
Common Mistakes
Using input.size(1) which is the noise vector size, not batch size.
Using input.shape which is valid but inconsistent with PyTorch style.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps image labels to their counts.

PyTorch
label_counts = {label: labels.count([1]) for label in set([2])}
Drag options to blanks, or click blank then click option'
Alabel
Blabels
Clabel_
Dlabel_counts
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable not defined in the comprehension.
Confusing the key and value variables.
5fill in blank
hard

Fill all three blanks to complete the training loop snippet for the generator model.

PyTorch
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()
Drag options to blanks, or click blank then click option'
Abatch_size
Bnoise
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong index for images in data tuple.
Passing wrong variable to generator.