禁用 `@tf.function` 装饰器进行调试?

Disabling `@tf.function` decorators for debugging?

在 TensorFlow 2 中,@tf.function decorator allows for Python functions to become TensorFlow graphs (more or less) and can lead to some performance improvements. However, when decorated this way, Python no longer traces the functions each time they run。这使得使用 Python 调试器调试函数变得更加困难。有没有办法暂时禁用所有 @tf.function 装饰器以便于调试?

您可以使用全局布尔变量 DEBUG 并将其应用于 @tf.function 中的 autograph 参数,如下所示:

import tensorflow as tf
DEBUG = False

@tf.function(autograph=not DEBUG)
def foo(x):
    return x + 1

否则,由于默认是autograph=True,不知道是否可以不修改源码

使用tf.config.run_functions_eagerly(True).