在 PyTorch 中添加 L1/L2 正则化? [英] Adding L1/L2 regularization in PyTorch?
本文介绍了在 PyTorch 中添加 L1/L2 正则化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么办法可以在 PyTorch 中添加简单的 L1/L2 正则化?我们可以通过简单地将 data_loss
与 reg_loss
相加来计算正则化损失,但是有没有任何明确的方法,来自 PyTorch 库的任何支持可以更轻松地做到这一点,而无需手动进行?
Is there any way, I can add simple L1/L2 regularization in PyTorch? We can probably compute the regularized loss by simply adding the data_loss
with the reg_loss
but is there any explicit way, any support from PyTorch library to do it more easily without doing it manually?
推荐答案
这在 PyTorch 的文档.您可以使用 weight_decay
参数将 L2 损失添加到优化函数中.
This is presented in the documentation for PyTorch. You can add L2 loss using the weight_decay
parameter to the Optimization function.
这篇关于在 PyTorch 中添加 L1/L2 正则化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文