当前位置:网站首页>【pytorch】nn. Crossentropyloss() and nn NLLLoss()
【pytorch】nn. Crossentropyloss() and nn NLLLoss()
2022-07-01 09:08:00 【Enzo tried to smash the computer】
One 、 Understanding of meaning The significance of cross entropy as a loss function
Cross entropy loss is often used for Multi classification function , Next, we understand the meaning of cross entropy as a loss function by disassembling the formula of cross entropy
Suppose we are making a n The problem of classification , The output of the model prediction is [ x 1 , x 2 , x 3 , . . . . , x n ] [x_1, x_2, x_3, ...., x_n] [x1,x2,x3,....,xn]
then , We need to define a loss function , Then the weight of the model is adjusted by back propagation , there We choose the loss function Cross entropy loss function ~
nn.CrossEntropyLoss() The formula of is :
l o s s ( x , c l a s s ) = − l o g ( e x [ c l a s s ] ∑ j e x j ) = − x [ c l a s s ] + l o g ( ∑ j e x j ) loss(x, class) = -log(\frac{e^{x_{[class]}}}{\sum_je^{x_{j}}}) = -x_{[class]} + log(\sum_j e^{x_{j}}) loss(x,class)=−log(∑jexjex[class])=−x[class]+log(j∑exj)
- x It's the prediction , It's a vector , The number of elements should be guaranteed by the model , Guarantee as many as the classification number
- class Represents the actual label of this sample , such as , The sample actually belongs to classification 2, that class=2
x [ c l a s s ] x_{[class]} x[class] Namely x 2 x_2 x2, Is to take the second element in the test result vector , That is, take the predicted value corresponding to its real classification
It's over , Next , We need to disassemble the formula , Understand the formula
1、 First , The cross entropy loss function formula contains a basic part : s o f t m a x ( x i ) = e x i ∑ j e x j softmax(x_i) = \frac{e^{x_i}}{\sum_je^{x_{j}}} softmax(xi)=∑jexjexi
softmax The classification results are normalized : e x e^x ex First map the data to (0, 1] The range of , Then make the sum of all classification probabilities equal 1. after softmax After processing ,size It won't change , The meaning of each value is the probability that the sample is assigned to this classification .
2、 We want to make the prediction result , The probability of the value of the real classification is close to 100%. We take the value of the real classification :
e x [ c l a s s ] ∑ j e x j \frac{e^{x_{[class]}}}{\sum_je^{x_{j}}} ∑jexjex[class], We want its value to be 100%
3、 The meaning of being a loss function is : When the prediction results are more Close to the real value , The closer the value of the loss function is to 0.
We put e x [ c l a s s ] ∑ j e x j \frac{e^{x_{[class]}}}{\sum_je^{x_{j}}} ∑jexjex[class] take log, Take the opposite again , Can guarantee when e x [ c l a s s ] ∑ j e x j \frac{e^{x_{[class]}}}{\sum_je^{x_{j}}} ∑jexjex[class] The more close to 100%, l o s s = − l o g ( e x [ c l a s s ] ∑ j e x j ) loss=-log(\frac{e^{x_{[class]}}}{\sum_je^{x_{j}}}) loss=−log(∑jexjex[class]) The closer the 0.
Two 、 application :
Suppose there is 4 A picture , Or say batch_ size=4. We need to put this 4 Pictures are classified into 5 Categories , for instance : bird , Dog , cat , automobile , ship
After network calculation , We got the prediction results :predict,size by [4, 5]
Its real label is label,size by [4]
Next use nn.CrossEntropyLoss() Calculation Predicted results predict and True value label The cross entropy loss of , Sure
import torch
import torch.nn as nn
# -----------------------------------------
# Defining data : batch_size=4; Altogether 5 A classification
# label.size() : torch.Size([4])
# predict.size(): torch.Size([4, 5])
# -----------------------------------------
torch.manual_seed(100)
predict = torch.rand(4, 5)
label = torch.tensor([4, 3, 3, 2])
print(predict)
print(label)
# -----------------------------------------
# Call function directly nn.CrossEntropyLoss() Calculation Loss
# -----------------------------------------
criterion = nn.CrossEntropyLoss()
loss = criterion(predict, label)
print(loss)

nn.CrossEntropyLoss() It can be disassembled as follows 3 A step , Or it can be described as follows 3 Operation replacement , The result is the same :
- softmax: Classify the results of each picture softmax, softmax Detailed introduction
- log: For the above results take log
( step 1 and step 2 Can be combined into nn.logSoftmax() ) - NLL:nn.NLLLoss(a, b) The operation of is from a Remove from b The corresponding value (b What's in store is index value ), Then remove the minus sign ( Take the opposite ), Then sum and take the mean
import torch
import torch.nn as nn
torch.manual_seed(100)
predict = torch.rand(4, 5)
label = torch.tensor([4, 3, 3, 2])
softmax = nn.Softmax(dim=1)
nll = nn.NLLLoss()
temp1 = softmax(predict)
temp2 = torch.log(temp1)
output = nll(temp2, label)
print(output) # tensor(1.5230)
Hand only version
import torch
torch.manual_seed(100)
predict = torch.rand(4, 5)
label = torch.tensor([4, 3, 3, 2])
# softmax
temp1 = torch.exp(predict) / torch.sum(torch.exp(predict), dim=1, keepdim=True)
# log
temp2 = torch.log(temp1)
# nll
temp3 = torch.gather(temp2, dim=1, index=label.view(-1, 1))
temp4 = -temp3
output = torch.mean(temp4)
print(output) # tensor(1.5230)
边栏推荐
- Mysql 优化
- How to manage fixed assets efficiently in one stop?
- 【MFC开发(17)】高级列表控件List Control
- 固定资产管理系统让企业动态掌握资产情况
- FAQ | FAQ for building applications for large screen devices
- Full mark standard for sports items in the high school entrance examination (Shenzhen, Anhui and Hubei)
- Understand shallow replication and deep replication through code examples
- Principles of Microcomputer - Introduction
- Why is the Ltd independent station a Web3.0 website!
- How to solve the problem of fixed assets management and inventory?
猜你喜欢

Redis——Lettuce连接redis集群

Principle and application of single chip microcomputer timer, serial communication and interrupt system

Dynamic proxy

Embedded Engineer Interview Question 3 Hardware

An overview of the design of royalties and service fees of mainstream NFT market platforms

NiO zero copy

Personal decoration notes

FreeRTOS learning easy notes

Public network cluster intercom +gps visual tracking | help the logistics industry with intelligent management and scheduling

How to manage fixed assets well? Easy to point and move to provide intelligent solutions
随机推荐
Understand shallow replication and deep replication through code examples
【检测技术课案】简易数显电子秤的设计与制作
如何高效拉齐团队认知
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于物联网的GY906红外测温门禁刷卡系统
Personal decoration notes
Reproduced Xray - cve-2017-7921 (unauthorized access by Hikvision)
C语言学生信息管理系统
【pytorch学习】torch.device
【pytorch】nn.AdaptiveMaxPool2d
Serialization, listening, custom annotation
DataBinding源码分析
Shell脚本-echo命令 转义符
Summary of reptile knowledge points
Understanding and implementation of AVL tree
[ESP nanny level tutorial] crazy completion chapter - Case: chemical environment system detection based on Alibaba cloud and Arduino, supporting nail robot alarm
JCL 和 SLF4J
I use flask to write the website "one"
The fixed assets management system enables enterprises to dynamically master assets
Nacos - service discovery
Graduation season, I want to tell you