gpt4 book ai didi

tensorflow - 两个不同长度的张量之间的交集

转载 作者:行者123 更新时间:2023-12-03 19:31:27 25 4
gpt4 key购买 nike

我有 tensorflow 的情况。我想找到两个具有不同形状的二维张量的交集。

例子:

object_ids_  [[0 0]
[0 1]
[1 1]]

object_ids_more_07_ [[0 0]
[0 1]
[0 2]
[1 0]
[1 2]]

我正在寻找的输出是:
[[0,0], 
[0,1]]

我遇到了“tf.sets.set_intersection”,tensorflow 页面: https://www.tensorflow.org/api_docs/python/tf/sets/set_intersection

但是不能对不同形状的张量执行它。我发现的另一个实现是:

Find the intersection of two tensors. Return the sorted, unique values that are in both of the input tensors

但是很难为 2D 张量复制它。

任何帮助将不胜感激,谢谢

最佳答案

一种方法是到 subtract->abs->sum所有组合,然后获取与零匹配的索引。可以使用 broadcasting 来实现.

a = tf.constant([[0,0],[0,1],[1,1]])
b = tf.constant([[0, 0],[0, 1],[0,2],[1, 0],[1, 2]])

find_match = tf.reduce_sum(tf.abs(tf.expand_dims(b,0) - tf.expand_dims(a,1)),2)

indices = tf.transpose(tf.where(tf.equal(find_match, tf.zeros_like(find_match))))[0]

out = tf.gather(a, indices)

with tf.Session() as sess:
print(sess.run(out))
#Output
#[[0 0]
#[0 1]]

关于tensorflow - 两个不同长度的张量之间的交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50848323/

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