Q&A-1Explain how a feed-forward neural network makes a prediction.
Answer
The 90-second answer
A feed-forward network is a fixed sequence of layers, and a prediction is one forward pass through them with the weights frozen. Each layer computes an affine map of its input, z = Wx + b, then applies a fixed nonlinearity, and hands the result to the next layer. The final layer emits raw scores (logits) or a scalar, and a task-specific head turns those into a class, a probability, or a value. Nothing is learned at inference; the only state that changes is activation buffers.
The mechanism, with numbers
Take a 2-2-1 MLP: two inputs, two ReLU hidden units, one sigmoid output. PyTorch 2.x nn.Linear stores weight shaped (out_features, in_features) and computes x @ weight.T + bias.
Input x = [1.0, 2.0].
Layer 1: W1 = [[0.5, -0.3], [0.2, 0.8]], b1 = [0.2, -0.2]
| unit | z = w·x + b | ReLU(z) |
|---|---|---|
| h1 | 0.5·1.0 + (-0.3)·2.0 + 0.2 = 0.1 | 0.1 |
| h2 | 0.2·1.0 + 0.8·2.0 + (-0.2) = 1.6 | 1.6 |
Layer 2: W2 = [0.7, -0.5], b2 = 0.05
z = 0.7·0.1 + (-0.5)·1.6 + 0.05 = 0.07 - 0.8 + 0.05 = -0.68
p = sigmoid(-0.68) = 1/(1 + e^0.68) ≈ 0.336
So the prediction is class 1 at p ≈ 0.336, class 0 at 0.664. Parameter count: (2·2 + 2) + (2·1 + 1) = 9.
Why the nonlinearity matters: without it, stacking affine maps collapses to a single affine map, W2(W1x + b1) + b2 = (W2W1)x + (W2b1 + b2), so a 10-layer net would be exactly as expressive as a linear model. ReLU is the default because its gradient is 1 for positive inputs, which keeps gradients from vanishing with depth; sigmoid and tanh saturate and their gradients approach 0.
Alternatives and when to pick them
- Activation: ReLU is cheap and the default; GELU is standard in transformers; tanh still appears in small control and RL nets where a bounded output helps.
- Normalization: BatchNorm normalizes across the batch and needs running statistics at serving time; LayerNorm normalizes per example and behaves identically in train and eval, which is why transformers use it.
- Depth vs width: for tabular data, 2-3 hidden layers of 64-256 units usually match much deeper nets; for images, convolutions share weights across positions and beat a dense net on parameter count.
- When not to use an FFN: small tabular datasets (gradient-boosted trees usually win), sequences where you need position sharing (use a conv or attention), or anything needing calibrated uncertainty.
Failure modes
- Shape mismatch:
matmulraises at build time. Cheap, caught in seconds. - Train/serve skew: dropout left enabled, or BatchNorm using batch statistics instead of running stats. Detection: run a fixed input batch through the model in
.eval()and through the serving path and diff the logits; a nonzero diff means the graphs differ. - Dead ReLU: a unit whose pre-activation is negative for every input has zero gradient forever. Detection: log the fraction of zeros per layer over a few thousand samples; a layer above ~50% zeros is suspect.
- Saturation: sigmoid/tanh outputs piling at 0 or 1 give near-zero gradients. Detection: activation histograms.
- Numerics: fp16 matmuls overflow past ~65504; loss scaling or bf16 avoids it.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.