关于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 相关文章推荐
使用PyV8在Python爬虫中执行js代码
Feb 16 Python
Python简单定义与使用字典dict的方法示例
Jul 25 Python
python学习笔记之列表(list)与元组(tuple)详解
Nov 23 Python
基于Python列表解析(列表推导式)
Jun 23 Python
python读取word文档,插入mysql数据库的示例代码
Nov 07 Python
python的继承知识点总结
Dec 10 Python
基于python实现百度翻译功能
May 09 Python
Python 转换RGB颜色值的示例代码
Oct 13 Python
Python jieba库用法及实例解析
Nov 04 Python
python批量处理txt文件的实例代码
Jan 13 Python
Python基于pandas爬取网页表格数据
May 11 Python
Python入门之使用pandas分析excel数据
May 12 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中的类-什么叫类
2006/11/20 PHP
Google Voice 短信发送接口PHP开源版(2010.5更新)
2010/07/22 PHP
php设计模式 Strategy(策略模式)
2011/06/26 PHP
TP - 比RBAC更好的权限认证方式(Auth类认证)
2021/03/09 PHP
Jquery工作常用实例 使用AJAX使网页进行异步更新
2011/07/26 Javascript
jQuery.validate 常用方法及需要注意的问题
2013/03/20 Javascript
Jquery操作下拉框(DropDownList)实现取值赋值
2013/08/13 Javascript
使用JSON.parse将json字符串转换成json对象的时候会出错
2014/09/04 Javascript
测试IE浏览器对JavaScript的AngularJS的兼容性
2015/06/19 Javascript
jQuery实现MSN中文网滑动Tab菜单效果代码
2015/09/09 Javascript
基于JavaScript实现回到页面顶部动画代码
2016/05/24 Javascript
sencha ext js 6 快速入门(必看)
2016/06/01 Javascript
js代码延迟一定时间后执行一个函数的实例
2017/02/15 Javascript
js前端日历控件(悬浮、拖拽、自由变形)
2017/03/02 Javascript
微信小程序实现点击返回顶层的方法
2017/07/12 Javascript
ReactNative列表ListView的用法
2017/08/02 Javascript
node.js多个异步过程中判断执行是否完成的解决方案
2017/12/10 Javascript
3种vue路由传参的基本模式
2018/02/22 Javascript
Vue.directive()的用法和实例详解
2018/03/04 Javascript
vue的mixins属性详解
2018/03/14 Javascript
vue实现循环切换动画
2018/10/17 Javascript
react脚手架如何配置less和ant按需加载的方法步骤
2018/11/28 Javascript
js纯前端实现腾讯cos文件上传功能的示例代码
2019/05/14 Javascript
vue 使用rules对表单字段进行校验的步骤
2020/12/25 Vue.js
python实现类似ftp传输文件的网络程序示例
2014/04/08 Python
在Python程序中操作文件之isatty()方法的使用教程
2015/05/24 Python
Django自定义过滤器定义与用法示例
2018/03/22 Python
攻击者是如何将PHP Phar包伪装成图像以绕过文件类型检测的(推荐)
2018/10/11 Python
pytorch的梯度计算以及backward方法详解
2020/01/10 Python
Python建造者模式案例运行原理解析
2020/06/29 Python
浅谈matplotlib 绘制梯度下降求解过程
2020/07/12 Python
俄罗斯金苹果网上化妆品和香水商店:Goldapple
2019/12/01 全球购物
综合素质评价个性发展自我评价
2015/03/06 职场文书
责任书范本大全
2015/05/11 职场文书
律政俏佳人观后感
2015/06/09 职场文书
vue3获取当前路由地址
2022/02/18 Vue.js