关于tensorflow softmax函数用法解析

 更新时间:2020年06月30日 11:27:06   作者:ASR_THU  
这篇文章主要介绍了关于tensorflow softmax函数用法解析,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

如下所示:

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):

以ccc为例(沿着axis=1做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导出chrome书签到markdown文件的实例代码

    python导出chrome书签到markdown文件的实例代码

    python导出chrome书签到markdown文件,主要就是解析chrome的bookmarks文件,然后拼接成markdown格式的字符串,最后输出到文件即可。下面给大家分享实例代码,需要的朋友参考下
    2017-12-12
  • python使用append合并两个数组的方法

    python使用append合并两个数组的方法

    这篇文章主要介绍了python使用append合并两个数组的方法,涉及Python中append方法的使用技巧,需要的朋友可以参考下
    2015-04-04
  • Python音频处理库pydub的使用示例详解

    Python音频处理库pydub的使用示例详解

    pydub是一个轻量级的音频处理库,安装方便,使用简单,这篇文章主要为大家详细介绍了pydub的具体使用,文中的示例代码讲解详细,需要的小伙伴可以参考下
    2023-11-11
  • Python通过OpenPyXL处理Excel的完整教程

    Python通过OpenPyXL处理Excel的完整教程

    OpenPyXL是一个强大的Python库,用于处理Excel文件,允许读取、编辑和创建Excel工作簿和工作表,本文将详细介绍OpenPyXL的各种功能,希望对大家有所帮助
    2023-11-11
  • python 高效去重复 支持GB级别大文件的示例代码

    python 高效去重复 支持GB级别大文件的示例代码

    今天小编就为大家分享一篇python 高效去重复 支持GB级别大文件的示例代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-11-11
  • python os模块在系统管理中的应用

    python os模块在系统管理中的应用

    这篇文章主要介绍了python os模块在系统管理中的应用,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-06-06
  • Python使用国内镜像加速pip安装的方法讲解

    Python使用国内镜像加速pip安装的方法讲解

    在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速度缓慢甚至超时,为了解决这个问题,本文将详细介绍如何使用-i参数配置国内镜像源,加速pip的安装过程,需要的朋友可以参考下
    2025-02-02
  • Python绘制3d螺旋曲线图实例代码

    Python绘制3d螺旋曲线图实例代码

    这篇文章主要介绍了Python绘制3d螺旋曲线图实例代码,具有一定借鉴价值,需要的朋友可以参考下。
    2017-12-12
  • python 实现矩阵上下/左右翻转,转置的示例

    python 实现矩阵上下/左右翻转,转置的示例

    今天小编就为大家分享一篇python 实现矩阵上下/左右翻转,转置的示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • 给Python中的MySQLdb模块添加超时功能的教程

    给Python中的MySQLdb模块添加超时功能的教程

    这篇文章主要介绍了给Python中的MySQLdb模块添加超时功能的教程,timeout功能在服务器的运维当中非常有用,需要的朋友可以参考下
    2015-05-05

最新评论