Bird
Raised Fist0
PyTorchml~20 mins

Defining a model class in PyTorch - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PyTorch Model Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple PyTorch model forward pass
What is the output shape of the tensor after running the forward method of this model with input shape (10, 5)?
PyTorch
import torch
import torch.nn as nn

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

    def forward(self, x):
        return self.linear(x)

model = SimpleModel()
input_tensor = torch.randn(10, 5)
output = model(input_tensor)
output_shape = output.shape
print(output_shape)
Atorch.Size([10, 3])
Btorch.Size([5, 3])
Ctorch.Size([10, 5])
Dtorch.Size([3, 10])
Attempts:
2 left
💡 Hint
Remember the linear layer changes the last dimension from input features to output features.
❓ Model Choice
intermediate
2:00remaining
Choosing the correct model class definition
Which of the following PyTorch model class definitions correctly implements a model with one hidden layer of size 4 and ReLU activation?
A
class MyModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.hidden = nn.Linear(4, 3)
        self.output = nn.Linear(3, 2)
    def forward(self, x):
        x = torch.relu(self.hidden(x))
        return self.output(x)
B
class MyModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.hidden = nn.Linear(3, 4)
        self.output = nn.Linear(4, 2)
    def forward(self, x):
        x = self.hidden(x)
        x = torch.relu(self.output(x))
        return x
C
class MyModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.hidden = nn.Linear(3, 2)
        self.output = nn.Linear(2, 4)
    def forward(self, x):
        x = torch.relu(self.hidden(x))
        return self.output(x)
D
class MyModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.hidden = nn.Linear(3, 4)
        self.output = nn.Linear(4, 2)
    def forward(self, x):
        x = torch.relu(self.hidden(x))
        return self.output(x)
Attempts:
2 left
💡 Hint
Check the input and output sizes of each layer and the order of activation.
❓ Hyperparameter
advanced
2:00remaining
Effect of changing hidden layer size in a model class
If you increase the hidden layer size from 10 to 100 in a fully connected PyTorch model, which of the following is a likely effect?
AThe model will train faster because it has more neurons.
BThe model will have more parameters and may overfit more easily.
CThe model will always have worse accuracy due to overfitting.
DThe model will use less memory during training.
Attempts:
2 left
💡 Hint
Think about how model size affects capacity and training behavior.
🔧 Debug
advanced
2:00remaining
Identify the error in this PyTorch model class
What error will this PyTorch model class raise when instantiated?
PyTorch
import torch.nn as nn

class FaultyModel(nn.Module):
    def __init__(self):
        self.linear = nn.Linear(5, 2)

    def forward(self, x):
        return self.linear(x)

model = FaultyModel()
AAttributeError because nn.Module __init__ is not called
BTypeError because forward method is missing
CSyntaxError due to missing colon
DNo error, model instantiates correctly
Attempts:
2 left
💡 Hint
Check the __init__ method and superclass initialization.
🧠 Conceptual
expert
2:00remaining
Why define a model class instead of using nn.Sequential?
Which reason best explains why you might define a custom PyTorch model class instead of using nn.Sequential?
ABecause custom classes always train faster than nn.Sequential models.
BBecause nn.Sequential cannot be used with linear layers.
CTo implement complex forward logic like multiple inputs, conditional operations, or loops.
DTo avoid writing the forward method manually.
Attempts:
2 left
💡 Hint
Think about flexibility in model design and forward pass control.

Practice

(1/5)
1. What is the main purpose of defining a model class in PyTorch?
easy
A. To automatically tune hyperparameters
B. To organize layers and define how data flows through the model
C. To store training data separately
D. To visualize the model architecture

Solution

  1. Step 1: Understand the role of a model class

    In PyTorch, a model class helps organize the layers and defines the forward pass, which is how data flows through the model.
  2. Step 2: Compare with other options

    Storing training data, tuning hyperparameters, or visualizing architecture are not the main purposes of defining the model class.
  3. Final Answer:

    To organize layers and define how data flows through the model -> Option B
  4. Quick Check:

    Model class = organize layers + data flow [OK]
Hint: Model class defines layers and data flow [OK]
Common Mistakes:
  • Thinking model class stores data
  • Confusing model class with training process
  • Assuming model class tunes hyperparameters
2. Which of the following is the correct way to start defining a PyTorch model class?
easy
A. class MyModel(nn.Module):
B. class MyModel():
C. def MyModel(nn.Module):
D. model = nn.Module()

Solution

  1. Step 1: Recall PyTorch model class syntax

    PyTorch models must inherit from nn.Module, so the class definition should be class MyModel(nn.Module):.
  2. Step 2: Check other options

    class MyModel(): misses inheritance, C uses def instead of class, and D creates an instance, not a class.
  3. Final Answer:

    class MyModel(nn.Module): -> Option A
  4. Quick Check:

    Model class inherits nn.Module [OK]
Hint: Model class must inherit nn.Module [OK]
Common Mistakes:
  • Forgetting to inherit nn.Module
  • Using def instead of class
  • Creating instance instead of class
3. Given this PyTorch model class snippet, what will print(model(torch.tensor([[1.0, 2.0]]))) output?
import torch
import torch.nn as nn

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

model = SimpleModel()
with torch.no_grad():
    model.linear.weight.fill_(1.0)
    model.linear.bias.fill_(0.0)
medium
A. tensor([[2.]])
B. RuntimeError due to wrong input shape
C. tensor([[0.]])
D. tensor([[3.]])

Solution

  1. Step 1: Understand the model and weights

    The model has a linear layer with 2 inputs and 1 output. We set weights to 1.0 and bias to 0.0.
  2. Step 2: Calculate output for input tensor

    Input is [[1.0, 2.0]]. The output is (1*1.0 + 2*1.0) + 0 = 3.0, so output tensor is [[3.0]].
  3. Final Answer:

    tensor([[3.]]) -> Option D
  4. Quick Check:

    Sum inputs * weights + bias = 3.0 [OK]
Hint: Sum inputs times weights plus bias for linear output [OK]
Common Mistakes:
  • Ignoring bias value
  • Mixing input shape
  • Assuming default weights instead of set values
4. What is wrong with this PyTorch model class definition?
import torch.nn as nn

class BadModel(nn.Module):
    def __init__(self):
        self.linear = nn.Linear(3, 1)
    def forward(self, x):
        return self.linear(x)
medium
A. Class should not inherit from nn.Module
B. forward method should be named backward
C. Missing call to super().__init__() in __init__ method
D. nn.Linear should have input size 1, not 3

Solution

  1. Step 1: Check __init__ method

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

    The forward method name is correct, input size 3 is valid, and inheriting nn.Module is required.
  3. Final Answer:

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

    Always call super().__init__() in __init__ [OK]
Hint: Always call super().__init__() in model __init__ [OK]
Common Mistakes:
  • Forgetting super().__init__()
  • Renaming forward method incorrectly
  • Changing input size without reason
5. You want to define a PyTorch model class with two linear layers and a ReLU activation between them. Which code correctly implements this?
hard
A. class TwoLayerModel(nn.Module): def __init__(self): super().__init__() self.layer1 = nn.Linear(4, 3) self.layer2 = nn.Linear(3, 1) self.relu = nn.ReLU() def forward(self, x): x = self.layer1(x) x = self.relu(x) x = self.layer2(x) return x
B. class TwoLayerModel(nn.Module): def __init__(self): self.layer1 = nn.Linear(4, 3) self.layer2 = nn.Linear(3, 1) self.relu = nn.ReLU() def forward(self, x): x = self.layer1(x) x = self.relu(x) x = self.layer2(x) return x
C. class TwoLayerModel(nn.Module): def __init__(self): super().__init__() self.layer1 = nn.Linear(3, 4) self.layer2 = nn.Linear(1, 3) self.relu = nn.ReLU() def forward(self, x): x = self.layer1(x) x = self.relu(x) x = self.layer2(x) return x
D. class TwoLayerModel: def __init__(self): super().__init__() self.layer1 = nn.Linear(4, 3) self.layer2 = nn.Linear(3, 1) self.relu = nn.ReLU() def forward(self, x): x = self.layer1(x) x = self.relu(x) x = self.layer2(x) return x

Solution

  1. Step 1: Check inheritance and super call

    class TwoLayerModel(nn.Module): def __init__(self): super().__init__() self.layer1 = nn.Linear(4, 3) self.layer2 = nn.Linear(3, 1) self.relu = nn.ReLU() def forward(self, x): x = self.layer1(x) x = self.relu(x) x = self.layer2(x) return x correctly inherits nn.Module and calls super().__init__(). class TwoLayerModel(nn.Module): def __init__(self): self.layer1 = nn.Linear(4, 3) self.layer2 = nn.Linear(3, 1) self.relu = nn.ReLU() def forward(self, x): x = self.layer1(x) x = self.relu(x) x = self.layer2(x) return x misses super().__init__(), and D does not inherit nn.Module.
  2. Step 2: Verify layer sizes and forward method

    class TwoLayerModel(nn.Module): def __init__(self): super().__init__() self.layer1 = nn.Linear(4, 3) self.layer2 = nn.Linear(3, 1) self.relu = nn.ReLU() def forward(self, x): x = self.layer1(x) x = self.relu(x) x = self.layer2(x) return x uses correct input-output sizes (4->3, 3->1) and applies ReLU between layers. class TwoLayerModel(nn.Module): def __init__(self): super().__init__() self.layer1 = nn.Linear(3, 4) self.layer2 = nn.Linear(1, 3) self.relu = nn.ReLU() def forward(self, x): x = self.layer1(x) x = self.relu(x) x = self.layer2(x) return x has wrong layer sizes.
  3. Final Answer:

    Option A correctly defines the model with two linear layers and ReLU -> Option A
  4. Quick Check:

    Inherit nn.Module + super().__init__() + correct layers [OK]
Hint: Inherit nn.Module, call super().__init__(), correct layer sizes [OK]
Common Mistakes:
  • Forgetting super().__init__()
  • Wrong input-output sizes in layers
  • Not inheriting nn.Module
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