【pytorch】对照原理,自己实现pytorch自带的损失函数:L1Loss,L2Loss,BCELoss,BCEWithLogitsLoss,NLLLoss,CrossEntropyLoss

89 篇文章 11 订阅
65 篇文章 3 订阅

nn.L1Loss绝对值损失
#1批4个求损失和

def L1Loss(y,yhead):
    return torch.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)
tensor(0.4083)

#nn.MSELoss均方损失

def L2Loss(y,yhead):
    return torch.mean((y-yhead)**2)
y=torch.rand(4,3)
yhead=torch.rand(4,3)
print(L2Loss(y,yhead))
print(nn.MSELoss(reduction='mean')(y,yhead))

输出:

tensor([0.1401])
tensor(0.1401)

#nn.BCELoss二分类交叉熵损失

def BCELoss(y,yhead):
    return torch.mean(-(torch.log(y)*yhead+torch.log((1-y))*(1-yhead)))
y=torch.rand(4,3)
yhead=torch.rand(4,3)
print(BCELoss(y,yhead))
print(nn.BCELoss(reduction='mean')(y,yhead))

输出:

tensor([2.0282])
tensor(2.0282)

#nn.BCELoss二分类交叉熵损失

def BCELossWithSigmod(y,yhead):
    return torch.mean(-(torch.log(torch.sigmoid(y))*yhead+torch.log(1-torch.sigmoid(y))*(1-yhead)))
y=torch.rand(4,3)
yhead=torch.rand(4,3)
print(BCELossWithSigmod(y,yhead))
print(nn.BCEWithLogitsLoss(reduction='mean')(y,yhead))

输出:

tensor([0.7641])
tensor(0.7641)

‘’‘最大似然 / log似然代价函数,X是log_softmax()的输出,label是对应的标签位置
适合最后一层是log_softmax().其并不计算对数,而是假定输入已经计算过了对数。这里假定
目标经过了onehot编码,也假设输入y为onehot编码格式。目前中其余元素为0,相乘为0,
只取labelIndex处的至进行相乘,默认为1.最后加负号输出结果。
‘’’

def NLLLoss(y,labelIndex):
    loss=0
    for i,index in enumerate(labelIndex):
        loss+=y[i][index]
    return -loss/y.size(0)
y=torch.rand(2,3)
labelIndex=torch.LongTensor([1,2])
print(NLLLoss(y,labelIndex))
print(nn.NLLLoss(reduction='mean')(y,labelIndex))

输出:

tensor([[0.5495, 0.5472, 0.2328],
        [0.6703, 0.0252, 0.5524]])
tensor(-0.5498)
tensor(-0.5498)

log_softmax:
LogSoftmax ( x i ) = log ⁡ ( exp ⁡ ( x i ) ∑ j exp ⁡ ( x j ) ) \text{LogSoftmax}(x_{i}) = \log\left(\frac{\exp(x_i) }{ \sum_j \exp(x_j)} \right) LogSoftmax(xi)=log(jexp(xj)exp(xi))

'''最大似然 / log似然代价函数,X是log_softmax()的输出,label是对应的标签位置
适合最后一层是log_softmax().其并不计算对数,而是假定输入已经计算过了对数。这里假定
目标经过了onehot编码,也假设输入y为onehot编码格式。目前中其余元素为0,相乘为0,
只取labelIndex处的至进行相乘,默认为1.最后加负号输出结果。
'''

def NLLLoss(y,labelIndex):
    loss=0
    for i,index in enumerate(labelIndex):
        loss+=y[i][index]
    return -loss/y.size(0)

loss = nn.CrossEntropyLoss()
input = torch.randn(3, 5, requires_grad=True)
target = torch.empty(3, dtype=torch.long).random_(5)
output = loss(input, target)
print(output)
def myCrossEntropyLoss(y,yhead):
    return NLLLoss(torch.nn.LogSoftmax(dim=-1)(y),yhead)
output = myCrossEntropyLoss(input, target)
print(output)

输出:

tensor(1.9680, grad_fn=<NllLossBackward0>)
tensor(1.9680, grad_fn=<DivBackward0>)

CrossEntropyLoss: log_softmax+NLLLoss
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

颢师傅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值