Challenge - 5 Problems
Linear Layer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1: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)
Attempts:
2 left
💡 Hint
Remember the linear layer transforms the last dimension from input features to output features, keeping batch size the same.
✗ Incorrect
The input tensor has shape (10, 5). The linear layer maps 5 input features to 3 output features. The batch size (10) remains unchanged, so output shape is (10, 3).
❓ Model Choice
intermediate1: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?
Attempts:
2 left
💡 Hint
The first argument is input features, the second is output features.
✗ Incorrect
The linear layer should take 20 input features and produce 4 output features, so nn.Linear(20, 4) is correct.
❓ Hyperparameter
advanced1: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?
Attempts:
2 left
💡 Hint
Learnable parameters depend on input and output features.
✗ Incorrect
The number of parameters in a linear layer is input_features * output_features + output_features (bias). Increasing output features increases parameters.
🔧 Debug
advanced1: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)
Attempts:
2 left
💡 Hint
Check if input tensor's last dimension matches linear layer's input features.
✗ Incorrect
The linear layer expects input features of size 8, but input tensor has size 10 in last dimension, causing a RuntimeError.
🧠 Conceptual
expert2:00remaining
Why use bias in linear layers?
Which statement best explains the role of the bias term in a linear (fully connected) layer?
Attempts:
2 left
💡 Hint
Think about what happens if bias is zero.
✗ Incorrect
Bias adds a constant term to the output, allowing the model to fit data that does not pass through the origin.