Tensorflow 中的集合交集

3

我希望检查一组给定的值是否包含在一个稀疏张量中。该稀疏张量称为标签,只有一个维度,包含一个id列表。

最终,这似乎是一个简单的集合交集问题,因此我尝试了以下解决方法。

sparse_ids = load_ids_as_sparse_tensor()
wanted_ids = tf.constant([34, 56, 12])
intersection = tf.sets.set_intersection(
    wanted_ids,
    tf.cast(sparse_ids.values, tf.int32)
)
contains_any_wanted_ids = tf.not_equal(tf.size(intersection), 0)

然而,我遇到了这个错误:
ValueError: Shape must be at least rank 2 but is rank 1 for 'DenseToDenseSetOperation' (op: 'DenseToDenseSetOperation') with input shapes: [3], [?].
你有什么想法吗?
1个回答

2
以下代码可以运行。但是,我不确定结果是否符合您的要求。
import tensorflow as tf
a = tf.constant([34, 56, 12])
b = tf.constant([56])
intersection = tf.sets.set_intersection(a[None,:],b[None,:])
sess=tf.Session()
sess.run(intersection)

输出:

SparseTensorValue(indices=array([[0, 0]], dtype=int64), values=array([56]), dense_shape=array([1, 1], dtype=int64))


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接