【问题标题】:Disabling `@tf.function` decorators for debugging?禁用“@tf.function”装饰器进行调试?
【发布时间】:2019-05-26 20:18:13
【问题描述】:

在 TensorFlow 2 中,@tf.function 装饰器允许 Python 函数(或多或少)成为 TensorFlow 图,并可以带来一些性能改进。但是,当以这种方式装饰时,Python no longer traces the functions each time they run。这使得使用 Python 调试器调试函数变得更加困难。有没有办法暂时禁用所有 @tf.function 装饰器以方便调试?

【问题讨论】:

    标签: python tensorflow tensorflow2.0


    【解决方案1】:

    【讨论】:

    • 在 TF 2.3 及更高版本中,这被替换为 tf.config.run_functions_eagerly(True)
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-01
      • 2019-09-17
      • 1970-01-01
      • 2013-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-19
      相关资源
      最近更新 更多