Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a simple generative model class in PyTorch.
PyTorch
import torch.nn as nn class SimpleGenerator(nn.Module): def __init__(self): super(SimpleGenerator, self).__init__() self.model = nn.Sequential( nn.Linear(100, 256), nn.ReLU(), nn.Linear(256, 784), nn.Tanh() ) def forward(self, [1]): return self.model(x)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not defined in the method parameters.
Forgetting to include the input parameter in the forward method.
✗ Incorrect
The forward method takes an input tensor, commonly named 'x', which is passed through the model to generate output data.
2fill in blank
mediumComplete the code to generate fake data from random noise using the generator model.
PyTorch
import torch noise = torch.randn(16, 100) fake_data = generator.[1](noise)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call a non-existent 'generate' or 'predict' method.
Using 'forward' method directly instead of calling the model.
✗ Incorrect
In PyTorch, calling the model instance directly (using __call__) runs the forward method, generating fake data from noise.
3fill in blank
hardFix the error in the training loop where the generator tries to create data.
PyTorch
for epoch in range(5): noise = torch.randn(32, 100) fake_data = generator.[1](noise) # further training steps...
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a method that does not exist on the model.
Using 'forward' method directly instead of calling the model.
✗ Incorrect
Calling the generator instance directly (using __call__) is the correct way to generate data in PyTorch, not using a non-existent 'generate' or 'run' method.
4fill in blank
hardFill both blanks to create a dictionary of generated images with their indices.
PyTorch
generated_images = {i: fake_data[1] for i in range(10) if i [2] 5} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets for indexing.
Using the wrong comparison operator in the condition.
✗ Incorrect
To index tensors, square brackets are used. The condition filters indices less than 5.
5fill in blank
hardFill all three blanks to create a dictionary of generated images with their pixel sums greater than 1000.
PyTorch
filtered_images = {i: fake_data[1] for i in range(20) if fake_data[2].sum() [3] 1000} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets for indexing.
Using '<' instead of '>' in the condition.
✗ Incorrect
Use square brackets to index the tensor at i, then sum the pixels and check if greater than 1000.