【发布时间】:2019-08-12 07:54:30
【问题描述】:
已知添加模型权重的平方和是一种正则化方法,可防止深度学习模型过度拟合。
目前我的模型有这个损失函数,它在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 功能。任何帮助将不胜感激。
【问题讨论】:
-
嗨,如果你有一个矩阵中的所有权重,那么你可以编写一个自定义函数来计算权重的平方和。
标签: python deep-learning pytorch