当前位置:网站首页>NLLLoss+log_ SoftMax=CE_ Loss
NLLLoss+log_ SoftMax=CE_ Loss
2022-04-23 09:17:00 【365JHWZGo】
List of articles
subject
NLLLoss+log_SoftMax=CE_Loss
Preface
It hasn't been updated for several days , alas , A little busy recently , Please forgive me ! Today, I finally stole some time to watch B Station video discovery NLLLoss+log_SoftMax=CE_Loss, ha-ha , I've seen it again , So let's take a closer look today !
NLLLoss
chinese
Negative log likelihood loss
function
torch.nn.NLLLoss(
weight=None,
size_average=None,
ignore_index=- 100,
reduce=None,
reduction='mean'
)
Parameters,
| Parameters | type | meaning |
|---|---|---|
| weight | tensor(optional) | Manually readjust the weight proportion of each class |
| size_average | bool(optional) Have been abandoned | Loss is a batch The average value of each loss element in |
| ignore_index | int(optional) | Ignoring a target value does not affect the gradient of the input |
| reduce | bool(optional) Have been abandoned | According to each mini-batch The average size of the loss is divided equally or summarized |
| reduction | string(optional) | reuduction Will be applied to the output |
| reduction Parameters | explain |
|---|---|
| none | Expand all |
| mean | Add up / Number |
| sum | hold none The results add up |
Function input and output
| Input | The goal is | Output | |
|---|---|---|---|
| type | tensor | tensor | tensor |
| dimension | ( N , C N,C N,C)/( C C C) | ( N N N)/() | ( N N N)/ ( N , d 1 , d 2 , . . . , d K ) (N, d_1, d_2, ..., d_K) (N,d1,d2,...,dK) |
Code
import torch
# Imitate the output after passing through the model
preds = torch.tensor([[1.5,2.5,3.0]])
# Real label
target = torch.tensor([1])
nllloss = torch.nn.NLLLoss()
print('nllloss:',nllloss(preds,target))

And CE_Loss The difference and connection
difference :
- CE_Loss It's going through first log_softmax after NLLLoss Loss of steps
- NLLLoss It just corresponds to index Of target The value becomes the opposite number
contact :
- There is a certain correlation , after softmax Most of the subsequent values are [0,1] Between , It's going on log After taking the logarithm, the value is distributed in ( − ∞ -\infty −∞,0] Between , and loss It can't be negative , So we need to NLLLoss To make it positive
import torch
# Imitate the output after passing through the model
preds = torch.tensor([[1.5,2.5,3.0]])
# Real label
target = torch.tensor([1])
cross_entropy_loss = torch.nn.CrossEntropyLoss()
log_softmax = torch.nn.LogSoftmax(dim=1)
nllloss = torch.nn.NLLLoss()
cs_loss = cross_entropy_loss(preds,target)
nls_loss = nllloss(log_softmax(preds),target)
print(f' The cross entropy loss function is :{
cs_loss}\n Go first log_softmax after nll The loss function is :{
nls_loss}')

版权声明
本文为[365JHWZGo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230633453332.html
边栏推荐
- Enterprise wechat application authorization / silent login
- 考研线性代数常见概念、问题总结
- Distributed message oriented middleware framework selection - Digital Architecture Design (7)
- Harbor enterprise image management system
- Download and install bashdb
- js 原型链的深入
- Go language self-study series | initialization of golang structure
- STM32 and FreeRTOS stack parsing
- tsdf +mvs
- Failed to prepare device for development
猜你喜欢

Using JS to realize a thousandth bit

LeetCode_DFS_中等_1254. 统计封闭岛屿的数目

653. 两数之和 IV - 输入 BST

Leetcode-199 - right view of binary tree

DJ music management software pioneer DJ rekordbox

Failed to download esp32 program, prompting timeout

SAP 101K 411K 库存变化

The crawler returns null when parsing with XPath. The reason why the crawler cannot get the corresponding element and the solution

EmuElec 编译总结

Applet error: cannot read property'currenttarget'of undefined
随机推荐
《数字电子技术基础》3.1 门电路概述、3.2 半导体二极管门电路
RSA 加密解密签名验签
Kettle experiment (III)
Wechat applet catchtap = "todetail" event problem
ATSS(CVPR2020)
Give the method of instantiating the object to the new object
Data visualization: use Excel to make radar chart
What is monitoring intelligent playback and how to use intelligent playback to query video recording
Taxable income
112. Path sum
Image processing in opencv -- Introduction to contour + contour features
Group Backpack
Valgrind et kcachegrind utilisent l'analyse d'exécution
[boutique] using dynamic agent to realize unified transaction management II
The K neighbors of each sample are obtained by packet switching
Go language self-study series | golang nested structure
PCTP考试经验分享
[original] use system Text. JSON formats the JSON string
Emuelec compilation summary
Go language self-study series | golang structure pointer