【pytorch】对照原理,自己实现pytorch自带的损失函数:L1Loss,L2Loss,BCELoss,BCEWithLogitsLoss,NLLLoss,CrossEntropyLoss
nn.L1Loss绝对值损失#1批4个求损失和defL1Loss(y,yhead):returntorch.mean(torch.abs(y-yhead))y=torch.rand(4,3)yhead=torch.rand(4,3)print(L1Loss(y,yhead))print(nn.L1Loss(reduction='mean')(y,yhead))输出:tensor(0.4083)te