NLLLOSS & CrossEntropyLoss

今天在看论文的时候,看到了NLLLOSS函数,嗯?这是个啥,然后就查了查,原来是跟CrossEntropyLoss一样的,这里整理一下,方便以后查阅。

NLLLOSS

官方API解释:
https://pytorch.org/docs/stable/generated/torch.nn.NLLLoss.html?highlight=nllloss#torch.nn.NLLLoss

在这里插入图片描述
在这里插入图片描述

The negative log likelihood loss. It is useful to train a classification problem with C classes.

在这里插入图片描述

The unreduced (i.e. with reduction set to ‘none’) loss can be described as:

在这里插入图片描述
其中 x x x是输入,也就是上文的“log-probabilities” y y y是标签,这里的输入的标签并不是one-hot,而是一个索引; w w w是权重,就如上面所说的,是针对不平衡数据集的; N N N是batch size。

对公式中 x n , y n x_{n, y_n} xn,yn的理解:
输入 x x x的尺寸是 ( N , C ) (N, C) (N,C),则下标 n n n代表一个batch里面的第 n n n个样本;下标 y n y_n yn代表的 target的第 n n n个数据,也就是在一个batch中的第 n n n个样本的标签,作为 x x x的下标取的是第 y n y_n yn列的数据,也就是第 y n y_n yn类的预测概率。

If reduction is not ‘none’ (default ‘mean’), then

在这里插入图片描述

m = nn.LogSoftmax(dim=1)
loss = nn.NLLLoss()
# input is of size N x C = 3 x 5
input = torch.randn(3, 5, requires_grad=True)
# each element in target has to have 0 <= value < C
target = torch.tensor([1, 0, 4]) # 生成三个样本的标签,标签值小于类别数
output = loss(m(input), target)
output.backward()

# 2D loss example (used, for example, with image inputs)
N, C = 5, 4
loss = nn.NLLLoss()
# input is of size N x C x height x width
data = torch.randn(N, 16, 10, 10)
conv = nn.Conv2d(16, C, (3, 3)) # output:[N, 4, 8, 8]
m = nn.LogSoftmax(dim=1)
# each element in target has to have 0 <= value < C
target = torch.empty(N, 8, 8, dtype=torch.long).random_(0, C) # [N, 8, 8]
output = loss(m(conv(data)), target)
output.backward()

CrossEntropyLoss

官方API解释:
https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html?highlight=crossentropyloss#torch.nn.CrossEntropyLoss
在这里插入图片描述

在这里插入图片描述

This criterion computes the cross entropy loss between input and target.

在这里插入图片描述

这一部分的介绍跟NLLLOSS是类似的。下面的介绍就跟NLLLOSS有区别了。针对输入的 target 得不同,分了两种情况介绍。

The target that this criterion expects should contain either:

在这里插入图片描述

Note that this case is equivalent to the combination of LogSoftmax and NLLLoss.

这种情况输入的target是个索引,跟NLLLOSS的target是一样的,所以说此时的CrossEntropyLoss相当于Softmax+Log+NLLLoss。

在这里插入图片描述
这种情况输入的target是预测值,也就是经过softmax之后的值,这里就跟NLLLOSS不一样了。

# Example of target with class indices
loss = nn.CrossEntropyLoss()
input = torch.randn(3, 5, requires_grad=True)
target = torch.empty(3, dtype=torch.long).random_(5)
output = loss(input, target)
output.backward()

# Example of target with class probabilities
input = torch.randn(3, 5, requires_grad=True)
target = torch.randn(3, 5).softmax(dim=1)
output = loss(input, target)
output.backward()

LogSoftmax

官方API解释:
https://pytorch.org/docs/stable/generated/torch.nn.LogSoftmax.html#torch.nn.LogSoftmax

在这里插入图片描述
Applies the log ⁡ ( Softmax ( x ) ) \log(\text{Softmax}(x)) log(Softmax(x)) function to an n-dimensional input Tensor. The LogSoftmax formulation can be simplified as:

在这里插入图片描述

m = nn.LogSoftmax()
input = torch.randn(2, 3)
output = m(input)

Softmax

官方API解释:
https://pytorch.org/docs/stable/generated/torch.nn.Softmax.html?highlight=softmax#torch.nn.Softmax

在这里插入图片描述

Applies the Softmax function to an n-dimensional input Tensor rescaling them so that the elements of the n-dimensional output Tensor lie in the range [0,1] and sum to 1.

Softmax is defined as:
在这里插入图片描述

m = nn.Softmax(dim=1)
input = torch.randn(2, 3)
output = m(input)

Sigmoid

官方API解释:
https://pytorch.org/docs/stable/generated/torch.nn.Sigmoid.html?highlight=sigmoid#torch.nn.Sigmoid
在这里插入图片描述

Applies the element-wise function:

在这里插入图片描述

在这里插入图片描述

m = nn.Sigmoid()
input = torch.randn(2)
output = m(input)

再谈CrossEntropyLoss

交叉熵(cross entropy)是深度学习中常用的一个概念,一般用来求目标与预测值之间的差距

线性回归中的损失函数MSN

在线性回归问题中,常常使用MSE(Mean Squared Error)作为loss函数,比如:

在这里插入图片描述
这里的m表示m个样本的,loss为m个样本的loss均值。MSE在线性回归问题中比较好用,那么在逻辑分类问题中还是如此么?

为什么线性回归任务中不用MSN方法呢?

  • 主要原因是在分类问题中,使用sigmoid/softmx得到概率,配合MSE损失函数时,采用梯度下降法进行学习时,会出现模型一开始训练时(这时候的概率往往很低),学习速率非常慢的情况
  • 因为回归问题要求拟合实际的值,通过MSE衡量预测值和实际值之间的误差,可以通过梯度下降的方法来优化。而不像分类问题,需要一系列的激活函数(sigmoid、softmax)来将预测值映射到0-1之间。
  • 如果分类任务使用sigmoid,当输出是0或1的值时,梯度接近于0,也出现了梯度消失现象

CrossEntropy

交叉熵在单分类问题上基本是标配的方法,如下式所示,其中n表示n种类别。

在这里插入图片描述

  • 二分类

在二分类的情况下,模型最后需要预测的结果只有两种情况,对于每个类别我们的预测得到的概率为 p p p 1 − p 1 − p 1p,此时表达式为:

在这里插入图片描述
其中:

y i y_i yi:表示样本 i i i的label,正类为1,负类为0。
p i p_i pi:表示样本 i i i预测为正类的概率。

  • 多分类

多分类的情况实际上就是对二分类的扩展:
在这里插入图片描述
其中:

M M M: 类别的数量
y i c y_{ic} yic:符号函数0或1,如果样本i ii的真实类别等于c$取1,否则取0。
p i c p_{ic} pic:观测样本 i i i属于类别 c c c的预测概率

现在我们利用这个表达式计算下面例子中的损失函数值:

预测真实是否正确
0.3 0.3 0.40 0 1 (猪)正确
0.3 0.4 0.30 1 0 (狗)正确
0.1 0.2 0.71 0 0 (猫)错误

在这里插入图片描述

对所有样本的loss求平均:
在这里插入图片描述

预测真实是否正确
0.1 0.2 0.70 0 1 (猪)正确
0.1 0.7 0.20 1 0 (狗)正确
0.3 0.4 0.31 0 0 (猫)错误

在这里插入图片描述
对所有样本的loss求平均:
在这里插入图片描述

可以发现,虽然上面两个模型的预测值一样,但是交叉熵差别还挺大,所以我们通常使用交叉熵来计算多分类任务。

  • softmax与交叉熵联合使用如下

在这里插入图片描述

参考资料:

简简单单了解一下softmax与交叉熵

损失函数|交叉熵损失函数

详解torch.nn.NLLLOSS

https://pytorch.org/docs/stable/index.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值