2022年4月答复
从 2.2 版本开始,Tensorflow 支持使用以下方法在 2D 张量中查找唯一的 1-D 张量tf.raw_ops.UniqueV2
.
>>> import tensorflow as tf
>>> a = tf.constant([[1, 2, 1], [3, 4, 1], [5, 6, 1], [1, 2, 1]])
>>> y, idx = tf.raw_ops.UniqueV2(x=a, axis=[0])
>>> y
<tf.Tensor: shape=(3, 3), dtype=int32, numpy=
array([[1, 2, 1],
[3, 4, 1],
[5, 6, 1]], dtype=int32)>
>>> idx
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([0, 1, 2, 0], dtype=int32)>
检查docs https://www.tensorflow.org/api_docs/python/tf/raw_ops/UniqueV2更多细节。