TensorFlow 2.6调用tf.enable_eager_execution报无该属性如何解决
TensorFlow 2.6
enable_eager_execution属性报错解决方案 报错根因
tf.enable_eager_execution()为TensorFlow 1.x专属接口,TensorFlow 2.0及以上版本已默认全局开启急切执行,该接口被正式移除,直接调用会抛出AttributeError。tf.executing_eagerly()返回False说明当前运行环境的急切执行被手动关闭,通常由代码中调用了tf.compat.v1.disable_eager_execution()、tf.compat.v1.disable_v2_behavior(),或显式创建v1版本Session的操作触发,部分基于TF1.x开发的第三方依赖也会默认关闭急切执行。
修复步骤
- 移除所有代码中对
tf.enable_eager_execution()的调用,TensorFlow 2.x版本无需手动调用该接口开启急切执行。 - 全局排查代码及依赖,删除所有关闭急切执行、启用v1图模式的操作,即可恢复默认急切执行状态。
- 若代码基于TensorFlow 1.x开发,暂时无法适配2.x版本,可在所有TensorFlow操作前添加兼容模式声明,即可正常使用1.x接口:
import tensorflow.compat.v1 as tf tf.disable_v2_behavior() - 修复完成后可运行以下代码验证执行状态:
import tensorflow as tf print(tf.__version__) print(tf.executing_eagerly()) # 返回True则说明急切执行已正常开启
内容的提问来源于stack exchange,提问作者Steve
相关产品推荐
相关产品推荐

