所以,假設我有一個這樣的張量:
[[0,18],
[1,19],
[2, 3],
[3,19],
[4, 18]]
我只需要使用 tensorflow 洗掉第二列中包含重復項的行。最終輸出應該是這樣的:
[[0,18],
[1,19],
[2, 3]]
uj5u.com熱心網友回復:
你應該能夠用tf.math.unsorted_segment_minand解決這個問題tf.gather:
import tensorflow as tf
x = tf.constant([[0,18],
[1,19],
[2, 3],
[3,19],
[4, 18]])
y, idx = tf.unique(x[:, 1])
indices = tf.math.unsorted_segment_min(tf.range(tf.shape(x)[0]), idx, tf.shape(y)[0])
result = tf.gather(x, indices)
print(result)
tf.Tensor(
[[ 0 18]
[ 1 19]
[ 2 3]], shape=(3, 2), dtype=int32)
這是對呼叫后發生的事情的簡單解釋tf.unique:

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/465895.html
