gpt4 book ai didi

python - Tensorflow:如何使 tf.unique 的返回值与输入的大小相同

转载 作者:太空宇宙 更新时间:2023-11-04 09:56:55 25 4
gpt4 key购买 nike

根据 https://www.tensorflow.org/api_docs/python/tf/unique , tf.unique(x) 返回一个元组 (y, idx), y 的形状是 (?, ) 期间未知构建时间。无论如何,我可以填充 y 以匹配输入大小 x 吗?

例如,

# tensor 'x' is [1, 1, 2, 4, 4, 4, 7, 8, 8]
y, idx = unique(x)
y ==> [1, 2, 4, 7, 8]
idx ==> [0, 0, 1, 2, 2, 2, 3, 4, 4]

我想让 y = [1, 2, 4, 7, 8, 0, 0, 0, 0]

最佳答案

您可以在调用tf.unique 后使用tf.pad 来填充零。

x = tf.placeholder(tf.int32, shape=(None))
y, idx = tf.unique(x)
y = tf.pad(y,[[0,(tf.shape(x) - tf.shape(y))[0]]])

sess = tf.InteractiveSession()
print(sess.run(y, {x:np.random.randint(0,10, (10), dtype=np.int32)}))

关于python - Tensorflow:如何使 tf.unique 的返回值与输入的大小相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45428557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com