TensorFlow函数:tf.sparse_softmax

2018-03-12 11:27 更新

tf.sparse_softmax 函数

sparse_softmax( sp_input, name=None )

定义在:tensorflow/python/ops/sparse_ops.py.

请参阅指南:稀疏张量>数学运算

将 softmax 应用于批量的 N 维 SparseTensor.

在 tf.sparse_softmax 函数中输入代表一个具有逻辑形状[..., B, C](其中N >= 2)的 N 维 SparseTensor ,并且具有按照规范词典顺序排序的索引.

这个操作相当于将 tf.nn.softmax() 应用于具有形状 [B, C] 的每个最内层逻辑子矩阵,但是有了catch,隐式零元素不参与.具体来说,该算法等同于:

  1. 将 tf.nn.softmax () 应用于每个内层子矩阵的致密视图,其形状为 [B, C],沿着大小为 C 的维度;
  2. 掩盖原始的隐式零元素;
  3. 重新规格化剩余的元素.

因此,SparseTensor 结果具有完全相同的非零指数和形状.

以下是一个示例:

# First batch: # [? e.] # [1. ? ] # Second batch: # [e ? ] # [e e ] shape = [2, 2, 2] # 3-D SparseTensor values = np.asarray([[[0., np.e], [1., 0.]], [[np.e, 0.], [np.e, np.e]]]) indices = np.vstack(np.where(values)).astype(np.int64).T result = tf.sparse_softmax(tf.SparseTensor(indices, values, shape)) # ...returning a 3-D SparseTensor, equivalent to: # [? 1.] [1 ?] # [1. ? ] and [.5 .5] # where ? means implicitly zero

函数参数:

  • sp_input:表示 N 维 SparseTensor,其中 N >= 2.
  • name:操作的可选名称.

函数返回值:

  • output:表示结果的 N 维 SparseTensor.
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号