Skip to content
datarekha
Deep Learning Medium Asked at GoogleAsked at MetaAsked at AmazonAsked at Microsoft

What causes overfitting in deep neural networks and how do you fight it?

The short answer

Overfitting occurs when a neural network learns training-set noise or shortcuts instead of patterns that generalize to new data. I diagnose the train-validation gap and address it with honest data splits, representative data, semantic augmentation, tuned regularization, early stopping, and an appropriately sized or pretrained model.

How to think about it

Suppose a defect detector reaches 99.8% accuracy on 3,200 training photos but only 76% on 800 unseen production photos. That is overfitting: the network has learned training-set-specific noise or shortcuts instead of patterns that survive on new data. I fight it by making evaluation honest, improving the data, and applying tuned regularization, early stopping, and capacity control.

Why it happens

Training minimizes training loss, the average error on examples the model sees. What we actually care about is error on future examples. A validation set, held-out data used while choosing models, estimates that future performance. The difference between training and validation performance is the generalization gap.

A neural network can fit many different rules to the same finite dataset. Its effective capacity, meaning its ability to represent many possible input-output rules, comes from more than parameter count. Architecture, optimizer, training duration, data augmentation, and regularization all affect it.

With enough freedom, the network can learn a real signal, such as the shape of a scratch, or a shortcut, such as a particular camera angle that happens to correlate with scratched boards in the training data. Incorrect or inconsistent labels create an even harsher problem. If one clean board is labeled “scratched,” fitting the training set perfectly requires the model to memorize that exception.

Take the circuit-board example. A 12-million-parameter convolutional neural network trains on 4,000 labeled images: 3,200 for training and 800 for validation. At epoch 10, one complete pass through the training set, it reaches 94% training accuracy and 92% validation accuracy. At epoch 60, training accuracy is 99.8%, but validation accuracy has fallen to 78%. Validation loss has climbed from 0.34 to 1.08.

That rising loss is useful evidence. The model is not merely making more mistakes; it is becoming more confident in some wrong predictions. An inspection might reveal that the training photos contain a gray fixture edge beside most scratched boards. The network found an easy rule. Unfortunately, next week’s boards do not come with the same fixture.

Warning — parameter count is not the diagnosis. “The model has more parameters than examples” is an incomplete answer. Modern networks can have more parameters than training examples and still generalize because the architecture and optimizer impose useful preferences, often called inductive bias. A smaller model can overfit a tiny, noisy dataset, while a larger pretrained model can outperform it. Look at held-out behavior, not parameter count alone.

How I fight it

1. Make the evaluation honest first

Before adding dropout, I check whether the validation set resembles deployment.

Use a train, validation, and test split. The test set is held back until the end; using it repeatedly to choose hyperparameters quietly turns it into another validation set. For industrial images, split by physical board, production run, or camera rather than by individual image. Two photos of the same board should not land on opposite sides of the split. For user data, group by user. For forecasting, split by time.

I also remove near-duplicate images, check label quality, and compare performance by class and subgroup. A random image split can report 98% validation accuracy while a time-based production split reports 74%. That is not a dropout problem. It is leakage or distribution shift.

The learning curves help separate common cases:

  • High training performance and low representative validation performance suggests overfitting.
  • Low training and low validation performance suggests underfitting, poor optimization, weak features, or bad labels.
  • High training and validation performance but poor production performance suggests a deployment mismatch, such as a new camera, population, or time period.

2. Improve the information the model sees

More representative labeled data usually beats a clever regularizer. It gives the model more examples of which features remain useful and reduces the chance that one accidental correlation dominates.

Clean labels matter just as much. Deep networks are remarkably willing to memorize annotation mistakes. If a defect is subtle, define the labeling rule, measure annotator agreement, and review examples where the model is confidently wrong.

Data augmentation creates altered training examples to encourage useful invariances. It does not create independent information, but it can stop the model relying on irrelevant detail. For the board detector, small crops, mild brightness changes, or rotations make sense only if the defect label should remain unchanged. A horizontal flip is safe only if board orientation carries no meaning. A crop that removes the scratch is not regularization; it is sabotage with a cheerful name.

Keep validation and test data clean apart from deterministic preprocessing such as resizing and normalization. Applying random augmentation to validation makes the metric noisy and makes checkpoint selection less trustworthy. Mixup and CutMix can help some classification tasks, but they may blur or remove small defects, so they need an ablation rather than faith.

3. Constrain the learned solution

Regularization changes training so that brittle solutions become less attractive.

TechniqueMechanismMain caveat
Weight decayShrinks parameters toward zero and favors lower-norm solutionsThe useful value depends on model, learning rate, and batch size
DropoutRandomly removes activations during training, encouraging redundant representationsIt can hurt optimization or add little value in some modern convolutional models
Batch normalizationUses batch statistics during training; their noise can mildly regularizeIt is not a guaranteed overfitting cure, and train/evaluation behavior differs
Pretraining or a smaller headSupplies a useful prior and limits what must be learned from a small datasetFreezing too much can prevent learning task-specific features

A practical PyTorch configuration might look like this:

import torch

from torch import nn

model = nn.Sequential(
    nn.Linear(512, 256),
    nn.ReLU(),
    nn.Dropout(p=0.30),
    nn.Linear(256, 10),
)

optimizer = torch.optim.AdamW(
    model.parameters(),
    lr=1e-3,
    weight_decay=1e-4,
)

model.train()
# Run training batches here.

model.eval()
with torch.no_grad():
    validation_logits = model(x_valid)

A dropout probability of 0.30 means each activation has a 30% chance of being removed during training. The 1e-4 weight-decay value is a starting experiment, not a universal default. With AdamW, weight decay is decoupled from the adaptive gradient update; it is not exactly the same operation as adding an L2 penalty to Adam’s loss. In either case, tune it against a fixed validation protocol.

4. Select the checkpoint, then control capacity

Early stopping is model selection: stop using the weights when validation performance stops improving, rather than automatically keeping the final epoch. In the board example, if validation loss is lowest at epoch 18 with a value of 0.31, I save and deploy that checkpoint even if training continues to epoch 60. A patience window is useful when validation measurements are noisy.

If overfitting remains, I try a narrower or shallower model, a smaller task-specific head, stronger but valid augmentation, or a pretrained backbone. I change one major factor at a time and compare on the same split and, where practical, across several random seeds.

I do not automatically shrink the model. If training and validation performance are both poor, stronger regularization usually makes things worse. The model may need more capacity, a better learning rate, more training, or better labels.

The senior-level nuance

The textbook slogan is “more parameters cause overfitting.” The better answer is “too much effective freedom relative to the amount, quality, and diversity of signal.” Large networks often fit the training set exactly, yet still generalize because stochastic gradient descent, architecture, pretraining, and regularization favor some solutions over others. Increasing model size can even improve test performance in some regimes.

Every defence trades variance for bias. Too much dropout can make both training and validation accuracy stall at, say, 84%. Excessive weight decay can suppress a genuinely useful but rare visual feature. Aggressive augmentation can change the task. Early stopping can preserve a checkpoint before a minority class has been learned. The symptom of too much regularization is often not a widening train-validation gap but poor training performance itself.

I also remember that validation can overfit. If a team tries 200 augmentation policies and keeps the one with the best validation score, the validation set has become part of training. On small datasets, repeated cross-validation, multiple seeds, and a genuinely untouched final test set provide a more honest estimate.

For the formal regularization vocabulary, see regularization.

A failure mode I check immediately

A common production bug is evaluating with dropout still enabled. The first symptom is that repeated predictions on the same image produce different logits, or validation accuracy changes between passes even though the weights did not change. Call model.eval() before evaluation or inference. torch.no_grad() saves gradient memory; it does not disable dropout. Evaluation mode also makes BatchNorm use its stored running statistics rather than current training-batch statistics.

Another failure appears as excellent random-split validation accuracy followed by a production collapse. That usually means duplicated entities, a leaked time period, or a distribution shift. No amount of dropout fixes a validation set that is answering the wrong question.

What they’ll ask next

Does more data always beat dropout?

No, but representative data usually attacks the cause more directly. More examples of new cameras, factories, users, or edge cases constrain the shortcut the model can use. Dropout is cheaper and can help when collecting labels is expensive, but it cannot repair systematic label errors or a misleading split.

How do you distinguish overfitting from distribution shift?

I compare training performance, an in-distribution validation set, a group or time-based holdout, and production slices. High training performance with poor representative validation performance is classic overfitting. High random-split validation performance with poor future or production performance points more strongly to leakage or distribution shift.

Why not always use a smaller model?

Because smaller is not automatically simpler in the relevant sense. It may underfit the real signal, while a pretrained larger model may learn useful representations with few task-specific parameters. I choose capacity using representative held-out data, error analysis, latency and memory constraints, and performance across important subgroups.

Say this in the interview

“Overfitting is a generalization failure caused by the model learning noise or shortcuts in finite training data; I diagnose it with honest held-out evaluation, then combine better data, valid augmentation, tuned regularization, early stopping, and appropriate model capacity rather than relying on one magic switch.”

Learn it properly Dropout, BN, LN

Keep practising

All Deep Learning questions

Explore further