本文介绍了确定一个值是否在 TensorFlow 的集合中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tf.logical_ortf.logical_andtf.select 函数非常有用.

The tf.logical_or, tf.logical_and, and tf.select functions are very useful.

然而,假设您有值 x,并且您想查看它是否在 set(a, b, c, d, e) 中.在 python 中,您只需编写:

However, suppose you have value x, and you wanted to see if it was in a set(a, b, c, d, e). In python you would simply write:

if x in set([a, b, c, d, e]):
  # Do some action.

据我所知,在 TensorFlow 中执行此操作的唯一方法是将tf.logical_or"与tf.equal"一起嵌套.我在下面仅提供了此概念的一次迭代:

As far as I can tell, the only way to do this in TensorFlow, is to have nested 'tf.logical_or' along with 'tf.equal'. I provided just one iteration of this concept below:

tf.logical_or(
    tf.logical_or(tf.equal(x, a), tf.equal(x, b)),
    tf.logical_or(tf.equal(x, c), tf.equal(x, d))
)

我觉得在 TensorFlow 中一定有更简单的方法来做到这一点.有吗?

I feel that there must be an easier way to do this in TensorFlow. Is there?

推荐答案

看看这个相关问题:计数真"的数量;布尔张量中的值

您应该能够构建一个由 [a, b, c, d, e] 组成的张量,然后使用 tf.equal(.) 检查是否有任何行等于 x

You should be able to build a tensor consisting of [a, b, c, d, e] and then check if any of the rows is equal to x using tf.equal(.)

这篇关于确定一个值是否在 TensorFlow 的集合中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 21:49