检查元素是否包含在张量中(带';@tf.function';)的Python Tensorflow2)
我要检查元素是否包含在张量中,我会遇到问题 e、 g.1检查元素是否包含在张量中(带';@tf.function';)的Python Tensorflow2),python,tensorflow,Python,Tensorflow,我要检查元素是否包含在张量中,我会遇到问题 e、 g.1 def foo(a): if 5 in tf.constant([5, 7, 9]): tf.print(a) foo(2) # you'll get '2', and no erros e、 g.2 @tf.function def foo(a): if 5 in tf.constant([5, 7, 9]): tf.print(a) foo(2) # you'll get er
def foo(a):
if 5 in tf.constant([5, 7, 9]):
tf.print(a)
foo(2)
# you'll get '2', and no erros
e、 g.2
@tf.function
def foo(a):
if 5 in tf.constant([5, 7, 9]):
tf.print(a)
foo(2)
# you'll get erros like "TypeError: argument of type 'Tensor' is not iterable"
显然,添加@tf.function后情况有所不同。如果你能帮我解决这个问题,我将不胜感激 使用@tf.function装饰函数时,它将以图形模式运行。在图形模式下,您不能迭代tf.Tensor(这就是您在if语句中所做的)。谢谢您的回复!但是有什么方法可以实现一个tf.张量呢?这取决于你想做什么。在您的示例中,可以使用tf.reduce_any(tf.equal(5,tf.constant([5,7,9]),并在条件语句(tf.cond)中使用结果。