0
0
PyTorchml~5 mins

__init__ for layers in PyTorch - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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
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()
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
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
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
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.