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

边栏推荐
- 任务、线程、进程 区别
- Microcomputer principle - bus and its formation
- Serialization, listening, custom annotation
- Advanced C language pointer (Part 2)
- How to solve the problem of fixed assets management and inventory?
- 【MFC开发(17)】高级列表控件List Control
- Shell脚本-select in循环
- Differences among tasks, threads and processes
- Redis——Lettuce连接redis集群
- Insert mathematical formula in MD document and mathematical formula in typora
猜你喜欢

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

Dynamic proxy

Ape anthropology topic 20 (the topic will be updated from time to time)

Nacos - service discovery

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

【MFC开发(16)】树形控件Tree Control

电视机尺寸与观看距离

Installing Oracle EE

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

Vsync+ triple cache mechanism +choreographer
随机推荐
Bird recognition app
In the middle of the year, where should fixed asset management go?
Computer tips
Redis source code learning (29), compressed list learning, ziplist C (II)
R language observation log (part24) -- initialization settings
Principles of Microcomputer - Introduction
Public network cluster intercom +gps visual tracking | help the logistics industry with intelligent management and scheduling
Vsync+ triple cache mechanism +choreographer
Jeecg restart alarm 40001
Mysql8.0 learning record 17 -create table
ARM v7的体系结构A、R、M区别,分别应用在什么领域?
DataBinding源码分析
记一次redis超时
软件工程师面试刷题网站、经验方法
FreeRTOS学习简易笔记
嵌入式工程师面试题3-硬件
How to solve the problem of fixed assets management and inventory?
Shell script -for loop and for int loop
AVL树的理解和实现
C language student information management system