Overview
Curated: · Written: · Reviewed:
Neural Network Fundamentals
In one line
A feed-forward network stacks dense layers, each computing z = Wx + b followed by an elementwise nonlinearity, and trains by backpropagating the loss gradient through the chain rule. Reach for it when the signal is high-dimensional or you need a differentiable component, and you have enough labelled data to beat a linear or tree baseline.
Key definitions
- Pre-activation: z = Wx + b, the value fed into the activation function.
- Logit: the raw, unnormalised output that softmax cross-entropy consumes directly.
- Dead ReLU: a unit whose pre-activation stays negative across the data, so it passes zero gradient upstream.
- Saturation: sigmoid or tanh outputs near their limits, where the local derivative is near zero.
- Gradient accumulation along paths: a parameter used on several paths receives the sum of every contribution.
- Running statistics: the BatchNorm mean and variance estimates used at inference.
Formulas and complexity
A dense layer costs O(B x in x out) multiply-adds per batch. Adam keeps two moments per parameter, so a training checkpoint is roughly 3x the model size in fp32.
How it works
- Each dense layer computes z = Wx + b on the batch, then applies the activation elementwise.
- The loss is computed from the final output: logits for classification, the linear output for regression.
- Backprop walks the graph from the loss, applying the chain rule at each node. Shared parameters receive the sum over paths.
.detach()orrequires_grad=Falsecuts the graph. Nothing upstream of the cut receives gradient.
When to use and when not to
| Situation | Dense network | Prefer |
|---|---|---|
| High-dimensional dense signal (embeddings, images, text features), many labels | Good fit | n/a |
| Tabular data, modest row count | Usually loses | Gradient-boosted trees |
| Per-feature interpretability or monotonicity required | Weak | Linear model |
| Calibrated probabilities from small data | Less reliable | Logistic regression |
| Tiny latency or memory budget | Heavy | Linear model or lookup |
| Bit-exact reproducibility across machines | Hard | Simpler model |
Traps interviewers probe
- Softmax then log. Applying softmax and then -log(p) gives inf at extreme logits (logits [1000, 0, 0] give inf naively, 1000.0 with fused cross-entropy). Fix: pass logits to the fused loss.
- Stacking linear layers without activations. Stacked affine maps collapse to one affine map, so depth adds no capacity. Fix: put a nonlinearity between layers.
- Xavier with ReLU. ReLU zeroes half the pre-activations and halves the variance. Fix: He initialization.
- Dropout or batch statistics left in training mode at inference. Fix: call
model.eval(). Dropout becomes identity and BatchNorm uses running statistics. - Treating gradient clipping as the fix for exploding gradients. Clipping caps the update size but hides the cause. Fix: check learning rate, initialization, normalization and residual paths, then say so explicitly.
Interview tips
Open with the mechanism: a dense network is a stack of learned linear projections, each followed by a nonlinearity, and the nonlinearity is what gives depth its capacity.
Follow-ups to expect:
- Shapes for input (B, 784) into a 256-unit layer: W is (256, 784), b is (256,), output is (B, 256).
- Which initialization with ReLU, and why: He, because ReLU halves the variance per unit.
- What happens at a detached branch: its outputs are constants and nothing upstream receives gradient.
- Offline metric improved, A/B test did not: check distribution shift, leakage in the offline split, and metric-proxy mismatch, in that order.
- Loss plateaus early: compare per-layer gradient norms with depth, and check for saturation or dead units.
30-second outline:
- Define the layer: z = Wx + b, then a nonlinearity.
- Why the nonlinearity matters: without it the stack is one affine map.
- Loss pairing: logits into the fused cross-entropy, or a regression loss matched to the error cost.
- Training: backprop through the chain rule, gradients summed over shared paths.
- One trap to name (softmax then log, or Xavier with ReLU).
- When not to use it: tabular data with modest rows, or a need for calibration or interpretability.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.