关于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列表(list)常用操作方法小结
Feb 02 Python
使用Python的Twisted框架实现一个简单的服务器
Apr 16 Python
在Django的模型中执行原始SQL查询的方法
Jul 21 Python
selenium+python自动化测试之多窗口切换
Jan 23 Python
如何用Python做一个微信机器人自动拉群
Jul 03 Python
django的csrf实现过程详解
Jul 26 Python
Python如何实现的二分查找算法
May 27 Python
什么是python类属性
Jun 10 Python
Python3读写ini配置文件的示例
Nov 06 Python
python 爬虫请求模块requests详解
Dec 04 Python
使用Pytorch实现two-head(多输出)模型的操作
May 28 Python
简单且有用的Python数据分析和机器学习代码
Jul 02 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(三)
2006/10/09 PHP
PHP+DBM的同学录程序(1)
2006/10/09 PHP
PHP面向对象的使用教程 简单数据库连接
2006/11/25 PHP
PHP生成HTML静态页面实例代码
2008/08/31 PHP
PHP的构造方法,析构方法和this关键字详细介绍
2013/10/22 PHP
Zend Framework教程之Zend_Helpers动作助手ViewRenderer用法详解
2016/07/20 PHP
避免 showModalDialog 弹出新窗体的原因分析
2010/05/31 Javascript
jQuery 1.5 源码解读 面向中高阶JSER
2011/04/05 Javascript
JavaScript中奇葩的假值示例应用
2014/03/11 Javascript
jQuery的Each比JS原生for循环性能慢很多的原因
2016/07/05 Javascript
js实现百度登录框鼠标拖拽效果
2017/03/07 Javascript
JavaScript贪吃蛇小组件实例代码
2017/08/20 Javascript
Jquery获取radio选中值实例总结
2019/01/17 jQuery
微信小程序实现日期格式化和倒计时
2020/11/01 Javascript
weui上传多图片,压缩,base64编码的示例代码
2020/06/22 Javascript
javascript实现多边形碰撞检测
2020/10/24 Javascript
[01:05]主宰至宝剑心之遗
2017/03/16 DOTA
python 装饰器功能以及函数参数使用介绍
2012/01/27 Python
Python语言实现获取主机名根据端口杀死进程
2016/03/31 Python
利用python和ffmpeg 批量将其他图片转换为.yuv格式的方法
2019/01/08 Python
python tkinter canvas 显示图片的示例
2019/06/13 Python
python利用appium实现手机APP自动化的示例
2021/01/26 Python
CSS3绘制超炫的上下起伏波动进度加载动画
2016/04/21 HTML / CSS
CSS3的常见transformation图形变化用法小结
2016/05/13 HTML / CSS
百联网上商城:i百联
2017/01/28 全球购物
英国珠宝和手表专家:Pleasance & Harper
2020/10/21 全球购物
某个公司的Java笔面试题
2016/03/11 面试题
教师评优事迹材料
2014/01/10 职场文书
教师个人鉴定材料
2014/02/08 职场文书
政治表现评语
2014/05/04 职场文书
环卫工作个人总结
2015/03/04 职场文书
交通事故被告答辩状
2015/05/22 职场文书
爱国主义主题班会
2015/08/14 职场文书
详解如何使用Node.js实现热重载页面
2021/05/06 Javascript
Spring Cloud Gateway去掉url前缀
2021/07/15 Java/Android