【问题标题】:tensorflow how do one get the output the same size as input tensor after segment sum张量流如何在段总和后获得与输入张量相同大小的输出
【发布时间】:2019-04-08 01:50:51
【问题描述】:

我正在使用 TensorFlow 的 tf.unsorted_segment_sum 方法,它可以工作。 例如:

tf.unsorted_segment_sum(tf.constant([0.2, 0.1, 0.5, 0.7, 0.8]),
                        tf.constant([0, 0, 1, 2, 2]), 3)

给出正确的结果:

array([ 0.3,  0.5 , 1.5 ], dtype=float32)

我想得到:

array([0.3, 0.3, 0.5, 1.5, 1.5], dtype=float32)

【问题讨论】:

    标签: tensorflow segment


    【解决方案1】:

    我已经解决了。

    data = tf.constant([0.2, 0.1, 0.5, 0.7, 0.8])
    gr_idx = tf.constant([0, 0, 1, 2, 2])
    y, idx, count = tf.unique_with_count(gr_idx)
    group_sum = tf.segment_sum(data, gr_idx)
    group_sup = tf.gather(group_sum, idx)
    

    答案:

    array([0.3, 0.3, 0.5, 1.5, 1.5], dtype=float32)
    

    【讨论】:

      猜你喜欢
      • 2020-10-13
      • 2019-03-12
      • 2020-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多