Search code examples
pythonmachine-learningpytorchloss-functionregularized

L1/L2 regularization in PyTorch


How do I add L1/L2 regularization in PyTorch without manually computing it?


Solution

  • See the documentation. Add a weight_decay parameter to the optimizer for L2 regularization.