Regularization adds a penalty term
where
Effect on update rule:
The factor
Effect: L1 drives small weights exactly to zero, producing a sparse model. This is useful for feature selection — irrelevant connections are pruned automatically.
| Property | L1 | L2 |
|---|---|---|
| Penalty shape | Diamond (corners at axes) | Sphere |
| Sparsity | Promotes exact zeros | Shrinks toward zero but rarely reaches it |
| Gradient at |
Undefined (sub-gradient) | Zero |
| Best for | Feature selection, sparse models | General-purpose weight control |
The regularized loss can be viewed as constrained optimization:
where
| Operation | Time | Space |
|---|---|---|
Regularization adds negligible computational cost — one pass over the parameter vector per training iteration.
Scenario: 3 parameters,
L2 Regularization:
| Step | Computation | Result |
|---|---|---|
| Penalty | ||
| Gradient | Added to |
|
| Contribution to loss |
L1 Regularization:
| Step | Computation | Result |
|---|---|---|
| Penalty | ||
| Gradient | Added to |
|
| Contribution to loss |
After several L1 updates (
-
$\lambda$ too large. The model underfits — weights are driven so close to zero that the network cannot represent the function. Cross-validate$\lambda$ . -
$\lambda$ too small. Negligible effect; overfitting persists. -
L1 non-differentiability. At
$\theta_i = 0$ , the L1 gradient is undefined. Use sub-gradient$\mathrm{sign}(0) = 0$ or proximal operators for exact handling. - Regularizing biases. Conventionally, bias parameters are excluded from regularization because they do not contribute to model complexity. This library regularizes all parameters in the flat vector — be aware of this if bias control matters.
-
Fixed-point precision. The regularization term can be much smaller than the main loss when
$\lambda$ is small. In low-precision fixed-point, it may round to zero. Scale$\lambda$ or use a wider accumulator.
| Variant | Key Difference |
|---|---|
| Elastic Net |
|
| Dropout | Randomly zeroes activations during training; implicit ensemble regularization |
| Early stopping | Halts training before overfitting; regularization without modifying the loss |
| Data augmentation | Expands the training set with transformed copies; reduces overfitting by increasing data diversity |
| Spectral normalization | Constrains the spectral norm of weight matrices; stabilizes GAN training |
| Weight clipping | Hard constraint: |
- Preventing overfitting — The primary use case for any neural network trained on limited data (common in embedded scenarios).
- Feature selection — L1 regularization identifies and prunes irrelevant input connections.
- Model compression — Sparse models (via L1) require less storage and computation for deployment on MCUs.
- Transfer learning — L2 regularization keeps fine-tuned weights close to pre-trained values.
graph TD
Reg["Regularization"]
Loss["Loss Functions"]
Opt["Optimizer"]
Model["Model"]
LR["Linear Regression"]
Reg -->|"λ Ω(θ) added to ℒ"| Loss
Loss --> Opt
Opt --> Model
Reg -.->|"L2 + MSE = Ridge regression"| LR
| Component | Relationship |
|---|---|
| Loss Functions | Regularization is a penalty added to the loss: |
| Optimizer | Receives the combined gradient |
| Linear Regression | L2-regularized MSE with a linear model is Ridge regression; L1 is Lasso |
- Goodfellow, I., Bengio, Y., and Courville, A., Deep Learning, MIT Press, 2016 — Chapter 7 (regularization).
- Tibshirani, R., "Regression shrinkage and selection via the lasso", JRSS-B, 58(1), 1996.
- Krogh, A. and Hertz, J.A., "A simple weight decay can improve generalization", NeurIPS, 1991.