模型加权平方和是一种防止深度学习模型过拟合的正则化方法。
目前,我在pytorch
中实现了我的模型的损失函数:
lossfunc = nn.NLLLoss(ignore_index=0)
然后我计算训练中的损失如下:
...
loss = lossfunc(out_perm, dec_pp[:,1:])
我想成为这样的人:
loss = lossfunc(out_perm, dec_pp[:,1:])+sum_square_wights(enc)+sum_square_wights(dec)
但不知道如何实现sum_square_wights
函数。任何帮助都将不胜感激。你知道吗
我相信你指的是L2正则化。如果确实如此,L2正则化已经添加到torch(SGD、Adam等)中的optimizer,您可以使用优化器参数中
weight_decay
的非零值来控制它。你知道吗就L1正则化而言,类似这样的方法应该可以完成这项工作:
相关问题 更多 >
编程相关推荐