- TensorFlow2.0和TensorFlow1.x有着较大的差异,不能直接兼容1.x的代码,但还是提供了TensorFlow1.x的API支持。
- TensorFlow2.0将原有的TensorFlow1.x的API整理到tensorflow.compat.v1包里去了。
当你要在TensorFlow2.0的环境下执行/开发TensorFlow1.x的代码,应做以下操作:
将
import tensorflow as tf
修改成
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior() #禁用TensorFlow 2.x行为
或是
import tensorflow.compat.v1 as tf
tf.disable_eager_execution() #禁止TensorFlow2默认的即时执行模式