抱歉标题不准确。以下是问题的详细说明:假设形状(?, 2)
的张量,例如,张量 T [[0,1], [0,2], [0,0], [1, 4], [1, 3], [2,0], [2,0], [2,0],[2,0]]
。如何计算每T[:, 0]
出现的零数。对于上面的示例,因为有[0,0]
和[2,0]
,答案是2。
更多例子:
[[0,1], [0,2], [0,1], [1, 4], [1, 3], [2,0], [2,0], [2,0],[2,0]]
(答案:1,因为[2,0]
)
[[0,1], [0,2], [0,1], [1, 4], [1, 3], [2,0], [2,0], [2,0],[2,0],[3,0]]
(答案:2,因为[2, 0]
和[3,0]
)
答案 0 :(得分:1)
如果我得到你想要的东西,问题是你在数据中有多少独特的“[X,0]对”。如果是这样,应该这样做:
import tensorflow as tf
x = tf.placeholder(shape=(None, 2), dtype=tf.int32)
indices = tf.where(tf.equal(x[:,1], tf.constant(0, dtype=tf.int32)))
unique_values, _ = tf.unique(tf.squeeze(tf.gather(x[:, 0], indices)))
no_unique_values = tf.shape(unique_values, out_type=tf.int32)
data = [ .... ]
with tf.Session() as sess:
no_unique = sess.run(fetches=[no_unique_values], feed_dict={x: data})
答案 1 :(得分:0)
这是我自己的解决方案
def get_unique(ts):
ts_part = ts[:, 1]
where = tf.where(tf.equal(0, ts_part))
gather_nd = tf.gather_nd(ts, where)
gather_plus = gather_nd[:, 0] + gather_nd[:, 1]
unique_values, _ = tf.unique(gather_plus)
return tf.shape(unique_values)[0]