当前位置:网站首页>【pytorch】softmax函数
【pytorch】softmax函数
2022-07-01 09:03:00 【Enzo 想砸电脑】
做过多分类任务的同学一定都知道softmax函数。softmax函数,又称归一化指数函数。它是二分类函数sigmoid在多分类上的推广,目的是将多分类的结果以概率的形式展现出来。下图展示了softmax的计算方法:

下面为大家解释一下为什么softmax是这种形式。
首先,我们知道概率有两个性质:1)预测的概率为非负数;2)各种预测结果概率之和等于1。
softmax就是将在负无穷到正无穷上的预测结果按照这两步转换为概率的。
1)将预测结果转化为非负数
下图为y=exp(x)的图像,我们可以知道指数函数的值域取值范围是零到正无穷。softmax第一步就是将模型的预测结果转化到指数函数上,这样保证了概率的非负性。

2)各种预测结果概率之和等于1
为了确保各个预测结果的概率之和等于1。我们只需要将转换后的结果进行归一化处理。方法就是将转化后的结果除以所有转化后结果之和,可以理解为转化后结果占总数的百分比。这样就得到近似的概率。
下面为大家举一个例子,假如模型对一个三分类问题的预测结果为-3、1.5、2.7。我们要用softmax将模型结果转为概率。步骤如下:
1)将预测结果转化为非负数
y1 = exp(x1) = exp(-3) = 0.05
y2 = exp(x2) = exp(1.5) = 4.48
y3 = exp(x3) = exp(2.7) = 14.88
2)各种预测结果概率之和等于1
z1 = y1/(y1+y2+y3) = 0.05/(0.05+4.48+14.88) = 0.0026
z2 = y2/(y1+y2+y3) = 4.48/(0.05+4.48+14.88) = 0.2308
z3 = y3/(y1+y2+y3) = 14.88/(0.05+4.48+14.88) = 0.7666
总结一下softmax如何将多分类输出转换为概率,可以分为两步:
1)分子:通过指数函数,将实数输出映射到零到正无穷。
2)分母:将所有结果相加,进行归一化。
下图为斯坦福大学CS224n课程中最softmax的解释:

边栏推荐
- Advanced level of C language pointer (Part 1)
- [interview brush 101] linked list
- Shell脚本-for循环和for int循环
- Shell script case in and regular expressions
- 固定资产管理系统让企业动态掌握资产情况
- Nacos - gestion de la configuration
- Microcomputer principle - bus and its formation
- How to effectively align team cognition
- TV size and viewing distance
- The meaning of yolov5 training visualization index
猜你喜欢

嵌入式工程师面试题3-硬件

Insert mathematical formula in MD document and mathematical formula in typora

如何一站式高效管理固定资产?
![[MFC development (17)] advanced list control list control](/img/e8/24c52cb51defc6c96b43c2ef3232ff.png)
[MFC development (17)] advanced list control list control

FAQ | FAQ for building applications for large screen devices

What are the differences between the architecture a, R and m of arm V7, and in which fields are they applied?

Mysql 优化

Football and basketball game score live broadcast platform source code /app development and construction project

如何解决固定资产管理和盘点的难题?

集团公司固定资产管理的痛点和解决方案
随机推荐
V79.01 Hongmeng kernel source code analysis (user mode locking) | how to use the fast lock futex (Part 1) | hundreds of blogs analyze the openharmony source code
猿人学第20题(题目会不定时更新)
【MFC开发(17)】高级列表控件List Control
动态代理
Redis source code learning (29), compressed list learning, ziplist C (II)
Embedded Engineer Interview frequently asked questions
Promise asynchronous programming
个人装修笔记
DataBinding源码分析
软件工程师面试刷题网站、经验方法
[MFC development (16)] tree control
Shell script - definition, assignment and deletion of variables
Do you know how data is stored? (C integer and floating point)
AVL树的理解和实现
Bimianhongfu queren()
Interrupt sharing variables with other functions and protection of critical resources
Win7 pyinstaller reports an error DLL load failed while importing after packaging exe_ Socket: parameter error
Naoqi robot summary 28
足球篮球体育比赛比分直播平台源码/app开发建设项目
Shell脚本-read命令:读取从键盘输入的数据