关于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文档生成工具pydoc使用介绍
Jun 02 Python
python插入排序算法实例分析
Jul 03 Python
Django自定义分页与bootstrap分页结合
Feb 22 Python
python批量替换页眉页脚实例代码
Jan 22 Python
Python图像处理实现两幅图像合成一幅图像的方法【测试可用】
Jan 04 Python
Python Web静态服务器非堵塞模式实现方法示例
Nov 21 Python
Pytorch模型转onnx模型实例
Jan 15 Python
解决tensorflow训练时内存持续增加并占满的问题
Jan 19 Python
使用keras框架cnn+ctc_loss识别不定长字符图片操作
Jun 29 Python
爬虫代理的cookie如何生成运行
Sep 22 Python
python 实现端口扫描工具
Dec 18 Python
细说NumPy数组的四种乘法的使用
Dec 18 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 和 MySQL 开发的 8 个技巧
2007/01/02 PHP
php cookie使用方法学习笔记分享
2013/11/07 PHP
用 Composer构建自己的 PHP 框架之使用 ORM
2014/10/30 PHP
php自定义加密与解密程序实例
2014/12/31 PHP
Zend Framework动作助手(Zend_Controller_Action_Helper)用法详解
2016/03/05 PHP
tp5框架前台无限极导航菜单类实现方法分析
2020/03/29 PHP
如何使用jQuery Draggable和Droppable实现拖拽功能
2013/07/05 Javascript
JavaScript实现Flash炫光波动特效
2015/05/14 Javascript
jquery实现简单手风琴菜单效果实例
2015/06/13 Javascript
JS+CSS实现大气的黑色首页导航菜单效果代码
2015/09/10 Javascript
js实现数组和对象的深浅拷贝
2017/09/30 Javascript
js中事件对象和事件委托的介绍
2019/01/21 Javascript
Vue 处理表单input单行文本框的实例代码
2019/05/09 Javascript
jQuery实现小火箭返回顶部特效
2020/02/03 jQuery
微信小程序自定义纯净模态框(弹出框)的实例代码
2020/03/09 Javascript
Python字符串替换实例分析
2015/05/11 Python
在Django的上下文中设置变量的方法
2015/07/20 Python
Python cookbook(数据结构与算法)实现优先级队列的方法示例
2018/02/18 Python
python scipy求解非线性方程的方法(fsolve/root)
2018/11/12 Python
Python小工具之消耗系统指定大小内存的方法
2018/12/03 Python
Python如何获得百度统计API的数据并发送邮件示例代码
2019/01/27 Python
django的聚合函数和aggregate、annotate方法使用详解
2019/07/23 Python
python 读写文件包含多种编码格式的解决方式
2019/12/20 Python
基于torch.where和布尔索引的速度比较
2020/01/02 Python
Python爬虫入门教程01之爬取豆瓣Top电影
2021/01/24 Python
深入理解HTML5定时器requestAnimationFrame的使用
2018/12/12 HTML / CSS
意大利制造的西装、衬衫和针对男士量身定制的服装:Lanieri
2018/04/08 全球购物
eDreams加拿大:廉价航班、酒店和度假
2019/03/29 全球购物
聚美优品的广告词
2014/03/14 职场文书
数学教师个人总结
2015/02/06 职场文书
2015年医院药剂科工作总结
2015/05/04 职场文书
活动新闻稿范文
2015/07/17 职场文书
2019年暑期法院实习报告
2019/12/18 职场文书
vue实现简单数据双向绑定
2021/04/28 Vue.js
解决MySQL添加新用户-ERROR 1045 (28000)的问题
2022/03/03 MySQL
SQL Server携程核心系统无感迁移到MySQL实战
2022/06/01 SQL Server