Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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
Python 在`tf.GradientTape()的上下文中用`语句澄清条件`with``_Python_Tensorflow_Conditional Statements_With Statement - Fatal编程技术网

Python 在`tf.GradientTape()的上下文中用`语句澄清条件`with``

Python 在`tf.GradientTape()的上下文中用`语句澄清条件`with``,python,tensorflow,conditional-statements,with-statement,Python,Tensorflow,Conditional Statements,With Statement,我有以下代码: with tf.GradientTape() as tape: # some code in which the object tape doesn't appear 我意识到,只有在满足某些条件时,我才想向该磁带添加项目。所以我必须使用条件with语句。我找到了,但我不确定是否正确。以下代码是否正确 from contextlib import ExitStack (...) with tf.GradientTape() if condition else ExitS

我有以下代码:

with tf.GradientTape() as tape:
    # some code in which the object tape doesn't appear
我意识到,只有在满足某些条件时,我才想向该
磁带添加项目。所以我必须使用条件with语句。我找到了,但我不确定是否正确。以下代码是否正确

from contextlib import ExitStack
(...)
with tf.GradientTape() if condition else ExitStack() as tape:
    # some code in which the object tape doesn't appear