我正在使用tf.unsorted_segment_sumTensorFlow的方法,当我作为数据给出的张量只有一行时,它工作正常。例如:
tf.unsorted_segment_sum(tf.constant([0.2, 0.1, 0.5, 0.7, 0.8]),
tf.constant([0, 0, 1, 2, 2]), 3)
Run Code Online (Sandbox Code Playgroud)
给出正确的结果:
array([ 0.3, 0.5 , 1.5 ], dtype=float32)
Run Code Online (Sandbox Code Playgroud)
问题是,如果我使用多行的张量,我怎样才能得到每一行的结果?例如,如果我尝试使用两行张量:
tf.unsorted_segment_sum(tf.constant([[0.2, 0.1, 0.5, 0.7, 0.8],
[0.2, 0.2, 0.5, 0.7, 0.8]]),
tf.constant([[0, 0, 1, 2, 2],
[0, 0, 1, 2, 2]]), 3)
Run Code Online (Sandbox Code Playgroud)
我期望的结果是:
array([ [ 0.3, 0.5 , 1.5 ], [ 0.4, 0.5, 1.5 ] ], dtype=float32)
Run Code Online (Sandbox Code Playgroud)
但我得到的是:
array([ 0.7, 1. , 3. ], dtype=float32)
Run Code Online (Sandbox Code Playgroud)
我想知道是否有人知道如何在不使用 for 循环的情况下获取每一行的结果?
提前致谢