0
0
PyTorchml~20 mins

Linear (fully connected) layers in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linear Layer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
Output shape of a linear layer
Given the following PyTorch code, what is the shape of the output tensor after passing input through the linear layer?
PyTorch
import torch
import torch.nn as nn

input_tensor = torch.randn(10, 5)  # batch size 10, input features 5
linear_layer = nn.Linear(5, 3)  # input features 5, output features 3
output = linear_layer(input_tensor)
print(output.shape)
Atorch.Size([5, 3])
Btorch.Size([10, 3])
Ctorch.Size([3, 10])
Dtorch.Size([10, 5])
Attempts:
2 left
💡 Hint
Remember the linear layer transforms the last dimension from input features to output features, keeping batch size the same.
Model Choice
intermediate
1:30remaining
Choosing the correct linear layer for input-output sizes
You have input data with 20 features and want to output predictions with 4 values per example. Which PyTorch linear layer correctly matches this?
Ann.Linear(20, 4)
Bnn.Linear(4, 20)
Cnn.Linear(20, 20)
Dnn.Linear(4, 4)
Attempts:
2 left
💡 Hint
The first argument is input features, the second is output features.
Hyperparameter
advanced
1:30remaining
Effect of changing output features in a linear layer
If you increase the output features of a linear layer from 10 to 50, which of the following is true?
AThe batch size of input must be reduced.
BThe number of learnable parameters decreases.
CThe number of learnable parameters increases.
DThe number of learnable parameters stays the same.
Attempts:
2 left
💡 Hint
Learnable parameters depend on input and output features.
🔧 Debug
advanced
1:30remaining
Identifying error in linear layer input size mismatch
What error will this code raise when running the forward pass?
PyTorch
import torch
import torch.nn as nn

linear = nn.Linear(8, 4)
input_tensor = torch.randn(5, 10)
output = linear(input_tensor)
ARuntimeError: size mismatch for input features
BTypeError: input must be a tensor
CValueError: output size must be positive
DNo error, runs successfully
Attempts:
2 left
💡 Hint
Check if input tensor's last dimension matches linear layer's input features.
🧠 Conceptual
expert
2:00remaining
Why use bias in linear layers?
Which statement best explains the role of the bias term in a linear (fully connected) layer?
ABias is used to increase the batch size during training.
BBias reduces the number of parameters to prevent overfitting.
CBias normalizes the input features before multiplication.
DBias allows the model to shift the output independently of the input, enabling better fitting of data.
Attempts:
2 left
💡 Hint
Think about what happens if bias is zero.