关于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模块学习 filecmp 文件比较
Aug 27 Python
Python使用Scrapy爬取妹子图
May 28 Python
Python中断言Assertion的一些改进方案
Oct 27 Python
python中异常捕获方法详解
Mar 03 Python
简单谈谈python中的lambda表达式
Jan 19 Python
windows下安装python的C扩展编译环境(解决Unable to find vcvarsall.bat)
Feb 21 Python
Python3 实现随机生成一组不重复数并按行写入文件
Apr 09 Python
python3之模块psutil系统性能信息使用
May 30 Python
Python使用pyautocad+openpyxl处理cad文件示例
Jul 11 Python
Django工程的分层结构详解
Jul 18 Python
python中的selenium安装的步骤(浏览器自动化测试框架)
Mar 17 Python
Python中使用Lambda函数的5种用法
Apr 01 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中常量,变量的作用域和生存周期
2013/08/10 PHP
php中通过DirectoryIterator删除整个目录的方法
2015/03/13 PHP
php使用socket post数据到其它web服务器的方法
2015/06/02 PHP
php调用淘宝开放API实现根据卖家昵称获取卖家店铺ID的方法
2015/07/29 PHP
使用PHP实现生成HTML静态页面
2015/11/18 PHP
phpstudy2018升级MySQL5.5为5.7教程(图文)
2018/10/24 PHP
laravel 去掉index.php伪静态的操作方法
2019/10/12 PHP
简易的投票系统以及js刷票思路和方法
2015/04/07 Javascript
针对JavaScript中this指向的简单理解
2016/08/26 Javascript
微信小程序 支付简单实例及注意事项
2017/01/06 Javascript
浅谈vue中.vue文件解析流程
2018/04/24 Javascript
Vue Promise的axios请求封装详解
2018/08/13 Javascript
javascript 易错知识点实例小结
2020/04/25 Javascript
vue 组件简介
2020/07/31 Javascript
Vue实现图书管理小案例
2020/12/03 Vue.js
一分钟学会JavaScript中的try-catch
2020/12/14 Javascript
[16:21]教你分分钟做大人:圣堂刺客
2014/12/03 DOTA
动态创建类实例代码
2009/10/07 Python
Python制作CSDN免积分下载器
2015/03/10 Python
Python的Flask框架标配模板引擎Jinja2的使用教程
2016/07/12 Python
PyQt实现界面翻转切换效果
2018/04/20 Python
tensorflow使用指定gpu的方法
2020/02/04 Python
tensorboard显示空白的解决
2020/02/15 Python
python使用dlib进行人脸检测和关键点的示例
2020/12/05 Python
HTML5录音实践总结(Preact)
2020/05/07 HTML / CSS
StubHub意大利:购买和出售全球演唱会和体育赛事门票
2017/11/21 全球购物
英国马匹装备和马术用品购物网站:Equine Superstore
2019/03/03 全球购物
2019史上最全Database工程师题库
2015/12/06 面试题
十月份红领巾广播稿
2014/01/22 职场文书
大队干部竞选演讲稿
2014/04/28 职场文书
2015年乡镇平安建设工作总结
2015/05/13 职场文书
一百条裙子读书笔记
2015/07/01 职场文书
二十年同学聚会感言
2015/07/30 职场文书
apache基于端口创建虚拟主机的示例
2021/04/24 Servers
JVM入门之类加载与字节码技术(类加载与类的加载器)
2021/06/15 Java/Android
Kubernetes控制节点的部署
2022/04/01 Servers