一文详解L1-loss和L2-loss(pytorch版本)

import torch.nn as nn
import torch
pixelwise_loss = nn.L1Loss(reduction='mean')  #平均绝对误差,L1-损失
loss2=nn.MSELoss()  #L2-损失
x1=torch.zeros([2,2])
x2=torch.ones([2,2])
x3=torch.tensor([[0.125,0.5],[0.5,0.5]])
y=pixelwise_loss(x3,x2)
print('L1Loss:',y.item())
print(((1-0.125)+0.5*3)/4)
y1=loss2(x3,x2)
print('L2Loss:',y1.item())
print(((1-0.125)**2+(0.5**2)*3)/4)

你可能感兴趣的:(python)