Tensorflow 是否有一个类似bash uniq的tf.unique版本?

Tensorflow 是否有一个类似bash uniq的tf.unique版本?,tensorflow,Tensorflow,我希望将张量中不连续的相同元素指定为不同的idx(即tf.unique的第二个输出)。有没有办法做到这一点?谢谢。好的,今天我的大脑比平时少了雾,这里有一个快速的解决方案: x = [1, 1, 2, 2, 3, 2, 1] def bash_unique_with_counts(vector): segment_starts = tf.concat( [[1], tf.to_int32(tf.not_equal(vector[:-1], vector[1:]))], a

我希望将张量中不连续的相同元素指定为不同的idx(即tf.unique的第二个输出)。有没有办法做到这一点?谢谢。

好的,今天我的大脑比平时少了雾,这里有一个快速的解决方案:

x = [1, 1, 2, 2, 3, 2, 1]
def bash_unique_with_counts(vector):
    segment_starts = tf.concat(
        [[1], tf.to_int32(tf.not_equal(vector[:-1], vector[1:]))], axis=0)
    new_vector = tf.cumsum(segment_starts, exclusive=False)
    return tf.unique_with_counts(new_vector)

y, idx, count = bash_unique_with_counts(x)
print(tf.Session().run(count))
[2 2 1]