0
0
PyTorchml~20 mins

__init__ for layers in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Layer Initialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple PyTorch layer initialization
What is the output of the following code snippet that initializes a linear layer and prints its weight shape?
PyTorch
import torch
import torch.nn as nn

layer = nn.Linear(10, 5)
print(layer.weight.shape)
Atorch.Size([10, 10])
Btorch.Size([10, 5])
Ctorch.Size([5, 10])
Dtorch.Size([5, 5])
Attempts:
2 left
💡 Hint
Remember that nn.Linear(in_features, out_features) creates weights of shape (out_features, in_features).
Model Choice
intermediate
2:00remaining
Choosing the correct __init__ signature for a convolutional layer
Which of the following __init__ method signatures correctly initializes a 2D convolutional layer with 3 input channels, 16 output channels, and a kernel size of 3?
A
def __init__(self):
    super().__init__()
    self.conv = nn.Linear(3, 16)
B
def __init__(self):
    super().__init__()
    self.conv = nn.Conv1d(3, 16, 3)
C
def __init__(self):
    super().__init__()
    self.conv = nn.Conv2d(16, 3, 3)
D
def __init__(self):
    super().__init__()
    self.conv = nn.Conv2d(3, 16, 3)
Attempts:
2 left
💡 Hint
Conv2d expects (in_channels, out_channels, kernel_size).
Hyperparameter
advanced
2:00remaining
Effect of bias parameter in layer initialization
In PyTorch, when initializing a linear layer with nn.Linear(10, 5, bias=False), what is the effect of setting bias=False?
AThe layer will initialize bias with zeros.
BThe layer will not have a bias term added to the output.
CThe layer will raise an error because bias is required.
DThe layer will add a bias term initialized randomly.
Attempts:
2 left
💡 Hint
Bias controls whether an extra constant term is added to the output.
🔧 Debug
advanced
2:00remaining
Identify the error in this layer __init__ method
What error will occur when running this code snippet?
PyTorch
import torch.nn as nn

class MyModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear = nn.Linear(10, 5)
AAttributeError because super().__init__() is called after layer initialization
BSyntaxError due to missing colon
CTypeError because nn.Linear requires extra arguments
DNo error, code runs fine
Attempts:
2 left
💡 Hint
In PyTorch, always call super().__init__() before defining layers.
🧠 Conceptual
expert
3:00remaining
Understanding parameter registration in __init__
Why is it important to define layers as attributes inside the __init__ method of a PyTorch nn.Module subclass?
ABecause only attributes defined in __init__ are registered as model parameters and updated during training.
BBecause layers defined outside __init__ run faster during training.
CBecause layers defined in __init__ cannot be saved or loaded.
DBecause layers defined in __init__ do not require calling super().__init__().
Attempts:
2 left
💡 Hint
Think about how PyTorch tracks parameters for optimization.