Bird
Raised Fist0
PyTorchml~10 mins

Why generative models create data in PyTorch - Test Your Understanding

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

Complete 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'
Anoise
Binput
Cdata
Dx
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.
2fill in blank
medium

Complete 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'
Agenerate
Bpredict
C__call__
Dforward
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.
3fill in blank
hard

Fix 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'
Aforward
B__call__
Cgenerate
Drun
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.
4fill in blank
hard

Fill 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'
A[i]
B(i)
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets for indexing.
Using the wrong comparison operator in the condition.
5fill in blank
hard

Fill 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'
A[i]
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets for indexing.
Using '<' instead of '>' in the condition.

Practice

(1/5)
1. Why do generative models create new data in machine learning?
easy
A. To speed up training by skipping data
B. To delete existing data from the dataset
C. To only classify data into categories
D. To mimic real examples and generate similar data

Solution

  1. Step 1: Understand the purpose of generative models

    Generative models learn patterns from real data to create new, similar data.
  2. Step 2: Identify the correct purpose from options

    Only To mimic real examples and generate similar data describes creating new data that mimics real examples, which is the main goal.
  3. Final Answer:

    To mimic real examples and generate similar data -> Option D
  4. Quick Check:

    Generative models create data = mimic real examples [OK]
Hint: Generative models create data similar to real examples [OK]
Common Mistakes:
  • Confusing generative models with classifiers
  • Thinking generative models delete data
  • Assuming generative models speed up training by skipping data
2. Which PyTorch code snippet correctly initializes a simple generative model layer?
easy
A. torch.nn.Linear(100, 256)
B. torch.nn.Conv2d(3, 64, kernel_size=3)
C. torch.optim.SGD(model.parameters(), lr=0.01)
D. torch.nn.CrossEntropyLoss()

Solution

  1. Step 1: Identify generative model layer types

    Generative models often use linear layers to transform random input vectors into data representations.
  2. Step 2: Match code to layer initialization

    torch.nn.Linear(100, 256) correctly initializes a linear layer with input size 100 and output size 256, suitable for generative models.
  3. Final Answer:

    torch.nn.Linear(100, 256) -> Option A
  4. Quick Check:

    Linear layer init = torch.nn.Linear(100, 256) [OK]
Hint: Linear layers often start generative models [OK]
Common Mistakes:
  • Choosing optimizer or loss function as layer
  • Confusing convolutional layers with simple linear layers
  • Using loss functions as model layers
3. Given this PyTorch code, what is the shape of the generated data tensor?
import torch
noise = torch.randn(16, 100)
generator = torch.nn.Linear(100, 784)
output = generator(noise)
medium
A. (16, 784)
B. (16, 100)
C. (100, 784)
D. (784, 16)

Solution

  1. Step 1: Understand input and output shapes

    The noise tensor has shape (16, 100), meaning 16 samples each with 100 features. The linear layer maps 100 features to 784 features.
  2. Step 2: Calculate output shape after linear layer

    Applying the linear layer to noise results in shape (16, 784), preserving batch size and changing feature size.
  3. Final Answer:

    (16, 784) -> Option A
  4. Quick Check:

    Output shape = (batch_size, output_features) = (16, 784) [OK]
Hint: Output shape matches batch size and output features [OK]
Common Mistakes:
  • Mixing batch size and feature dimensions
  • Confusing input and output shapes
  • Transposing shapes incorrectly
4. Find the error in this PyTorch code snippet for generating data:
import torch
noise = torch.randn(32, 50)
generator = torch.nn.Linear(100, 784)
output = generator(noise)
medium
A. Output tensor shape is correct
B. Generator layer should be torch.nn.Conv2d
C. Noise tensor has wrong shape for the generator input
D. Missing optimizer initialization

Solution

  1. Step 1: Check input shape compatibility

    The generator expects input with 100 features, but noise has 50 features per sample, causing a shape mismatch error.
  2. Step 2: Identify the error cause

    Because noise shape (32, 50) does not match expected input (batch_size, 100), the code will fail at runtime.
  3. Final Answer:

    Noise tensor has wrong shape for the generator input -> Option C
  4. Quick Check:

    Input shape mismatch = noise shape wrong [OK]
Hint: Input features must match layer input size [OK]
Common Mistakes:
  • Ignoring input-output shape mismatch
  • Thinking convolutional layers are always needed
  • Confusing optimizer with model layers
5. Why do generative models use random noise as input when creating new data?
hard
A. To speed up training by skipping data
B. To introduce variety and generate diverse outputs
C. To reduce the size of the model
D. To make the model deterministic and fixed

Solution

  1. Step 1: Understand the role of random noise in generative models

    Random noise acts as a seed to produce different outputs, allowing the model to generate varied data samples.
  2. Step 2: Match the purpose with the correct option

    To introduce variety and generate diverse outputs correctly states that noise introduces variety and diversity in generated data.
  3. Final Answer:

    To introduce variety and generate diverse outputs -> Option B
  4. Quick Check:

    Noise input = variety in outputs [OK]
Hint: Noise input creates diverse generated data [OK]
Common Mistakes:
  • Thinking noise speeds up training
  • Assuming noise reduces model size
  • Believing noise makes output fixed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard