pytorch 的损失函数

MSELoss()

在这里插入图片描述

调用方法

import torch.nn.functional as F
a=torch.tensor([[0,0.5,2,3],[0,1,2,3],[0,1,2,3]],dtype=torch.float32)
b=torch.tensor([[1,0.3,3,4],[0,1.2,2.2,3],[0,1,2,5]],dtype=torch.float32) print(a.shape,b.shape)
c=F.mse_loss(a, b, reduction='sum')
d=0.5*F.mse_loss(a, b, reduction='mean')#通常加上0.5 
print(c)
print(d)

结果:
torch.Size([3, 4]) torch.Size([3, 4])
tensor(7.1200)
tensor(0.2967)

Smooth L1 Loss(Huber)

公式

在这里插入图片描述

调用方法

import torch.nn.functional as F
a=torch.tensor([[0,0.5,2,3],[0,1,2,3],[0,1,2,3]],dtype=torch.float32)
b=torch.tensor([[1,0.3,3,4],[0,1.2,2.2,3],[0,1,2,5]],dtype=torch.float32) print(a.shape,b.shape)
c=F.smooth_l1_loss(a, b, reduction='sum')
d=F.smooth_l1_loss(a, b, reduction='mean')
print(c)
print(d)

结果:
torch.Size([3, 4]) torch.Size([3, 4])
tensor(3.0600)
tensor(0.2550)

torch.nn.logSoftmax()

该API的主要是对预测结果进行计算转换,先求softmax在求log
在这里插入图片描述
代码:

pred=torch.tensor([[1.0,2.0,3.0],[0.5,0.5,0.5]],dtype=torch.float32)
pred_softmax=torch.nn.functional.softmax(pred,dim=1)
pred_logsoftmax=np.log(pred_softmax.data)
 pred_logsoftmax1=torch.nn.functional.log_softmax(pred,dim=1)

输出为:

#原tensor
tensor([[1.0000, 2.0000, 3.0000],
        [0.5000, 0.5000, 0.5000]])
#原tensor 进行softmax
tensor([[0.0900, 0.2447, 0.6652],
        [0.3333, 0.3333, 0.3333]])
tensor([[-2.4076, -1.4076, -0.4076],
        [-1.0986, -1.0986, -1.0986]])
tensor([[-2.4076, -1.4076, -0.4076],
        [-1.0986, -1.0986, -1.0986]])

torch.nn.NLLLoss()

用于多分类的负对数似然损失函数(negative log likelihood loss)
将每个label的值作为pred每行取值的索引,取相反数
代码:

pred=torch.tensor([[1.0,2.0,3.0],[0.5,0.5,0.5]],dtype=torch.float32)
    pred_logsoftmax=torch.nn.functional.log_softmax(pred,dim=1)
    label=torch.LongTensor([0,1])
    #用logsoftmax的结果进行nll_loss,默认是取平均值
    loss=torch.nn.functional.nll_loss(pred_logsoftmax,label)
    loss1=torch.nn.functional.cross_entropy(pred,label)
    print(pred)
    print(label)
    print(pred_logsoftmax)
    print(loss)
    print(loss1)
tensor([[1.0000, 2.0000, 3.0000],
        [0.5000, 0.5000, 0.5000]])
tensor([0, 1])
tensor([[-2.4076, -1.4076, -0.4076], 取该行第0个值的相反数
        [-1.0986, -1.0986, -1.0986]])    取该行第1个值的相反数
tensor(1.7531)    计算过程=[-(-2.4076)+(-(-1.0986))]/2=1.7531
tensor(1.7531)

修改

loss=torch.nn.functional.nll_loss(pred_logsoftmax,label,reduction="sum")
print(loss) tensor(3.5062)

torch.nn.CrossEntropyLoss()

就是torch.nn.log_softmax()与torch.nn.nll_loss()的结合体
代码:

#pre.shape=3,2 batch_size=3 cls_num=2
#label.shape=3 每个元素代表每隔样本的分类标签
pre=torch.tensor([[-1.0,1.0],[2.0,-2.0],[-3.0,3.0]])#定义一个预测tensor 一定要.0否则会报错
label=torch.LongTensor([1,0,1])
# loss=F.cross_entropy(a,b) 与torch.nn.CrossEntropyLoss()效果一样,不需要先定义
lossfn=torch.nn.CrossEntropyLoss()
loss=lossfn(pre,label)
print("loss ",loss)#loss  tensor(0.0492)
a=torch.tensor([[-1.0,1.0],[2.0,-2.0],[-3.0,3.0]])#一定要
b=torch.LongTensor([1,0,1])
loss=F.cross_entropy(a,b)

会报错

RuntimeError: "log_softmax_lastdim_kernel_impl" not implemented for 'torch.LongTensor'

BCEWithLogitsLoss与BCELoss

转载地址

如果觉得本文对你有用的话请转发/点赞/么么哒

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值