TensorFlow数学函数:tf.sparse_segment_sum

2018-03-09 10:30 更新

tf.sparse_segment_sum 函数

sparse_segment_sum( data, indices, segment_ids, name=None, num_segments=None )

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

请参阅指南:数学>分段

计算张量的稀疏段的和.

与 SegmentSum 类似,不同的是 segment_ids 可以有低于 data 的第一维度的秩,选择维度 0 的子集 (由索引指定).segment_ids 允许缺少 ID,在这种情况下,输出将在这些索引处为零.在这些情况下,num_segments 用于确定输出的大小.

如下示例:

c = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]]) # Select two rows, one segment. tf.sparse_segment_sum(c, tf.constant([0, 1]), tf.constant([0, 0])) # => [[0 0 0 0]] # Select two rows, two segment. tf.sparse_segment_sum(c, tf.constant([0, 1]), tf.constant([0, 1])) # => [[ 1 2 3 4] # [-1 -2 -3 -4]] # With missing segment ids. tf.sparse_segment_sum(c, tf.constant([0, 1]), tf.constant([0, 2]), num_segments=4) # => [[ 1 2 3 4] # [ 0 0 0 0] # [-1 -2 -3 -4] # [ 0 0 0 0]] # Select all rows, two segments. tf.sparse_segment_sum(c, tf.constant([0, 1, 2]), tf.constant([0, 0, 1])) # => [[0 0 0 0] # [5 6 7 8]] # Which is equivalent to: tf.segment_sum(c, tf.constant([0, 0, 1]))

函数参数:

  • data:Tensor 数据将在输出中组合.
  • indices:1 维 Tensor,索引为 data.与 segment_ids 具有相同的秩.
  • segment_ids:一个 1-D 的 Tensor,它带有索引的输出 Tensor.值应该排序并且可以重复.
  • name:操作的名称(可选).
  • num_segments:一个可选的 int32 标量.指示输出 Tensor 的大小.

函数返回值:

tf.sparse_segment_sum 函数返回作为数据的形状的一个 tensor,除了大小为 k 的维度 0 之外,它通过 num_segments 指定的段的数量或在 segments_ids 中推断最后一个元素.

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号