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

边栏推荐
- Common interview questions for embedded engineers 2-mcu_ STM32
- Shell脚本-位置参数(命令行参数)
- Dynamic proxy
- Shell脚本-for循环和for int循环
- Why is the Ltd independent station a Web3.0 website!
- Full mark standard for sports items in the high school entrance examination (Shenzhen, Anhui and Hubei)
- 【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DS18B20温度传感器 +NodeJs本地服务+ MySQL数据库
- 3. Detailed explanation of Modbus communication protocol
- Shell script -for loop and for int loop
- Promise异步编程
猜你喜欢

Computer tips

Nacos - Configuration Management

Dynamic proxy

如何做好固定资产管理?易点易动提供智能化方案

Glitch free clock switching technology

3. Detailed explanation of Modbus communication protocol

Bimianhongfu queren()

ARM v7的体系结构A、R、M区别,分别应用在什么领域?

It is designed with high bandwidth, which is almost processed into an open circuit?

猿人学第20题(题目会不定时更新)
随机推荐
Shell script case in statement
C语言学生信息管理系统
Shell脚本-case in 和正则表达式
MySQL optimization
Shell script - string
嵌入式工程师面试题3-硬件
Nacos - Configuration Management
足球篮球体育比赛比分直播平台源码/app开发建设项目
Mysql8.0 learning record 17 -create table
Shell脚本-while循环详解
FreeRTOS学习简易笔记
Foundation: 2 The essence of image
Imitation of Baidu search results top navigation bar effect
【MFC开发(17)】高级列表控件List Control
小鸟识别APP
Shell脚本-特殊变量:Shell $#、$*、[email protected]、$?、$$
How to manage fixed assets efficiently in one stop?
大型工厂设备管理痛点和解决方案
Common interview questions for embedded engineers 2-mcu_ STM32
如何解决固定资产管理和盘点的难题?