Bird
Raised Fist0
PyTorchml~10 mins

Why nn.Module organizes model code 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 PyTorch model class that inherits from nn.Module.

PyTorch
import torch.nn as nn

class SimpleModel(nn.Module):
    def __init__(self):
        super(SimpleModel, self).__init__()
        self.linear = nn.Linear(10, 1)

    def [1](self, x):
        return self.linear(x)
Drag options to blanks, or click blank then click option'
Aforward
Bapply_activation
Cbackward
Dtrain
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'backward' or 'train' instead of 'forward' for the method name.
2fill in blank
medium

Complete the code to initialize the parent class in the model constructor.

PyTorch
class MyModel(nn.Module):
    def __init__(self):
        [1]
        self.layer = nn.Linear(5, 2)
Drag options to blanks, or click blank then click option'
Asuper().__init__()
Bself.__init__()
Cnn.Module.__init__(self)
Dsuper(MyModel, self).__init__()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling self.__init__() causes infinite recursion.
Using super().__init__() without arguments is valid in Python 3 but here we want the explicit form.
3fill in blank
hard

Fix the error in the forward method to correctly use the linear layer.

PyTorch
class FixModel(nn.Module):
    def __init__(self):
        super(FixModel, self).__init__()
        self.linear = nn.Linear(3, 1)

    def forward(self, x):
        return self.linear[1](x)
Drag options to blanks, or click blank then click option'
A.call
B.forward
C()
D.apply
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call '.forward(x)' directly instead of using parentheses.
4fill in blank
hard

Fill both blanks to create a model with two layers and use ReLU activation between them.

PyTorch
class TwoLayerModel(nn.Module):
    def __init__(self):
        super(TwoLayerModel, self).__init__()
        self.fc1 = nn.Linear(4, 3)
        self.fc2 = nn.Linear(3, 1)
        self.relu = nn.ReLU()

    def forward(self, x):
        x = self.fc1[1](x)
        x = self.relu[2](x)
        return self.fc2(x)
Drag options to blanks, or click blank then click option'
A()
B.forward
C.call
D.apply
Attempts:
3 left
💡 Hint
Common Mistakes
Using '.forward' or '.call' instead of parentheses to call layers.
5fill in blank
hard

Fill all three blanks to define a model that stores layers as attributes, calls them in forward, and returns the output.

PyTorch
class CustomModel(nn.Module):
    def __init__(self):
        super(CustomModel, self).__init__()
        self.layer1 = nn.Linear(6, 4)
        self.activation = nn.Sigmoid()
        self.layer2 = nn.Linear(4, 2)

    def forward(self, input):
        out = self.layer1[1](input)
        out = self.activation[2](out)
        return self.layer2[3](out)
Drag options to blanks, or click blank then click option'
A()
B.forward
C.call
D.apply
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call layers with '.forward' or '.call' instead of parentheses.

Practice

(1/5)
1. Why do we use nn.Module to organize model code in PyTorch?
easy
A. It replaces the need for Python functions.
B. It automatically writes the training loop for you.
C. It only helps with saving models, not training.
D. It groups layers and logic to simplify training and reuse.

Solution

  1. Step 1: Understand the role of nn.Module

    nn.Module helps organize layers and the forward pass logic in one place.
  2. Step 2: Recognize benefits for training and reuse

    This organization makes training, saving, and reusing models easier and cleaner.
  3. Final Answer:

    It groups layers and logic to simplify training and reuse. -> Option D
  4. Quick Check:

    nn.Module groups layers and logic = C [OK]
Hint: Remember: nn.Module bundles layers and logic together [OK]
Common Mistakes:
  • Thinking nn.Module writes training loops automatically
  • Believing nn.Module replaces all Python functions
  • Assuming nn.Module only helps with saving models
2. Which part of a PyTorch nn.Module subclass should you use to define layers?
easy
A. Outside the class definition
B. Inside the forward method
C. Inside the __init__ method
D. Inside the train method

Solution

  1. Step 1: Identify where layers are defined

    Layers must be created once when the model is built, so they go in __init__.
  2. Step 2: Understand the role of forward

    forward defines how data moves through layers, not layer creation.
  3. Final Answer:

    Inside the __init__ method -> Option C
  4. Quick Check:

    Define layers in __init__ = A [OK]
Hint: Layers go in __init__, data flow in forward [OK]
Common Mistakes:
  • Defining layers inside forward method
  • Placing layers outside the class
  • Using a non-existent train method for layers
3. Given this PyTorch model code, what will print(model(torch.ones(1, 3))) output?
import torch
import torch.nn as nn

class SimpleModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear = nn.Linear(3, 2)
    def forward(self, x):
        return self.linear(x) + 1

model = SimpleModel()
medium
A. A tensor of shape (3, 2) with random values
B. A tensor of shape (1, 2) with values equal to linear output plus 1
C. An error because forward is missing
D. A tensor of zeros

Solution

  1. Step 1: Analyze the model structure

    The model has a linear layer from 3 inputs to 2 outputs, then adds 1 to output.
  2. Step 2: Understand the input and output shapes

    Input shape is (1,3), so output shape is (1,2). The output is linear(x) + 1.
  3. Final Answer:

    A tensor of shape (1, 2) with values equal to linear output plus 1 -> Option B
  4. Quick Check:

    Output shape (1,2) and adds 1 = B [OK]
Hint: Check layer shapes and operations in forward [OK]
Common Mistakes:
  • Confusing input and output shapes
  • Expecting an error due to missing forward
  • Assuming output is zeros without computation
4. What is wrong with this PyTorch model code?
import torch.nn as nn

class BadModel(nn.Module):
    def __init__(self):
        self.linear = nn.Linear(4, 2)
    def forward(self, x):
        return self.linear(x)
medium
A. Missing call to super().__init__() in __init__
B. forward method should not return anything
C. Layers should be defined inside forward, not __init__
D. nn.Linear requires 3 input features, not 4

Solution

  1. Step 1: Check __init__ method correctness

    In nn.Module subclasses, you must call super().__init__() to initialize the base class.
  2. Step 2: Verify other parts

    forward returns output correctly; layers are defined in __init__; input size 4 is valid.
  3. Final Answer:

    Missing call to super().__init__() in __init__ -> Option A
  4. Quick Check:

    Always call super().__init__() in __init__ = D [OK]
Hint: Always call super().__init__() in your model's __init__ [OK]
Common Mistakes:
  • Forgetting super().__init__() call
  • Defining layers inside forward method
  • Misunderstanding nn.Linear input size
5. You want to create a PyTorch model that has two linear layers and applies ReLU between them. How should you organize this using nn.Module?
hard
A. Define both linear layers in __init__ and apply ReLU in forward.
B. Define layers and ReLU calls inside forward only.
C. Define layers outside the class and call them in forward.
D. Use only one linear layer and apply ReLU twice in forward.

Solution

  1. Step 1: Organize layers in __init__

    Both linear layers should be created once in __init__ to register parameters properly.
  2. Step 2: Define data flow in forward

    Apply the first linear layer, then ReLU, then second linear layer inside forward.
  3. Final Answer:

    Define both linear layers in __init__ and apply ReLU in forward. -> Option A
  4. Quick Check:

    Layers in __init__, logic in forward = A [OK]
Hint: Layers in __init__, operations in forward [OK]
Common Mistakes:
  • Defining layers inside forward causing re-creation
  • Not using ReLU between layers
  • Defining layers outside the class
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