Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查元素是否包含在张量中(带';@tf.function';)的Python Tensorflow2)_Python_Tensorflow - Fatal编程技术网

检查元素是否包含在张量中(带';@tf.function';)的Python Tensorflow2)

检查元素是否包含在张量中(带';@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

我要检查元素是否包含在张量中,我会遇到问题

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 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)中使用结果。