Weight decay e regolarizzazione L2 a confronto
La sottile differenza che conta
Weight decay e regolarizzazione L2 a confronto è una lezione Deep Learning Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Deep Learning Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Deep Learning Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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. 🪶
Domande Frequenti
La lezione «Weight decay e regolarizzazione L2 a confronto» è gratuita?
Sì — il testo completo di «Weight decay e regolarizzazione L2 a confronto» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Deep Learning Academy, passa a CoddyKit PRO. Il corso Deep Learning Academy include 4 lezioni in totale.
Cosa imparerò in «Weight decay e regolarizzazione L2 a confronto»?
La sottile differenza che conta Eserciti Deep Learning Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Deep Learning Academy?
Non è richiesta alcuna esperienza precedente. Deep Learning Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.
Quanto tempo richiede la lezione «Weight decay e regolarizzazione L2 a confronto»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Deep Learning Academy?
Sì. Ogni lezione Deep Learning Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- SGD con momentum
- Adam e AdamW spiegati
- Weight decay e regolarizzazione L2 a confronto
- Scheduler del learning rate e warmup