tf.segment_sum和tf.unsorted_segment_sum

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010365819/article/details/88224476
c = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]])
result = tf.segment_sum(c, tf.constant([0, 0, 1]))#第二个参数长度必须为3
result_ = tf.segment_sum(c, tf.constant([0, 1, 1]))
result__ = tf.segment_sum(c, tf.constant([0, 1, 2]))
result2 = tf.unsorted_segment_sum(c, tf.constant([2, 1, 1]),3)#第二个参数长度必须为3
result3 = tf.unsorted_segment_sum(c, tf.constant([1, 0, 1]),2)
#result4 = tf.unsorted_segment_sum(c, tf.constant([2, 0, 1]),2) #错误,segment_ids[0] = 2 is out of range [0, 2)
result4 = tf.unsorted_segment_sum(c, tf.constant([2, 0, 1]),3)
result5 = tf.unsorted_segment_sum(c, tf.constant([3, 1, 0]),5)
sess = tf.Session()
print("result")
print(sess.run(result))
print("result_")
print(sess.run(result_))
print("result__")
print(sess.run(result__))
print("result2")
print(sess.run(result2))
print("result3")
print(sess.run(result3))
print("result4")
print(sess.run(result4))
print("result5")
print(sess.run(result5))

输出结果:

result
[[0 0 0 0]
 [5 6 7 8]]
result_
[[1 2 3 4]
 [4 4 4 4]]
result__
[[ 1  2  3  4]
 [-1 -2 -3 -4]
 [ 5  6  7  8]]
result2
[[0 0 0 0]
 [4 4 4 4]
 [1 2 3 4]]
result3
[[-1 -2 -3 -4]
 [ 6  8 10 12]]
result4
[[-1 -2 -3 -4]
 [ 5  6  7  8]
 [ 1  2  3  4]]
result5
[[ 5  6  7  8]
 [-1 -2 -3 -4]
 [ 0  0  0  0]
 [ 1  2  3  4]
 [ 0  0  0  0]]

第二个方法名称上和第一个方法比多了一个unsorted,意思是第二个参数,segmentids可以是无序的,但是第一个方法必须是有序的。还有就是segmentids的size必须和da ta的第一维度长度一致,因为这个是一个近似所以的关系。

tf.segment_sum:

c = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]])
result = tf.segment_sum(c, tf.constant([0, 0, 1]))#第二个参数长度必须为3

因为是[0,0,1],所以[1,2,3,4], [-1,-2,-3,-4]相加,[5,6,7,8]不变,因此输出结果[[0 0 0 0]
                                                                                                                                             [5 6 7 8]]

result_ = tf.segment_sum(c, tf.constant([0, 1, 1]))

因为[0,1,1]所以第一个位置[1,2,3,4]不变,第二个位置[-1,-2,-3,-4], [5,6,7,8]相加,输出

[[1 2 3 4]
 [4 4 4 4]]
result__ = tf.segment_sum(c, tf.constant([0, 1, 2]))

因为[0,1,2]三个值都不一样,所以都不相加,输出结果

扫描二维码关注公众号,回复: 5516491 查看本文章

[[ 1  2  3  4]
 [-1 -2 -3 -4]
 [ 5  6  7  8]]

tf.unsorted_segment_sum:

和前面的方法一样,不同之处就是ids可以是无序的,但是结果输出还是要按照顺序输出,同时还多了一个参数

num_segments,指定了输出的维度。

c = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]])

result2 = tf.unsorted_segment_sum(c, tf.constant([2, 1, 1]),3)#第二个参数长度必须为3

因为ids[2, 1, 1],所以1的位置两个相加放在1的位置, [-1,-2,-3,-4], [5,6,7,8]相加,[1,2,3,4]不变放在2的位置,但是nu m_segments为3,所以最后输出应该size为3,在0的位置用0补齐,最后结果为

[[0 0 0 0]
 [4 4 4 4]
 [1 2 3 4]]
result3 = tf.unsorted_segment_sum(c, tf.constant([1, 0, 1]),2)

[1,2,3,4],[5,6,7,8]相加放在1的位置, [-1,-2,-3,-4]不变放在0的位置,输出

[[-1 -2 -3 -4]
 [ 6  8 10 12]]
#result4 = tf.unsorted_segment_sum(c, tf.constant([2, 0, 1]),2) #错误,segment_ids[0] = 2 is out of range [0, 2)
result4 = tf.unsorted_segment_sum(c, tf.constant([2, 0, 1]),3)

不需要相加,因为ids都不同,按顺序输出

[[-1 -2 -3 -4]
 [ 5  6  7  8]
 [ 1  2  3  4]]
result5 = tf.unsorted_segment_sum(c, tf.constant([3, 1, 0]),5)

不想要相加,按顺序输出,因为没有2,4,所以在2和4的位置用0补齐

[[ 5  6  7  8]
 [-1 -2 -3 -4]
 [ 0  0  0  0]
 [ 1  2  3  4]
 [ 0  0  0  0]]

猜你喜欢

转载自blog.csdn.net/u010365819/article/details/88224476