Pytorch如何自定义Loss

匿名 (未验证) 提交于 2019-12-03 00:21:02
将Loss视作单独的层,在forward函数里写明loss的计算方式,无需定义backward

class MyLoss(nn.Module):     def __init__(self):         super(MyLoss, self).__init__()         print '1'     def forward(self, pred, truth):         return  torch.mean(torch.mean((pred-truth)**2,1),0)

super(MyLoss, self).__init__()

AttributeError: 'MyLoss' object has no attribute '_forward_pre_hooks' 错误。

要打印loss可以用

loss.data.cpu().numpy()[0]
访问
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!