Weight Decay vs. L2-Regularisierung
Der subtile Unterschied, auf den es ankommt.
Weight Decay vs. L2-Regularisierung ist eine kostenlose Deep Learning Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Deep Learning Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Deep Learning Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Keep Weights Small
Big weights often mean an overfit model. Both weight decay and L2 regularization push weights toward zero so the network stays simpler.
L2 Adds to the Loss
L2 regularization adds a penalty term, the sum of squared weights, straight into the loss. Minimizing loss then also means shrinking the weights.
loss = data_loss + lam * (w ** 2).sum()Decay Shrinks Directly
Weight decay skips the loss and instead multiplies each weight by a number slightly under one every step. It shrinks weights before the gradient update.
w = w - lr * grad - lr * wd * wSame Thing for SGD
With plain SGD, the two are mathematically identical. The L2 penalty's gradient is exactly the decay term, so it makes no practical difference.
Adam Breaks the Tie
The catch appears with Adam. Its per-weight scaling divides the L2 gradient unevenly, so L2 and true weight decay stop being equal.
Why It Distorts
Adam shrinks weights with large gradients less and small ones more. Folded-in L2 inherits that bias, weakening the regularization where you need it.
Decoupling Is the Fix
AdamW applies decoupled weight decay, shrinking every weight by the same fraction independent of its gradient. That restores honest regularization.
Set It in PyTorch
The weight_decay argument controls the strength. In AdamW it is true decoupled decay, the behavior you usually want.
opt = torch.optim.AdamW(model.parameters(), lr=1e-3, weight_decay=0.01)Pick a Strength
Values like 0.01 or 0.0001 are typical. Too much decay underfits; too little lets weights grow and overfit. Tune it like any hyperparameter.
Spare the Biases
It is common to skip decay on bias and norm parameters. Shrinking them rarely helps and can quietly hurt how the model trains.
The Takeaway
With SGD, reach for either name freely. With adaptive optimizers, prefer AdamW so your weight decay actually behaves as designed.
Quick Check
Test when the two truly diverge.
Recap
L2 adds a penalty to the loss; weight decay shrinks weights directly. They match under SGD but split under Adam, which is why AdamW decouples decay. 🪶
Häufig gestellte Fragen
Ist die Lektion „Weight Decay vs. L2-Regularisierung“ kostenlos?
Ja — der vollständige Text von „Weight Decay vs. L2-Regularisierung“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Deep Learning Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Deep Learning Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Weight Decay vs. L2-Regularisierung“?
Der subtile Unterschied, auf den es ankommt. Du übst Deep Learning Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Deep Learning Academy zu starten?
Keine Vorkenntnisse erforderlich. Deep Learning Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „Weight Decay vs. L2-Regularisierung“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Deep Learning Academy-Lektion Code schreiben und ausführen?
Ja. Jede Deep Learning Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- SGD mit Momentum
- Adam und AdamW erklärt
- Weight Decay vs. L2-Regularisierung
- Lernratenpläne und Warmup