Pytorch中常用Loss函数总结

Pytorch中常用Loss函数总结


  • torch.nn.MSELoss():常用于回归模型
  • torch.nn.CrossEntropyLoss():常用于分类模型
  • smooth L1损失
一、MSE(均方差)

MSE loss = 1/n(x-y)***

loss_func2 = torch.nn.MSELoss()
target = torch.tensor([[112.2396,  82.2253],
       				  [115.4184,  76.4030]],dtype=torch.float32)
predict = torch.tensor([[113,  80],
                        [113,  80]],dtype=torch.float32
                        
loss = loss_func2(predict, target)
loss_compare = torch.mul((predict-target),(predict-target)).mean()
print loss_compare
print loss

output:
tensor(6.0792)
tensor(6.0792)

二、CrossEntropy(交叉熵)

关于信息熵的概念可以参考这篇文章信息熵,交叉熵及相对熵代码实现过程理解在这里插入图片描述
在pytorch中交叉熵的计算公式如下
在这里插入图片描述

# -*- coding: utf-8 -*-
import torch
import torch.nn.functional as F
# predict为网络输出预测的值
predict = torch.tensor([[1.,  0],
                       [0 ,0.5]])
# label为分类标签值
label = torch.tensor([0,1])

#通过pytorch自带函数包计算的交叉熵
loss_fun1 = torch.nn.CrossEntropyLoss()
loss1 = loss_fun1(predict,label)
print 'CrossEntropyLoss()的计算结果',loss1
######################################
#手算多维张量的交叉熵
### 通过label计算实分布 p(x)
def one_hot(label,n_class):
    one_hot_label = torch.zeros((label.shape[0], n_class))
    one_hot_label[torch.arange(0, label.shape[0]), label] = 1
    return one_hot_label
p = one_hot(label,2)

### 通过predict计算非真实分布(预测分布) q(x)
loss_fun2 =torch.nn.Softmax()
q = loss_fun2(predict)

### 根据交叉熵公式,L = -(1/m)sum(p(x)*log(q(x)))
loss2 = torch.sum(-(p*torch.log(q)))/label.size(0)

### 或者直接利用log_softmax()
loss_fun3 =torch.nn.LogSoftmax()
q1 = loss_fun3(predict)
loss3 = torch.sum(-(p*q1))/label.size(0)

print '手算的结果',loss2,loss3

结果为:

CrossEntropyLoss()的计算结果 tensor(0.3937)
手算的结果 tensor(0.3937) tensor(0.3937)
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值