在 TensorFlow 中,我可以使用 tf.bincount 获取数组中每个元素的计数:
x = tf.placeholder(tf.int32, [None])
freq = tf.bincount(x)
tf.Session().run(freq, feed_dict = {x:[2,3,1,3,7]})
这返回
Out[45]: array([0, 1, 1, 2, 0, 0, 0, 1], dtype=int32)
有没有办法在二维张量上做到这一点? IE。
x = tf.placeholder(tf.int32, [None, None])
freq = tf.axis_bincount(x, axis = 1)
tf.Session().run(freq, feed_dict = {x:[[2,3,1,3,7],[1,1,2,2,3]]})
返回
[[0, 1, 1, 2, 0, 0, 0, 1],[0, 2, 2, 1, 0, 0, 0, 0]]