loss表示差异
import torch from torch.nn import L1Loss inputs=torch.tensor([1,2,3],dtype=torch.float32) targets=torch.tensor([1,2,5],dtype=torch.float32)//输入inputs targets的数据 inputs=torch.reshape(inputs,(1,1,1,3)) targets=torch.reshape(targets,(1,1,1,3))//重构 loss=L1Loss() result=loss(inputs,targets)//loss print(result)
运行结果:
tensor(0.6667)
进程已结束,退出代码为 0
如果改为
loss=L1Loss(reduction='sum') result=loss(inputs,targets)
运行结果就为2
此外还有MSE与交叉熵。
交叉熵概念了解。
当调用loss,可以调用它的反向传播。