当前位置:网站首页>Pytorch crossentropyloss learning
Pytorch crossentropyloss learning
2022-07-01 18:02:00 【Dream without trace 123】
class torch.nn.CrossEntropyLoss(weight=None,size_average=None,ignore_index=-100,reduce=None,reduction='elementwise_mean')
function :
Pass the input through softmax After activating the function , Then calculate it and target The cross entropy loss of . That is, the method will nn.LogSoftmax() and nn.NLLLoss() Combined with . Strictly speaking, the cross loss function should be nn.NLLLoss()
Cross entropy loss (cross-entropy Loss) Also known as log likelihood loss (Log-likelihood loss), Logarithmic loss ; In the second classification, it can also be called logistic regression loss . The expression of cross entropy loss function is L=-sigama(y_ilog(x_i)).PyTorch First the input after softmax Activation function , Vector " normalization " In the form of probability , And then with target Calculate the cross entropy loss in the strict sense .
In multi category tasks , Always use softmax Activation function + Cross entropy loss function , Because cross entropy describes the difference between two probability distributions , But the neural network outputs vectors , It's not in the form of a probability distribution . So we need to softmax The activation function performs a vector " normalization " In the form of probability distribution . So we need to softmax The activation function performs a vector " normalization " In the form of probability distribution , And then the cross entropy loss function is used to calculate Loss.PyTorch Of CrossEntropyLoss(), Will be nn.LogSoftmax() and nn.NLLLoss() Combined with ,nn.LogSoftmax() Equivalent to activation function ,nn.NLLLoss() Is the loss function , Combine it , So I think this function should be called softmax+ Cross entropy loss function .
example :
import torch import torch.nn as nn import numpy as np entroy = nn.CrossEntropyLoss() input = torch.Tensor([[-0.7715,-0.6205,-0.2562]]) target = torch.tensor([0]) output = entroy(input,target) print(output) # use CrossEntropyLoss The result of the calculation . myselfout = -(input[:,0])+np.log(np.exp(input[:,0])+np.exp(input[:,1])+np.exp(input[:,2])) # Calculate the result with formula print(myselfout) lsf = nn.LogSoftmax() loss = nn.NLLLoss() lsfout = lsf(input) lsfnout = loss(lsfout,target) print(lsfnout)
边栏推荐
- EasyCVR设备录像出现无法播放现象的问题修复
- ZABBIX alarm execute remote command
- Is online stock account opening safe? Is it reliable?
- APK签名流程介绍[通俗易懂]
- At present, where is the most formal and safe account opening for futures speculation? How to open a futures account?
- Depth first traversal and breadth first traversal [easy to understand]
- 两数之和c语言实现[通俗易懂]
- Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion
- Product service, operation characteristics
- Work and leisure suggestions of old programmers
猜你喜欢

New 95 community system whole station source code

Fix the black screen caused by iPhone system failure

DNS

Heavy disclosure! Hundreds of important information systems have been invaded, and the host has become a key attack target

Intelligent operation and maintenance practice: banking business process and single transaction tracking

Yuancosmos game farmersworld farmers world - core content of the second conference in China!

Setting up a time server requires the client to automatically synchronize the time of the server at 9 a.m. every day

Product service, operation characteristics

Apache iceberg source code analysis: schema evolution

SQL injection vulnerability (MySQL and MSSQL features)
随机推荐
China biodegradable plastics market forecast and investment strategy report (2022 Edition)
SQL injection vulnerability (MySQL and MSSQL features)
Kernel stray cat stray dog pet adoption platform H5 source code
JDBC:深入理解PreparedStatement和Statement[通俗易懂]
(17) DAC conversion experiment
MES production equipment manufacturing execution system software
期货先锋这个软件正规吗安全吗?选择哪家期货公司更安全?
Equipment simulation and deduction training system software
[C supplement] [string] display the schedule of a month by date
Three dimensional anti-terrorism Simulation Drill deduction training system software
Is Huishang futures a regular futures platform? Is it safe to open an account in Huishang futures?
MFC obtains local IP (used more in network communication)
聊聊项目经理最爱使用的工具
In depth Research Report on China's disposable sanitary products production equipment industry (2022 Edition)
ACL 2022 | decomposed meta learning small sample named entity recognition
Fix the problem that easycvr device video cannot be played
[splishsplash] about how to receive / display user parameters, MVC mode and genparam on GUI and JSON
Sword finger offer II 105 Maximum area of the island
两数之和c语言实现[通俗易懂]
Leetcode 1380. Lucky numbers in the matrix (save the minimum number of each row and the maximum number of each column)