当前位置:网站首页>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)
边栏推荐
- [splishsplash] about how to receive / display user parameters, MVC mode and genparam on GUI and JSON
- 徽商期货是正规期货平台吗?在徽商期货开户安全吗?
- Roewe rx5's "a little more" product strategy
- Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
- Code example of libcurl download file
- Thinkphp6 - CMS multi wechat management system source code
- Equipment simulation and deduction training system software
- Enter wechat applet
- 【Try to Hack】vulnhub DC4
- ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
猜你喜欢

Penetration practice vulnhub range Nemesis

Apache iceberg source code analysis: schema evolution

Debiasing word embeddings | talking about word embedding and deviation removal # yyds dry goods inventory #

Work and leisure suggestions of old programmers

Fix the black screen caused by iPhone system failure

Cassette helicopter and alternating electric field magnetic manometer DPC

Intel's open source deep learning tool library openvino will increase cooperation with local software and hardware parties and continue to open

Euler function: find the number of numbers less than or equal to N and coprime with n

DNS

(十六)ADC转换实验
随机推荐
MySQL + JSON = King fried
Extract the compressed package file and retrieve the password
DNS
开发那些事儿:EasyCVR平台添加播放地址鉴权
DNS
Is it reasonable and safe to open a securities account for 10000 shares free of charge? How to say
Is online stock account opening safe? Is it reliable?
Is it safe to open a stock account by mobile phone? What do you need to bring with you to open an account?
Maizeer: the two batches of products reported by the media have been taken off the shelves and sealed, and consumer appeals are accepted
Detailed explanation of ArrayList expansion
. Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes
Develop those things: add playback address authentication to easycvr platform
ISO 27001 Information Security Management System Certification
Apache iceberg source code analysis: schema evolution
Cloud picture says | distributed transaction management DTM: the little helper behind "buy buy buy"
Software construction scheme of smart factory collaborative management and control application system
. Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes
Leetcode 1380. Lucky numbers in the matrix (save the minimum number of each row and the maximum number of each column)
Easycvr accesses the equipment through the national standard gb28181 protocol. What is the reason for the automatic streaming of the equipment?
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)