B. Linear layer input features do not match input tensor features.
C. Output tensor shape is incorrect.
D. Missing activation function after Linear layer.
Solution
Step 1: Check Linear layer input features
The Linear layer expects input features of size 3.
Step 2: Check input tensor shape
The input tensor shape is (4, 4), so each sample has 4 features, which does not match the expected 3.
Final Answer:
Linear layer input features do not match input tensor features. -> Option B
Quick Check:
Input features must match layer's in_features [OK]
Hint: Input feature size must match Linear layer's in_features [OK]
Common Mistakes:
Confusing batch size with feature size
Ignoring feature dimension mismatch
Thinking activation function is mandatory
5. You want to build a simple neural network with one hidden Linear layer that takes 8 input features, has 4 hidden units, and outputs 2 classes. Which PyTorch code correctly defines this model?
hard
A. import torch.nn as nn
model = nn.Sequential(
nn.Linear(8, 2),
nn.ReLU(),
nn.Linear(4, 2)
)
B. import torch.nn as nn
model = nn.Sequential(
nn.Linear(4, 8),
nn.ReLU(),
nn.Linear(2, 4)
)
C. import torch.nn as nn
model = nn.Sequential(
nn.Linear(8, 4),
nn.ReLU(),
nn.Linear(4, 2)
)
D. import torch.nn as nn
model = nn.Sequential(
nn.Linear(2, 4),
nn.ReLU(),
nn.Linear(8, 4)
)
Solution
Step 1: Define input to hidden layer
The first Linear layer should map 8 input features to 4 hidden units: nn.Linear(8, 4).
Step 2: Define hidden to output layer
The second Linear layer should map 4 hidden units to 2 output classes: nn.Linear(4, 2).
Step 3: Check activation function placement
ReLU activation is correctly placed between the two Linear layers.
Final Answer:
Correct model definition with layers: Linear(8,4), ReLU, Linear(4,2) -> Option C
Quick Check:
Input -> hidden -> output sizes match [OK]
Hint: Match layer sizes: input->hidden->output with activation in between [OK]
Common Mistakes:
Swapping input and output sizes in layers
Placing activation after output layer
Using wrong feature sizes in layers
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.