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