Ultralytics YOLO27: Join the waitlist.
License YOLO
Back to Ultralytics Glossary

Weight Decay

Weight decay is a regularization technique that shrinks model weights toward zero, helping neural networks reduce overfitting and generalize to new data.

Weight decay is a regularization technique that pulls a model's learned weights toward zero during training. This repeated shrinkage discourages unnecessarily large parameter values and helps a neural network generalize to unseen examples. It is especially useful when a model has enough capacity to memorize noise in its training data.

Geoffrey Hinton introduced weight decay in his 1986 paper Learning Distributed Representations of Concepts. In its family-tree experiment, he reduced weight magnitudes by 0.2% after each update to make learned connections easier to interpret. Weight decay subsequently became widely used to control overfitting.

How Weight Decay Works#

During gradient descent, the optimizer changes parameters using gradients: derivatives that describe how each parameter affects the loss function. Weight decay adds a pull toward zero. Under the learning-rate-scaled convention used by many optimizers, a plain SGD update without momentum is new weight = (1 - learning rate × decay) × old weight - learning rate × gradient. This combines learning from prediction errors with proportional shrinkage.

The decay coefficient controls the strength of that pull. Zero disables it; larger positive values increase shrinkage at a fixed learning rate. The task gradient can still increase a weight when that improves predictions. Learning-rate decay progressively reduces the optimizer's step size; weight decay acts on parameter values.

Weight Decay vs. L2 Regularization#

L2 regularization adds a scaled sum of squared weights to the training objective. With plain stochastic gradient descent, differentiating this penalty produces the same proportional shrinkage, with appropriately matched coefficients. In linear regression, fitting with this squared-weight penalty is called ridge regression.

With the Adam optimizer, adding an L2 penalty sends the penalty gradient through Adam's moving averages and parameter-specific scaling. AdamW instead applies shrinkage separately from that adaptive gradient calculation. Ilya Loshchilov and Frank Hutter introduced AdamW in their 2017 paper Decoupled Weight Decay Regularization, published at ICLR 2019. This separation makes decay strength independent of Adam's gradient-history scaling.

Choosing the Decay Strength#

Weight decay is a hyperparameter, selected using validation data. Too little leaves overfitting insufficiently constrained. Excessive decay causes underfitting: the model fails to fit useful patterns, producing high errors on training and validation examples. Comparing validation scores across decay settings reveals the useful strength for a particular model and dataset.

The accumulated effect also depends on the learning-rate schedule and number of optimizer updates. Changing batch size or training epochs can change the number of decay applications; gradient accumulation combines multiple batches before an update. A dropout layer regularizes by randomly zeroing activations, while early stopping limits training duration.

Ultralytics YOLO exposes a weight_decay training setting, with a default of 0.0005, to control regularization when training models such as YOLO26. Its optimizer parameter groups exclude biases and normalization-layer parameters from weight decay.

Weight Decay in PyTorch#

The torch.optim.AdamW optimizer provides decoupled weight decay in PyTorch. Install the package with pip install torch; this example creates a zero task gradient so the printed update isolates weight decay:

import torch

weight = torch.nn.Parameter(torch.tensor([2.0]))
optimizer = torch.optim.AdamW([weight], lr=0.1, weight_decay=0.1)
before = weight.item()

# Create a zero gradient while keeping the parameter in the computation.
optimizer.zero_grad()
loss = (weight * 0).sum()
loss.backward()
optimizer.step()

print(f"Weight: {before:.2f} -> {weight.item():.2f}")

The output is Weight: 2.00 -> 1.98: one update multiplies the weight by 1 - 0.1 × 0.1, or 0.99, reducing its magnitude by 1%.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Turn drone and aerial imagery into real-time insights for agriculture, aquaculture, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Apply computer vision in aerospace with Ultralytics YOLO for aircraft inspection, runway FOD detection, airport ground operations, and composite defect detection.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning