def matrix_segment(): """ :return: """ isses = tf.InteractiveSession() # 对角值 X = tf.constant([5., 1., 7., 2., 3., 4., 1., 3.], dtype=tf.float32) s_id = [0, 0, 0, 1, 2, 2, 3, 3] logger.info("X\n%s" % X) logger.info("s_id\n%s" % s_id) logger.info("tf.segment_sum(X,s_id)\n {0}".format(tf.segment_sum(X, s_id))) logger.info("tf.segment_mean(X,s_id)\n {0}".format( tf.segment_mean(X, s_id).eval())) logger.info("tf.segment_max(X, s_id)\n {0}".format( tf.segment_max(X, s_id).eval())) logger.info("tf.segment_min(X, s_id)\n {0}".format( tf.segment_min(X, s_id).eval())) logger.info("tf.segment_prod(X, s_id)\n {0}".format( tf.segment_prod(X, s_id).eval())) logger.info("tf.unsorted_segment_sum(X, s_id)\n {0}".format( tf.unsorted_segment_sum(X, s_id, 2))) # c = tf.constant([0., 1.], dtype=tf.float32) # logger.info("tf.sparse_segment_sum(X, s_id)\n {0}".format(tf.sparse_segment_sum(X, c, s_id))) # logger.info("tf.sparse_segment_mean(X, s_id)\n {0}".format(tf.sparse_segment_mean(X, c, s_id))) # logger.info("tf.sparse_segment_sqrt_n(X, s_id)\n {0}".format(tf.sparse_segment_sqrt_n(X, c, s_id))) isses.close()
def test_segment(): seg_ids = tf.constant([0, 1, 1, 2, 2]) x = tf.constant([[2, 5, 3, -5], [0, 3, -2, 5], [4, 3, 5, 3], [6, 1, 4, 0], [6, 1, 4, 0]]) with tf.Session() as sess: # 按seg_ids进行加法 print(tf.segment_sum(x, seg_ids).eval()) # 按seg_ids进行乘法 print(tf.segment_prod(x, seg_ids).eval()) # 按seg_ids进行min运算 print(tf.segment_min(x, seg_ids).eval()) # 按seg_ids进行max运算 print(tf.segment_max(x, seg_ids).eval()) # 按seg_ids进行mean运算 print(tf.segment_mean(x, seg_ids).eval())
def test_SegmentProd(self): t = tf.segment_prod(self.random(4, 2, 3), np.array([0, 1, 1, 2])) self.check(t)
import tensorflow as tf """tf.segment_prod(data, segment_ids, name=None) 功能:tensor进行拆分后求积。 输入:segment_ids:必须是整型,1维向量,向量数目与data第一维的数量一致。 必须从0开始,且以1进行递增。""" a = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) z = tf.segment_prod(a, [0, 0, 1]) sess = tf.Session() print(sess.run(z)) sess.close() # z==>[[4 10 18] # [7 8 9]]
import tensorflow as tf import numpy as np input_a = np.array([[1, 1, 2], [2, 3, 4], [3, 1, 1], [2, 4, 6]]) a_seg_sum = tf.segment_sum(data=input_a, segment_ids=[0, 1, 1, 1]) a_seg_prod = tf.segment_prod(data=input_a, segment_ids=[0, 0, 1, 1]) a_seg_max = tf.segment_max(data=input_a, segment_ids=[0, 0, 0, 1]) a_seg_min = tf.segment_min(data=input_a, segment_ids=[1, 1, 1, 1]) a_seg_mean = tf.segment_mean(data=input_a, segment_ids=[0, 0, 0, 1]) a_seg_sum_num = tf.unsorted_segment_sum(data=input_a, segment_ids=[0, 1, 1, 0], num_segments=2) a_sparse_seg_sum = tf.sparse_segment_sum(data=input_a, indices=[0, 1, 2], segment_ids=[0, 0, 1]) with tf.Session() as sess: init = tf.global_variables_initializer() sess.run(init) print(sess.run(a_seg_sum), '\n', sess.run(a_seg_prod), '\n', sess.run(a_seg_max), '\n', sess.run(a_seg_min)) print(sess.run(a_seg_mean), '\n', sess.run(a_seg_sum_num), '\n', sess.run(a_sparse_seg_sum))
first dimension onto segment_ids. The segment_ids tensor should be the size of the first dimension, d0, with consecutive IDs in the range 0 to k, where k<d0. In particular, a segmentation of a matrix tensor is a mapping of rows to segments. Gopi Subramanian 12-March-2016 """ import tensorflow as tf import numpy as np a_constant = tf.constant([[1, 2], [3, 4]]) print "(d0, d1)", a_constant.get_shape() # segment id per row seg_ids = tf.constant([0, 1]) seg_sum = tf.segment_sum(a_constant, seg_ids) seg_prod = tf.segment_prod(a_constant, seg_ids) # Session sess = tf.Session() output_sum = sess.run(seg_sum) output_prod = sess.run(seg_prod) sess.close() print output_sum print output_prod
import tensorflow as tf sess = tf.InteractiveSession() seg_ids = tf.constant([0, 1, 1, 2, 2]) #下标 tens1 = tf.constant([[2, 5, 3, -5], [0, 3, -2, 5], [6, 1, 4, 0], [6, 1, 4, 0]]) print(tf.segment_sum(tens1, seg_ids).eval()) print(tf.segment_prod(tens1, seg_ids).eval()) print(tf.segment_min(tens1, seg_ids).eval()) print(tf.segment_max(tens1, seg_ids).eval()) print(tf.segment_mean(tens1, seg_ids).eval())
tf.cumsum([a, b, c], exclusive=True, reverse=True) # => [b + c, c, 0] # Computes the sum/mean/max/min/prod along segments of a tensor tf.segment_sum(data, segment_ids, name=None) # Eg: m = tf.constant([5,1,7,2,3,4,1,3]) s_id = [0,0,0,1,2,2,3,3] s.run(tf.segment_sum(m, segment_ids=s_id)) """ >array([13, 2, 7, 4], dtype=int32) """ tf.segment_mean(data, segment_ids, name=None) tf.segment_max(data, segment_ids, name=None) tf.segment_min(data, segment_ids, name=None) tf.segment_prod(data, segment_ids, name=None) # 其它 tf.unsorted_segment_sum tf.sparse_segment_sum tf.sparse_segment_mean tf.sparse_segment_sqrt_n # 比较两个 list 或者 string 的不同,并返回不同的值和索引 tf.setdiff1d(x, y, index_dtype=tf.int32, name=None) # 返回 x 中的唯一值所组成的tensor 和原 tensor 中元素在现 tensor 中的索引 tf.unique(x, out_idx=None, name=None) # x if condition else y, condition 为 bool 类型的,可用tf.equal()等来表示 # x 和 y 的形状和数据类型必须一致
#Segmentation Examples import tensorflow as tf sess = tf.InteractiveSession() seg_ids = tf.constant([0,1,1,2,2]); # Group indexes : 0|1,2|3,4 tens1 = tf.constant([[2, 5, 3, -5], [0, 3,-2, 5], [4, 3, 5, 3], [6, 1, 4, 0], [6, 1, 4, 0]]) # A sample constant matrix tf.segment_sum(tens1, seg_ids).eval() # Sum segmentation tf.segment_prod(tens1, seg_ids).eval() # Product segmantation tf.segment_min(tens1, seg_ids).eval() # minimun value goes to group tf.segment_max(tens1, seg_ids).eval() # maximum value goes to group tf.segment_mean(tens1, seg_ids).eval() # mean value goes to group
# Segmentation Examples import tensorflow as tf sess = tf.InteractiveSession() seg_ids = tf.constant([0, 1, 1, 2, 2]) # Group indexes : 0|1,2|3,4 print seg_ids.get_shape().as_list() print tens1 = tf.constant([[2, 5, 3, -5], [0, 3, -2, 5], [4, 3, 5, 3], [6, 1, 4, 0], [6, 1, 4, 0]]) # A sample constant matrix print tens1.get_shape().as_list() print print tf.segment_sum(tens1, seg_ids).eval() # Sum segmentation print tf.segment_prod(tens1, seg_ids).eval().shape # Product segmantation print tf.segment_min(tens1, seg_ids).eval() # minimun value goes to group print tf.segment_max(tens1, seg_ids).eval() # maximum value goes to group print tf.segment_mean(tens1, seg_ids).eval() # mean value goes to group
#!/usr/bin/env python import tensorflow as tf import numpy as np # tf.segment_sum x = np.random.rand(10, 4, 3) segment_ids = [0, 0, 0, 1, 2, 2, 3, 4, 5, 5] z_segment_sum = tf.segment_sum(x, segment_ids) # tf.segment_prod x = np.random.rand(10, 4, 3) segment_ids = [0, 0, 0, 1, 2, 2, 3, 4, 5, 5] z_segment_prod = tf.segment_prod(x, segment_ids) # tf.segment_min x = np.random.rand(10, 4, 3) segment_ids = [0, 0, 0, 1, 2, 2, 3, 4, 5, 5] z_segment_min = tf.segment_min(x, segment_ids) # tf.segment_max x = np.random.rand(10, 4, 3) segment_ids = [0, 0, 0, 1, 2, 2, 3, 4, 5, 5] z_segment_max = tf.segment_max(x, segment_ids) # tf.segment_mean x = np.random.rand(10, 4, 3) segment_ids = [0, 0, 0, 1, 2, 2, 3, 4, 5, 5] z_segment_mean = tf.segment_mean(x, segment_ids) # tf.unsorted_segment_sum
tf.cumsum([a, b, c], exclusive=True) ==> [0, a, a + b] tf.cumsum([a, b, c], reverse=True) ==> [a + b + c, b + c, c] tf.cumsum([a, b, c], exclusive=True, reverse=True) ==> [b + c, c, 0] 六、分割(Segmentation) tf.segment_sum(data, segment_ids, name=None) 根据segment_ids的分段计算各个片段的和 其中segment_ids为一个size与data第一维相同的tensor 其中id为int型数据,最大id不大于size c = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]]) tf.segment_sum(c, tf.constant([0, 0, 1])) ==>[[0 0 0 0] [5 6 7 8]] 上面例子分为[0,1]两id,对相同id的data相应数据进行求和, 并放入结果的相应id中, 且segment_ids只升不降 tf.segment_prod(data, segment_ids, name=None) 根据segment_ids的分段计算各个片段的积 tf.segment_min(data, segment_ids, name=None) 根据segment_ids的分段计算各个片段的最小值 tf.segment_max(data, segment_ids, name=None) 根据segment_ids的分段计算各个片段的最大值 tf.segment_mean(data, segment_ids, name=None) 根据segment_ids的分段计算各个片段的平均值 tf.unsorted_segment_sum(data, segment_ids, num_segments, name=None) 与tf.segment_sum函数类似, 不同在于segment_ids中id顺序可以是无序的 tf.sparse_segment_sum(data, indices, segment_ids, name=None) 输入进行稀疏分割求和 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]] 对原data的indices为[0,1]位置的进行分割, 并按照segment_ids的分组进行求和
import os os.environ['TF_CPP_MIN_LOG_LEVEL']='2' import tensorflow as tf sess = tf.InteractiveSession() seg_ids = tf.constant([0,1,1,2,2]) # Group indexes : 0|1,2|3,4 tens1 = tf.constant([[2, 5, 3, -5], [0, 3,-2, 5], [4, 3, 5, 3], [6, 1, 4, 0], [6, 1, 4, 0]]) # A sample constant m print('\nseg_ids->', seg_ids.eval()) print('tens1->', tens1.eval()) print("\ntf.segment_sum(tens1, seg_ids).eval() ") # Sum segmen print(tf.segment_sum(tens1, seg_ids).eval() ) # Sum segmen print("\ntf.segment_prod(tens1, seg_ids).eval() ") # Product segmen print(tf.segment_prod(tens1, seg_ids).eval() ) # Product segmen print(tf.segment_min(tens1, seg_ids).eval() ) # minimun value goes to print(tf.segment_max(tens1, seg_ids).eval() ) # maximum value goes to print(tf.segment_mean(tens1, seg_ids).eval() ) # mean value goes to group
#Segmentation Examples import tensorflow as tf sess = tf.InteractiveSession() seg_ids = tf.constant([0, 1, 1, 2, 2]) # Group indexes : 0|1,2|3,4 tens1 = tf.constant([[2, 5, 3, -5], [0, 3, -2, 5], [4, 3, 5, 3], [6, 1, 4, 0], [6, 1, 4, 0]]) # A sample constant matrix tf.segment_sum(tens1, seg_ids).eval() # Sum segmentation tf.segment_prod(tens1, seg_ids).eval() # Product segmantation tf.segment_min(tens1, seg_ids).eval() # minimun value goes to group tf.segment_max(tens1, seg_ids).eval() # maximum value goes to group tf.segment_mean(tens1, seg_ids).eval() # mean value goes to group