2017-07-17 75 views
1

在我看來,numpy功能bincount是非常有用和簡單的使用,所以我自然使用TensorFlow中的模擬功能。最近我瞭解到,不幸tf.bincount不支持GPU(因爲您可以閱讀here)。在TensorFlow GPU中有效嗎?有沒有其他方法可以加權直方圖?Tensorflow:有沒有一種方法來建立一個沒有tf.bincount的加權直方圖?

sess = tf.Session() 

values = tf.random_uniform((1,50),10,20,dtype = tf.int32) 
weights = tf.random_uniform((1,50),0,1,dtype = tf.float32) 

counts = tf.bincount(values, weights = weights) 

histogram = sess.run(counts) 
print(histogram) 

回答

1

如所建議的通過ekelsen on GitHub,到tf.bincount高效和GPU支持的另一種方法是tf.unsorted_segment_sum。 正如您可以在documentation中看到的那樣,您可以使用權重爲data的函數,值爲segments_ids。第三個參數num_segments應該大於計數返回直方圖的大小(如果>在上一個直方圖的最後一個直方圖之後只有零個元素)。在我上面的例子將是:

sess = tf.Session() 

values = tf.random_uniform((1,50),10,20,dtype = tf.int32) 
weights = tf.random_uniform((1,50),0,1,dtype = tf.float32) 
bins = 50 
counts = tf.unsorted_segment_sum(weights, values, bins) 

histogram = sess.run(counts) 
print(histogram) 

和輸出:

[ 0.   0.   0.   0.   0.   
    0.   0.   0.   0.   0.   
    2.92621088 1.12118244 2.79792929 0.96016133 2.75781202 
    2.55233836 2.71923089 0.75750649 2.84039998 3.41356659 
    0.   0.   0.   0.   0.   
    0.   0.   0.   0.   0.  ] 
相關問題