【转载】softmax的性质及其实现
原文地址:https://segmentfault.com/a/1190000010039529?utm_source=tag-newest
softmax函数将任意n维的实值向量转换为取值范围在(0,1)之间的n维实值向量,并且总和为1。
例如:向量softmax([1.0, 2.0, 3.0]) ------> [0.09003057, 0.24472847, 0.66524096]
性质:
- 因为softmax是单调递增函数,因此不改变原始数据的大小顺序。
- 将原始输入映射到(0,1)区间,并且总和为1,常用于表征概率。
- softmax(x) = softmax(x+c), 这个性质用于保证数值的稳定性。
softmax的实现及数值稳定性
一个最简单的计算给定向量的softmax的实现如下:
import numpy as np
def softmax(x):
"""Compute the softmax of vector x."""
exp_x = np.exp(x)
softmax_x = exp_x / np.sum(exp_x)
return softmax_x
让我们来测试一下上面的代码:
softmax([1, 2, 3])
array([0.09003057, 0.24472847, 0.66524096])
但是,当我们尝试输入一个比较大的数值向量时,就会出错:
softmax([1000, 2000, 3000])
array([nan, nan, nan])
这是由numpy中的浮点型数值范围限制所导致的。当输入一个较大的数值时,sofmax函数将会超出限制,导致出错。
为了解决这一问题,这时我们就能用到sofmax的第三个性质,即:softmax(x) = softmax(x+c),
一般在实际运用中,通常设定c = - max(x)。
接下来,我们重新定义softmax函数:
import numpy as np
def softmax(x):
"""Compute the softmax in a numerically stable way."""
x = x - np.max(x)
exp_x = np.exp(x)
softmax_x = exp_x / np.sum(exp_x)
return softmax_x
然后再次测试一下:
softmax([1000, 2000, 3000])
array([ 0., 0., 1.])
Done!
以上都是基于向量上的softmax实现,下面提供了基于向量以及矩阵的softmax实现,代码如下:
import numpy as np
def softmax(x):
"""
Compute the softmax function for each row of the input x.
Arguments:
x -- A N dimensional vector or M x N dimensional numpy matrix.
Return:
x -- You are allowed to modify x in-place
"""
orig_shape = x.shape
if len(x.shape) > 1:
# Matrix
exp_minmax = lambda x: np.exp(x - np.max(x))
denom = lambda x: 1.0 / np.sum(x)
x = np.apply_along_axis(exp_minmax,1,x)
denominator = np.apply_along_axis(denom,1,x)
if len(denominator.shape) == 1:
denominator = denominator.reshape((denominator.shape[0],1))
x = x * denominator
else:
# Vector
x_max = np.max(x)
x = x - x_max
numerator = np.exp(x)
denominator = 1.0 / np.sum(numerator)
x = numerator.dot(denominator)
assert x.shape == orig_shape
return x
【转载】softmax的性质及其实现的更多相关文章
- Softmax vs. Softmax-Loss VS cross-entropy损失函数 Numerical Stability(转载)
http://freemind.pluskid.org/machine-learning/softmax-vs-softmax-loss-numerical-stability/ 卷积神经网络系列之s ...
- 【转载】softmax的log似然代价函数(求导过程)
全文转载自:softmax的log似然代价函数(公式求导) 在人工神经网络(ANN)中,Softmax通常被用作输出层的激活函数.这不仅是因为它的效果好,而且因为它使得ANN的输出值更易于理解.同时, ...
- 【转载】深度学习中softmax交叉熵损失函数的理解
深度学习中softmax交叉熵损失函数的理解 2018-08-11 23:49:43 lilong117194 阅读数 5198更多 分类专栏: Deep learning 版权声明:本文为博主原 ...
- Catalan 数列的性质及其应用(转载)
转自:http://lanqi.org/skills/10939/ 卡特兰数 — 计数的映射方法的伟大胜利 发表于2015年11月8日由意琦行 卡特兰(Catalan)数来源于卡特兰解决凸$n+2$边 ...
- 转载:sigmoid和softmax总结
转自:http://blog.csdn.net/u014422406/article/details/52805924 sigmoid函数(也叫逻辑斯谛函数): 引用wiki百科的定义: A logi ...
- 全连接与softmax[转载]
转自:https://www.jianshu.com/p/88bb976ccbd9 1.全连接示例: 2.softmax softmax输入层应和输出层(输出维度与类别数一致)纬度一样,如果不一样,就 ...
- Machine Learning 学习笔记 (3) —— 泊松回归与Softmax回归
本系列文章允许转载,转载请保留全文! [请先阅读][说明&总目录]http://www.cnblogs.com/tbcaaa8/p/4415055.html 1. 泊松回归 (Poisson ...
- (转载)经典计算机视觉论文笔记——DeepFace\DeepID\DeepID2\DeepID3\FaceNet\VGGFace汇总
1. DeepFace:Closing the Gap to Human-Level Performance in Face Verification 最早将深度学习用于人脸验证的开创性工作.Face ...
- 逻辑回归与神经网络还有Softmax regression的关系与区别
本文讨论的关键词:Logistic Regression(逻辑回归).Neural Networks(神经网络) 之前在学习LR和NN的时候,一直对它们独立学习思考,就简单当做是机器学习中的两个不同的 ...
随机推荐
- Java File download
注意文件响应处理方式,是响应为网页形式还是附件显示,看如下信息: In a regular HTTP response, the Content-Disposition response ...
- maven的概念-01
1.maven 简介 maven是Apach软件基金会维护的一款自动化构建工具: 作用是服务于java平台的项目构建和依赖管理: 2.关于项目构建 1)java代码 Java是一门编译型语言,.j ...
- 【Python之路】特别篇--Python面向对象(初级篇)
概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 面向过程编程最易被初学 ...
- Confluence 6.15 锚点(Anchor)宏
允许你链接到页面的特定部分.有关如何使用锚点的内容,请参考页面 Anchors 页面中的详细内容. Wiki 标记(markup) 示例 宏名称: anchor 宏内容:None. {anchor:h ...
- 实现同时将一批.bmp文件转换成.mat格式
%% 功能:实现同时对一批.bmp文件的转换成.mat格式PicFormat = {'*.bmp','Bitmap image (*.bmp)';... '*.jpg','JPEG image (*. ...
- apt 和 apt-get的区别
apt 和 apt-get的区别 - liudsl的博客 - CSDN博客 https://blog.csdn.net/liudsl/article/details/79200134 Linux软件 ...
- BZOJ 4042 Luogu P4757 [CERC2014]Parades (树形DP、状压DP)
题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=4042 (Luogu) https://www.luogu.org/prob ...
- Spring Boot教程(三十四)使用Redis数据库(2)
除了String类型,实战中我们还经常会在Redis中存储对象,这时候我们就会想是否可以使用类似RedisTemplate<String, User>来初始化并进行操作.但是Spring ...
- A. Sea Battle
A. Sea Battle time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- P1598 垂直柱状图
输入格式: 四行字符,由大写字母组成,每行不超过100个字符 输出格式: 由若干行组成,前几行由空格和星号组成,最后一行则是由空格和字母组成的.在任何一行末尾不要打印不需要的多余空格.不要打印任何空行 ...