展开批次中索引2处的tensorflow计数数为0

展开批次中索引2处的tensorflow计数数为0,tensorflow,Tensorflow,抱歉标题不准确。下面是问题的详细描述:假设一个形状为?、2的张量,例如张量T[[0,1]、[0,2]、[0,0]、[1,4]、[1,3]、[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的张量,例如张量T[[0,1]、[0,2]、[0,0]、[1,4]、[1,3]、[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],[3,0]]回答:2,因为[2,0]和[3,0]


如果我得到了你想要的,问题是数据中有多少唯一的[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})

这是我自己找到的解决办法

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]