当前位置:网站首页>【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的解释:

边栏推荐
- Shell script echo command escape character
- 中断与其他函数共享变量、临界资源的保护
- 【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云和Arduino的化学环境系统检测,支持钉钉机器人告警
- 钓鱼识别app
- Shell script - string
- 安装Oracle EE
- Principles of Microcomputer - Introduction
- Shell脚本-if else语句
- How can enterprises and developers take the lead in the outbreak of cloud native landing?
- Redis——Lettuce连接redis集群
猜你喜欢
随机推荐
How to manage fixed assets efficiently in one stop?
Common interview questions for embedded engineers 2-mcu_ STM32
Ape anthropology topic 20 (the topic will be updated from time to time)
日常办公耗材管理解决方案
Shell script - array definition and getting array elements
jeecg 重启报40001
Shell脚本-read命令:读取从键盘输入的数据
【MFC开发(16)】树形控件Tree Control
Promise异步编程
Foundation: 3 Opencv getting started images and videos
Shell脚本-变量的定义、赋值和删除
Daily office consumables management solution
Jetson nano installs tensorflow GPU and problem solving
Public network cluster intercom +gps visual tracking | help the logistics industry with intelligent management and scheduling
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
如何一站式高效管理固定资产?
Advanced C language pointer (Part 2)
Pain points and solutions of equipment management in large factories
Log4j 日志框架
C语言学生信息管理系统








