【问题标题】:Tensorflow: is there a way to build a weighted histogram without tf.bincount?Tensorflow:有没有办法在没有 tf.bincount 的情况下构建加权直方图?
【发布时间】:2017-07-17 15:16:22
【问题描述】:

在我看来,numpy 函数bincount 非常好用且易于使用,所以我很自然地使用了 TensorFlow 中的类比函数。最近我了解到,不幸的是tf.bincount 没有 GPU 支持(您可以阅读here)。有没有其他方法可以在 TensorFlow with GPU 中高效地进行加权直方图,如下例所示?

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)

【问题讨论】:

    标签: python tensorflow gpu tensorflow-gpu


    【解决方案1】:

    正如ekelsen on GitHub 所建议的,tf.bincount 的一种高效且受 GPU 支持的替代方案是tf.unsorted_segment_sum。 正如您在documentation 中所读到的,您可以使用权重为data、值为segments_ids 的函数。第三个参数num_segments 应该≥ 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)
    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.        ]
    

    【讨论】:

    • 感谢您的回答。知道如何让它支持批次吗?。
    猜你喜欢
    • 1970-01-01
    • 2012-07-25
    • 1970-01-01
    • 2018-11-10
    • 2017-09-20
    • 2014-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多