zoukankan      html  css  js  c++  java
  • tf.segment_sum和tf.unsorted_segment_sum理解实例

    本文来自 guotong1988 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/guotong1988/article/details/77622790

     1 import tensorflow as tf
     2 c = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]])
     3 result = tf.segment_sum(c, tf.constant([0, 0, 1]))#第二个参数长度必须为3
     4 result_ = tf.segment_sum(c, tf.constant([0, 1, 1]))
     5 result__ = tf.segment_sum(c, tf.constant([0, 1, 2]))
     6 result2 = tf.unsorted_segment_sum(c, tf.constant([2, 1, 1]),3)#第二个参数长度必须为3
     7 result3 = tf.unsorted_segment_sum(c, tf.constant([1, 0, 1]),2)
     8 #result4 = tf.unsorted_segment_sum(c, tf.constant([2, 0, 1]),2) #错误,segment_ids[0] = 2 is out of range [0, 2)
     9 result4 = tf.unsorted_segment_sum(c, tf.constant([2, 0, 1]),3)
    10 result5 = tf.unsorted_segment_sum(c, tf.constant([3, 1, 0]),5)
    11 sess = tf.Session()
    12 print("result")
    13 print(sess.run(result))
    14 print("result_")
    15 print(sess.run(result_))
    16 print("result__")
    17 print(sess.run(result__))
    18 print("result2")
    19 print(sess.run(result2))
    20 print("result3")
    21 print(sess.run(result3))
    22 print("result4")
    23 print(sess.run(result4))
    24 print("result5")
    25 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]]

    将索引值相同的进行求和,其余的按顺序计算。

  • 相关阅读:
    反转链表 16
    CodeForces 701A Cards
    hdu 1087 Super Jumping! Jumping! Jumping!(动态规划)
    hdu 1241 Oil Deposits(水一发,自我的DFS)
    CodeForces 703B(容斥定理)
    poj 1067 取石子游戏(威佐夫博奕(Wythoff Game))
    ACM 马拦过河卒(动态规划)
    hdu 1005 Number Sequence
    51nod 1170 1770 数数字(数学技巧)
    hdu 2160 母猪的故事(睡前随机水一发)(斐波那契数列)
  • 原文地址:https://www.cnblogs.com/gaofighting/p/9706081.html
Copyright © 2011-2022 走看看