关于tensorflow softmax函数用法解析


Posted in Python onJune 30, 2020

如下所示:

def softmax(logits, axis=None, name=None, dim=None):
 """Computes softmax activations.
 This function performs the equivalent of
  softmax = tf.exp(logits) / tf.reduce_sum(tf.exp(logits), axis)
 Args:
 logits: A non-empty `Tensor`. Must be one of the following types: `half`,
  `float32`, `float64`.
 axis: The dimension softmax would be performed on. The default is -1 which
  indicates the last dimension.
 name: A name for the operation (optional).
 dim: Deprecated alias for `axis`.
 Returns:
 A `Tensor`. Has the same type and shape as `logits`.
 Raises:
 InvalidArgumentError: if `logits` is empty or `axis` is beyond the last
  dimension of `logits`.
 """
 axis = deprecation.deprecated_argument_lookup("axis", axis, "dim", dim)
 if axis is None:
 axis = -1
 return _softmax(logits, gen_nn_ops.softmax, axis, name)

softmax函数的返回结果和输入的tensor有相同的shape,既然没有改变tensor的形状,那么softmax究竟对tensor做了什么?

答案就是softmax会以某一个轴的下标为索引,对这一轴上其他维度的值进行 激活 + 归一化处理

一般来说,这个索引轴都是表示类别的那个维度(tf.nn.softmax中默认为axis=-1,也就是最后一个维度)

举例:

def softmax(X, theta = 1.0, axis = None):
 """
 Compute the softmax of each element along an axis of X.
 Parameters
 ----------
 X: ND-Array. Probably should be floats.
 theta (optional): float parameter, used as a multiplier
  prior to exponentiation. Default = 1.0
 axis (optional): axis to compute values along. Default is the
  first non-singleton axis.
 Returns an array the same size as X. The result will sum to 1
 along the specified axis.
 """
 
 # make X at least 2d
 y = np.atleast_2d(X)
 
 # find axis
 if axis is None:
  axis = next(j[0] for j in enumerate(y.shape) if j[1] > 1)
 
 # multiply y against the theta parameter,
 y = y * float(theta)
 
 # subtract the max for numerical stability
 y = y - np.expand_dims(np.max(y, axis = axis), axis)
 
 # exponentiate y
 y = np.exp(y)
 
 # take the sum along the specified axis
 ax_sum = np.expand_dims(np.sum(y, axis = axis), axis)
 
 # finally: divide elementwise
 p = y / ax_sum
 
 # flatten if X was 1D
 if len(X.shape) == 1: p = p.flatten()
 
 return p
c = np.random.randn(2,3)
print(c)
# 假设第0维是类别,一共有里两种类别
cc = softmax(c,axis=0)
# 假设最后一维是类别,一共有3种类别
ccc = softmax(c,axis=-1)
print(cc)
print(ccc)

结果:

c:
[[-1.30022268 0.59127472 1.21384177]
 [ 0.1981082 -0.83686108 -1.54785864]]
cc:
[[0.1826746 0.80661068 0.94057075]
 [0.8173254 0.19338932 0.05942925]]
ccc:
[[0.0500392 0.33172426 0.61823654]
 [0.65371718 0.23222472 0.1140581 ]]

可以看到,对axis=0的轴做softmax时,输出结果在axis=0轴上和为1(eg: 0.1826746+0.8173254),同理在axis=1轴上做的话结果的axis=1轴和也为1(eg: 0.0500392+0.33172426+0.61823654)。

这些值是怎么得到的呢?

以cc为例(沿着axis=0做softmax):

关于tensorflow softmax函数用法解析

以ccc为例(沿着axis=1做softmax):

关于tensorflow softmax函数用法解析

知道了计算方法,现在我们再来讨论一下这些值的实际意义:

cc[0,0]实际上表示这样一种概率: P( label = 0 | value = [-1.30022268 0.1981082] = c[*,0] ) = 0.1826746

cc[1,0]实际上表示这样一种概率: P( label = 1 | value = [-1.30022268 0.1981082] = c[*,0] ) = 0.8173254

ccc[0,0]实际上表示这样一种概率: P( label = 0 | value = [-1.30022268 0.59127472 1.21384177] = c[0]) = 0.0500392

ccc[0,1]实际上表示这样一种概率: P( label = 1 | value = [-1.30022268 0.59127472 1.21384177] = c[0]) = 0.33172426

ccc[0,2]实际上表示这样一种概率: P( label = 2 | value = [-1.30022268 0.59127472 1.21384177] = c[0]) = 0.61823654

将他们扩展到更多维的情况:假设c是一个[batch_size , timesteps, categories]的三维tensor

output = tf.nn.softmax(c,axis=-1)

那么 output[1, 2, 3] 则表示 P(label =3 | value = c[1,2] )

以上这篇关于tensorflow softmax函数用法解析就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python语言实现获取主机名根据端口杀死进程
Mar 31 Python
Python单链表简单实现代码
Apr 27 Python
全面了解Python环境配置及项目建立
Jun 30 Python
Python字符串和字典相关操作的实例详解
Sep 23 Python
python微信跳一跳系列之棋子定位颜色识别
Feb 26 Python
Pandas Shift函数的基础入门学习笔记
Nov 16 Python
django模板加载静态文件的方法步骤
Mar 01 Python
python网络应用开发知识点浅析
May 28 Python
python3.8.1+selenium实现登录滑块验证功能
May 22 Python
利用python 下载bilibili视频
Nov 13 Python
matplotlib相关系统目录获取方式小结
Feb 03 Python
python爬虫scrapy基本使用超详细教程
Feb 20 Python
基于tensorflow for循环 while循环案例
Jun 30 #Python
解析Tensorflow之MNIST的使用
Jun 30 #Python
Tensorflow tensor 数学运算和逻辑运算方式
Jun 30 #Python
Python requests模块安装及使用教程图解
Jun 30 #Python
在Tensorflow中实现leakyRelu操作详解(高效)
Jun 30 #Python
TensorFlow-gpu和opencv安装详细教程
Jun 30 #Python
tensorflow 2.1.0 安装与实战教程(CASIA FACE v5)
Jun 30 #Python
You might like
PHP.MVC的模板标签系统(一)
2006/09/05 PHP
php中apc缓存使用示例
2013/12/25 PHP
PHP 接入支付宝即时到账功能
2016/09/18 PHP
php连接微软MSSQL(sql server)完全攻略
2016/11/27 PHP
老生常谈php中传统验证与thinkphp框架(必看篇)
2017/06/10 PHP
PHP自定义序列化接口Serializable用法分析
2017/12/29 PHP
php中加密解密DES类的简单使用方法示例
2020/03/26 PHP
JavaScript 在线压缩和格式化收藏
2009/01/16 Javascript
JavaScript高级程序设计(第3版)学习笔记6 初识js对象
2012/10/11 Javascript
easyui datagrid 键盘上下控制选中行示例
2014/03/31 Javascript
js获取input长度并根据页面宽度设置其大小及居中对齐
2014/08/22 Javascript
解决jquery实现的radio重新选中的问题
2015/07/03 Javascript
js判断一个字符串是以某个字符串开头的简单实例
2016/12/27 Javascript
js实现打地鼠小游戏
2017/02/13 Javascript
vue2.0+vuex+localStorage代办事项应用实现详解
2018/05/31 Javascript
vue.js通过路由实现经典的三栏布局实例代码
2018/07/08 Javascript
小程序图片长按识别功能的实现方法
2018/08/30 Javascript
React如何实现浏览器打印部分内容详析
2019/05/19 Javascript
Vuex新手的理解与使用详解
2019/05/31 Javascript
小程序实现列表展开收起效果
2020/07/29 Javascript
PyQt5每天必学之像素图控件QPixmap
2018/04/19 Python
Python多线程中阻塞(join)与锁(Lock)使用误区解析
2018/04/27 Python
Python中py文件引用另一个py文件变量的方法
2018/04/29 Python
pandas.dataframe按行索引表达式选取方法
2018/10/30 Python
Python实现截取PDF文件中的几页代码实例
2019/03/11 Python
Python3 chardet模块查看编码格式的例子
2019/08/14 Python
Python二次规划和线性规划使用实例
2019/12/09 Python
Jupyter Notebook 实现正常显示中文和负号
2020/04/24 Python
浅析移动设备HTML5页面布局
2015/12/01 HTML / CSS
Qoo10马来西亚:全球时尚和引领潮流的购物市场
2016/08/25 全球购物
LINUX下线程,GDI类的解释
2012/04/17 面试题
大学生个人求职信范文
2013/09/21 职场文书
大学生就业自我鉴定
2013/10/26 职场文书
手术室护士长竞聘书
2014/03/31 职场文书
电子信息专业应届生自荐信
2014/06/04 职场文书
优秀党员先进事迹材料2016
2016/02/29 职场文书