← Back to Blog

Regularization: Preventing Your Network from Overfitting

Introduction

A sufficiently flexible neural network can memorize its training data, including noise. Regularization discourages this by constraining the model or the training process, so that it is more likely to learn patterns that generalize.

This article covers five regularization techniques: L1, L2, Dropout, Data Augmentation, and Early Stopping. Each works differently but shares a goal: make your model perform well on data it hasn't seen.

The Overfitting Problem

Overfitting occurs when a model learns the training data too well—capturing noise and specific details instead of underlying patterns. The result: high training accuracy but low test accuracy.

Schematic losses: training falls while validation can rise later; regularization may reduce this gap.

Schematic curves, generated analytically. No experiment was run to produce this chart.

The chart shows a typical overfitting scenario. Training loss keeps decreasing while validation loss increases after a certain point. The network is memorizing, not generalizing.

L1 Regularization

L1 regularization adds a penalty for large weights to the loss function:

loss = standard_loss + λ × Σ|weights|

The penalty pushes weights toward zero. With a suitable solver, such as proximal gradient descent, some weights become exactly zero, which removes those connections and creates sparse models. Plain SGD on this penalty usually leaves weights small but not exactly zero.

Feature Selection

Unlike L2, which shrinks weights gradually, L1 can eliminate them completely, which acts as a form of feature selection in linear models. For a task with 100 candidate features, L1 might keep only a small subset. That subset is a modelling result, not proof that the others are irrelevant: among correlated features, L1 can select one fairly arbitrarily.

Advantages

  • Automatic feature selection
  • Sparse models (faster only if zero weights are actually pruned)
  • More interpretable

Disadvantages

  • Can remove too many features
  • Harder to optimize mathematically

L2 Regularization

L2 regularization adds a penalty for the square of weights:

loss = standard_loss + (λ / 2) × Σ(weights²)

This shrinks each weight in proportion to its size, without generally setting any to zero. It encourages smaller weights across the board and tends to spread importance across correlated features. Here the half-coefficient convention gives gradient λ × weights; texts that omit the half use a differently scaled λ.

Weight Decay

For plain SGD, an L2 penalty is equivalent to multiplying weights by a constant slightly below 1 at each step (with λ rescaled by the learning rate), which is why the two terms are often used interchangeably. They are not equivalent with adaptive optimizers such as Adam: adding the L2 gradient lets it be rescaled by the adaptive denominator. AdamW decouples weight decay for this reason (Loshchilov & Hutter, 2019).

Advantages

  • Smooth weight reduction
  • Often a good default in neural networks
  • Easier to optimize
  • Most popular choice

Disadvantages

  • Doesn't eliminate features
  • Less interpretable than L1
L2 is the default regularization choice for most problems. Use L1 when you need to understand which features matter most.

Dropout

Dropout randomly disables neurons during training. Each neuron is dropped with probability p, sampled independently for each training example. p must be in [0, 1): p = 1 would drop every unit. A common choice is around 0.5 for fully connected hidden layers, with lower values for inputs and convolutional layers.

keep_prob = 1 - p  # 0 ≤ p < 1
output = output × random_mask / keep_prob  # mask is 1 with probability keep_prob
(During testing, all neurons are active and no mask or scaling is applied)

Why It Works

Dropout forces the network to learn with different combinations of neurons. No single neuron can dominate. Each neuron must learn to work with various teammates. This prevents co-adaptation—where neurons specialize in detecting training-specific patterns that don't generalize.

Dividing by keep_prob keeps each unit's expected output the same between training and testing. This is the "inverted dropout" used by modern libraries. The original paper instead scales weights by the keep probability at test time; the two are equivalent in expectation. You can think of dropout as approximately training an ensemble of weight-sharing sub-networks, with the full network acting as an approximate average at test time.

Advantages

  • Very effective at preventing overfitting
  • Simple to implement
  • Ensemble-like behavior
  • Works well with deep networks

Disadvantages

  • Requires more training iterations
  • Slower convergence
  • Must disable during testing
Constraint geometry
Subnetwork

The setup is illustrative — a synthetic two-parameter quadratic loss, because constraint geometry is only visible in two dimensions. The solutions are computed. The L2 point is the closed-form ridge estimate; the L1 point comes from proximal gradient descent with soft-thresholding, the same operator that produces sparsity in real training. The geometry uses loss + λ‖w‖₁ for L1 and loss + (λ/2)‖w‖₂² for L2. The circle and diamond show the budgets corresponding to the computed solutions; they are not equal-radius comparisons. L1 can produce exact zeros. Ridge generally shrinks coefficients without enforcing sparsity, though individual coefficients can cross zero with correlated features. The separate network panel is a schematic shrinkage/masking illustration, not a trained neural network. Its dropout probability p is independent of λ; activation scaling is explained above.

Data Augmentation

Data augmentation artificially creates new training examples by applying transformations to existing ones.

Examples

For images: rotate, flip, crop, change brightness, add noise, adjust contrast. For text: paraphrase, reorder sentences, synonym replacement. The key is that transformations shouldn't change the label.

Why It Works

With 1000 images, you have 1000 independent examples. Augmented versions add variety, but they are correlated with their originals, so they are not equivalent to collecting more real data. The network sees more variation, learns tolerance to the applied changes, and finds it harder to memorize specific images.

Advantages

  • Increases effective data size
  • Forces learning of general patterns
  • Handles real-world variation
  • No additional data collection

Disadvantages

  • Requires domain knowledge
  • Computationally expensive
  • Can be ineffective with poor augmentations
Data augmentation is usually worth trying when the transformations preserve the label. A transform that changes the correct answer, such as flipping a digit or a road sign, can harm the model.

Early Stopping

Early stopping monitors validation performance and stops training when it starts declining, even if training performance is still improving.

The Process

During training, periodically evaluate on a validation set. If validation loss increases (or validation accuracy decreases), stop. Save the weights from when validation was best.

best_validation_loss = infinity
patience_counter = 0

for each epoch:
training_loss = train(network)
validation_loss = validate(network)

if validation_loss < best_validation_loss:
best_validation_loss = validation_loss
patience_counter = 0
save_weights(network)
else:
patience_counter += 1

if patience_counter > patience_threshold:
stop_training()

The "patience" parameter allows temporary increases in validation loss, so you don't stop on the first bad epoch. A suitable value depends on the training length and how noisy validation is.

Advantages

  • Simple and effective
  • Few hyperparameters (patience, monitored metric)
  • Selects the best checkpoint seen on validation data
  • Saves training time

Disadvantages

  • Requires a validation set
  • Depends on validation set quality
  • Reduces training data (some goes to validation)

Combining Techniques

These techniques work together. Don't think of them as either/or choices.

For Simple Models

L2 regularization + Early stopping + Data augmentation. Simple, effective, minimal overhead.

For Deep Networks

L2 regularization or weight decay + Dropout (where appropriate, around 0.5 in fully connected layers) + Data augmentation + Early stopping + Batch normalization. This is a common starting set, not a recipe: each addition should earn its place on validation data.

For Limited Data

Heavy data augmentation + L2 regularization + Dropout + Early stopping. When data is scarce, augmentation becomes critical.

For Interpretability

L1 regularization (automatic feature selection) + Early stopping. When understanding which features matter is important, L1 helps by zeroing out irrelevant ones.

Early stopping with a validation set is simple and often useful, but it has costs: it holds out data, repeated evaluation takes time, and it can stop too soon if validation is noisy or if the learning-rate schedule has not finished.

Comparison Table

Technique Type Typical impact (rule of thumb) Complexity
L1 Regularization Weight penalty Task dependent Low
L2 Regularization Weight penalty Task dependent Low
Dropout Architectural Task dependent Low
Data Augmentation Data level Task dependent Medium
Early Stopping Training process Task dependent Very Low

Conclusion

Regularization helps models generalize beyond the data they were trained on. Without some form of it, flexible networks can memorize noise.

The five techniques covered here address overfitting from different angles. L1 and L2 constrain weights. Dropout approximates an ensemble. Data augmentation provides varied training examples. Early stopping selects a good point in training using validation data.

A practical approach: start with L2 or weight decay plus early stopping, add dropout where the architecture benefits, and use label-preserving data augmentation. Fine-tune from there based on what you observe in your specific problem.