• 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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    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)
    
    • 1
    • 2
    • 3

    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)
    
    • 1
    • 2
    • 3

    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)
    
    • 1
    • 2
    • 3

    再谈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

  • 相关阅读:
    自定义容器控件之C#设计笔记(十五)
    公司为什么要使用OKR,目的是什么?
    Prometheus
    【iOS开发】iOS App的加固保护原理:使用ipaguard混淆加固
    anaconda安装paddle(安装CUDA,CUDNN)
    图的邻接矩阵存储 C语言实现BFS
    分割回文串
    EasyPoi导入
    FileChannel 文件流的简单使用
    自制快速冒烟测试小工具--基于python多线程
  • 原文地址:https://blog.csdn.net/qq_41990294/article/details/126907905