Pytorch损失函数torch.nn.NLLLoss()详解

在各种深度学习框架中,我们最常用的损失函数就是交叉熵(torch.nn.CrossEntropyLoss),熵是用来描述一个系统的混乱程度,通过交叉熵我们就能够确定预测数据与真是数据之间的相近程度。交叉熵越小,表示数据越接近真实样本。

交叉熵计算公式:

就是我们预测的概率的对数与标签的乘积,当qk->1的时候,它的损失接近零。

nn.NLLLoss
官方文档中介绍称: nn.NLLLoss输入是一个对数概率向量和一个目标标签,它与nn.CrossEntropyLoss的关系可以描述为:softmax(x)+log(x)+nn.NLLLoss====>nn.CrossEntropyLoss

CrossEntropyLoss()=log_softmax() + NLLLoss() 

其中softmax函数又称为归一化指数函数,它可以把一个多维向量压缩在(0,1)之间,并且它们的和为1.

计算公式:
1
示例代码

import math
z = [1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0]
z_exp = [math.exp(i) for i in z]  
print(z_exp)  # Result: [2.72, 7.39, 20.09, 54.6, 2.72, 7.39, 20.09] 
sum_z_exp = sum(z_exp)  
print(sum_z_exp)  # Result: 114.98 
softmax = [round(i / sum_z_exp, 3) for i in z_exp]
print(softmax)  # Result: [0.024, 0.064, 0.175, 0.475, 0.024, 0.064, 0.175]

log_softmax
log_softmax是指在softmax函数的基础上,再进行一次log运算,此时结果有正有负,log函数的值域是负无穷到正无穷,当x在0—1之间的时候,log(x)值在负无穷到0之间。

nn.NLLLoss
此时,nn.NLLLoss的结果就是把上面的输出与Label对应的那个值拿出来,再去掉负号,再求均值。
代码示例:

import torch
input=torch.randn(3,3)
soft_input = torch.nn.Softmax(dim=0)
soft_input(input)
Out[20]: 
tensor([[0.7284, 0.7364, 0.3343],
        [0.1565, 0.0365, 0.0408],
        [0.1150, 0.2270, 0.6250]])

#对softmax结果取log
torch.log(soft_input(input))
Out[21]: 
tensor([[-0.3168, -0.3059, -1.0958],
        [-1.8546, -3.3093, -3.1995],
        [-2.1625, -1.4827, -0.4701]])

假设标签是[0,1,2],第一行取第0个元素,第二行取第1个,第三行取第2个,去掉负号,即[0.3168,3.3093,0.4701],求平均值,就可以得到损失值。

(0.3168+3.3093+0.4701)/3
Out[22]: 1.3654000000000002

#验证一下

loss=torch.nn.NLLLoss()
target=torch.tensor([0,1,2])
loss(input,target)
Out[26]: tensor(0.1365)

nn.CrossEntropyLoss

loss=torch.nn.NLLLoss()
target=torch.tensor([0,1,2])
loss(input,target)
Out[26]: tensor(-0.1399)
loss =torch.nn.CrossEntropyLoss()
input = torch.tensor([[ 1.1879,  1.0780,  0.5312],
        [-0.3499, -1.9253, -1.5725],
        [-0.6578, -0.0987,  1.1570]])
target = torch.tensor([0,1,2])
loss(input,target)
Out[30]: tensor(0.1365)

以上为全部实验验证两个loss函数之间的关系!!!
  • 83
    点赞
  • 222
    收藏
    觉得还不错? 一键收藏
  • 21
    评论
torch.nn.Linear是PyTorch中的一个模块,用于定义一个线性层。它接受两个参数,即输入和输出的维度。通过调用torch.nn.Linear(input_dim, output_dim),可以创建一个线性层,其中input_dim是输入的维度,output_dim是输出的维度。Linear模块的主要功能是执行线性变换,将输入数据乘以权重矩阵,并加上偏置向量。这个函数的具体实现可以参考PyTorch官方文档中的链接。 在引用中的示例中,linear1是一个Linear模块的实例。可以通过print(linear1.weight.data)来查看linear1的权重。示例中给出了权重的具体数值。 在引用中的示例中,x是一个Linear模块的实例,输入维度为5,输出维度为2。通过调用x(data)来计算线性变换的结果。在这个示例中,输入data的维度是(5,5),输出的维度是(5,2)。可以使用torch.nn.functional.linear函数来实现与torch.nn.Linear相同的功能,其中weight和bias分别表示权重矩阵和偏置向量。 以上是关于torch.nn.Linear的一些介绍和示例。如果需要更详细的信息,可以参考PyTorch官方文档中关于torch.nn.Linear的说明。 https://pytorch.org/docs/stable/_modules/torch/nn/modules/linear.html<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [torch.nn.Linear详解](https://blog.csdn.net/sazass/article/details/123568203)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [torch.nn.Linear](https://blog.csdn.net/weixin_41620490/article/details/127833324)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [pytorch 笔记:torch.nn.Linear() VS torch.nn.function.linear()](https://blog.csdn.net/qq_40206371/article/details/124473437)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值