What is the purpose of the __init__ method in a PyTorch layer class?
The __init__ method sets up the layer by defining its parameters and sub-layers. It prepares the layer to be used later in the forward pass.
Click to reveal answer
beginner
Why do we call super().__init__() inside the __init__ method of a PyTorch layer?
Calling super().__init__() initializes the parent class nn.Module, which is necessary for PyTorch to track parameters and manage the layer properly.
Click to reveal answer
beginner
How do you define a fully connected layer inside the __init__ method?
You create an instance of nn.Linear with input and output sizes, for example: self.fc = nn.Linear(input_size, output_size).
Click to reveal answer
intermediate
What happens if you forget to call super().__init__() in your layer's __init__?
PyTorch will not properly register the layer's parameters, which can cause errors during training or the model not learning correctly.
Click to reveal answer
beginner
Can you store multiple layers inside the __init__ method? How?
Yes, you can define multiple layers as attributes, for example: self.conv1 = nn.Conv2d(...) and self.fc1 = nn.Linear(...). This organizes the model parts clearly.
Click to reveal answer
What is the first thing you should do inside the __init__ method of a PyTorch layer?
ACall <code>super().__init__()</code>
BDefine the forward method
CInitialize the optimizer
DLoad the dataset
✗ Incorrect
Calling super().__init__() initializes the base nn.Module class, which is essential for the layer to work properly.
How do you define a linear layer with 10 inputs and 5 outputs inside __init__?
Aself.fc = nn.Linear(5, 10)
Bself.fc = nn.Linear(10, 5)
Cself.fc = nn.Conv2d(10, 5)
Dself.fc = nn.Linear()
✗ Incorrect
The first argument is input size (10), the second is output size (5) for nn.Linear.
What happens if you do not call super().__init__() in your layer?
AParameters are not registered properly
BThe model trains faster
CThe forward method runs twice
DNothing happens
✗ Incorrect
Without calling super().__init__(), PyTorch cannot track parameters, causing training issues.
Where do you define the layers like convolution or linear in a PyTorch model?
AOutside the class
BInside the <code>forward</code> method
CInside the <code>__init__</code> method
DIn the training loop
✗ Incorrect
Layers are defined in __init__ so they are created once and reused during forward passes.
Can you define multiple layers inside __init__?
AOnly if you use a list
BNo, only one layer per class
COnly if they are the same type
DYes, by assigning them as attributes
✗ Incorrect
You can define many layers as attributes like self.conv1, self.fc1, etc.
Explain the role of the __init__ method when creating a custom PyTorch layer.
Think about what happens before the model processes data.
You got /4 concepts.
Describe what could go wrong if you forget to call super().__init__() in your PyTorch layer's __init__.
Consider how PyTorch manages model parts internally.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the __init__ method in a PyTorch layer class?
easy
A. To save the model to disk
B. To set up the parts of the layer like linear or dropout layers
C. To train the model on data
D. To run the forward pass of the model
Solution
Step 1: Understand the role of __init__ in PyTorch
The __init__ method is used to prepare the layer by defining its parts, such as linear or dropout layers.
Step 2: Differentiate from other methods
The forward pass runs the data through the layer, training updates weights, and saving stores the model. These are not done in __init__.
Final Answer:
To set up the parts of the layer like linear or dropout layers -> Option B
Quick Check:
__init__ prepares layer parts = D [OK]
Hint: Remember: __init__ builds the layer parts first [OK]
Common Mistakes:
Confusing __init__ with the forward method
Thinking __init__ trains the model
Assuming __init__ saves the model
2. Which of the following is the correct way to start the __init__ method in a PyTorch layer class?
easy
A. def __init__(self): super()
B. def __init__(): super().__init__()
C. def __init__(self): super().__init__()
D. def __init__(self): init.super()
Solution
Step 1: Check the method signature
The __init__ method must have self as the first parameter.
Step 2: Call the parent class initializer correctly
Use super().__init__() to properly initialize the parent class.
Final Answer:
def __init__(self): super().__init__() -> Option C
Quick Check:
Correct __init__ start = B [OK]
Hint: Always include self and call super().__init__() first [OK]
Common Mistakes:
Omitting self parameter
Calling super() without __init__()
Wrong method signature without self
3. What will be the output shape of the linear layer defined below inside __init__ when input tensor has shape (10, 5)?
Step 1: Check the order of super().__init__() call
It must be called first to initialize the parent class properly.
Step 2: Verify layer dimensions and definitions
Linear layers should connect properly: first from 10 to 20, second from 20 to 5. Dropout is correctly defined with 0.3 probability.
Step 3: Identify the correct option
def __init__(self):
super().__init__()
self.linear1 = torch.nn.Linear(10, 20)
self.linear2 = torch.nn.Linear(20, 5)
self.dropout = torch.nn.Dropout(0.3) matches correct order, layer sizes, and dropout setup. Codes that call super().__init__() last or have mismatched dimensions like Linear(20,10) and Linear(5,20) are incorrect.