Dropout and Regularization
🎭 What is dropout? Dropout randomly zeroes a fraction
pof a layer's activations on every training step, so the network cannot rely on any single unit and learns redundant features instead. At inference nothing is dropped. Like L2 regularization and early stopping, it trades a little training fit for better performance on new data.
It is a short, common question in deep learning coding rounds, often asked right after an MLP with backprop, and it tests one idea above all: what changes between training and inference.
The Prompt
Dropout is a mask and a multiply, so the interviewer listens for the scaling, the mode switch and the backward pass, then asks how it compares with other regularizers.
"Implement dropout as a layer with forward and backward passes and training and evaluation modes. Then add L2 regularization and early stopping to a training loop."
Clarifying Questions
You: Does p mean the probability of dropping a unit, as in PyTorch?
Interviewer: Yes.
You: Should evaluation mode be the identity? That means scaling by 1 / (1 - p) during training, the
inverted form.
Interviewer: Yes, match PyTorch.
You: What range of p do I support? At p = 1 every unit is dropped and the scale divides by zero.
Interviewer: Reject p = 1, or handle it; your choice.
You: For early stopping, what do I watch and what do I return?
Interviewer: Validation loss, a patience in steps, and return the best weights, not the last.
What You'll Build
Dropout(p, seed)withforward(x, training) -> same shapeandbackward(dout) -> same shape.l2_penalty(params, lam) -> (loss, grads):0.5 * lam * sum ||W||^2and its gradients.EarlyStopping(patience, min_delta)withstep(val_loss, params) -> boolthat keeps the best parameters.fit(X, y, lam, early_stop): a gradient-descent loop that uses both and reports the step it stopped at.