当前位置:网站首页>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)
边栏推荐
- Apk signature process introduction [easy to understand]
- The latest intelligent factory MES management system software solution
- Development cost of smart factory management system software platform
- Samba basic usage
- PHP implements sensitive word filtering system "suggestions collection"
- Software construction scheme of smart factory collaborative management and control application system
- Yolov5 practice: teach object detection by hand
- 两数之和c语言实现[通俗易懂]
- C language implementation of sum of two numbers [easy to understand]
- Heavy disclosure! Hundreds of important information systems have been invaded, and the host has become a key attack target
猜你喜欢
Debiasing word embeddings | talking about word embedding and deviation removal # yyds dry goods inventory #
Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion
How to write good code - Defensive Programming Guide
Explain in detail the process of realizing Chinese text classification by CNN
Oom caused by improper use of multithreading
Replace UUID, nanoid is faster and safer!
Cassette helicopter and alternating electric field magnetic manometer DPC
Nearly 60% of the employees strongly support Ctrip's "3+2" working mode, and work at home for two days a week
Yolov5 practice: teach object detection by hand
Roewe rx5's "a little more" product strategy
随机推荐
股票万1免5证券开户是合理安全的吗,怎么讲
Research Report on development monitoring and investment prospects of China's smart environmental protection industry (2022 Edition)
Maizeer: the two batches of products reported by the media have been taken off the shelves and sealed, and consumer appeals are accepted
Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
Is it reasonable and safe to open a securities account for 10000 shares free of charge? How to say
Radhat builds intranet Yum source server
Explain in detail the process of realizing Chinese text classification by CNN
Sword finger offer II 105 Maximum area of the island
传感器尺寸、像素、DPI分辨率、英寸、毫米的关系
Report on research and investment prospects of UHMWPE industry in China (2022 Edition)
[splishsplash] about how to receive / display user parameters, MVC mode and genparam on GUI and JSON
Subnet division and summary
What are the six steps of the software development process? How to draw software development flow chart?
JDBC:深入理解PreparedStatement和Statement[通俗易懂]
How to retrieve the password for opening Excel files
MySQL -- explain performance optimization
网上股票开户安全吗?是否可靠?
EasyCVR设备录像出现无法播放现象的问题修复
Yolov5 practice: teach object detection by hand
ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing