gpt4 book ai didi

python - Tensorflow有类似python中set()的数据类型吗?

转载 作者:行者123 更新时间:2023-12-01 07:58:07 25 4
gpt4 key购买 nike

我想实现一个提取 Tensorflow 张量中所有不同元素的操作。在python中,我通常使用set(array),但我不知道Tensorflow中是否有像set这样的数据类型或任何功能相同,效果相同。

最佳答案

要从张量中获取所有唯一元素,请使用tf.unique()。首先,将张量 reshape 为 rank=1,然后应用 tf.unique():

import tensorflow as tf

tensor = tf.constant([[1., 2., 3.], [3., 4., 5.]], dtype=tf.float32)
flatten = tf.reshape(tensor, [-1])
unique = tf.unique(flatten)[0]

with tf.Session() as sess:
print(unique.eval()) # [1. 2. 3. 4. 5.]

关于python - Tensorflow有类似python中set()的数据类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55849331/

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