tensorflow1.0和tensorflow2.0兼容问题
方法一
出现The Session graph is empty. Add operations to the graph before calling run().
报错时
# 将import tensorflow as tf改成下面两条语句即可
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
或者
出现module 'tensorflow' has no attribute 'enable_eager_execution'
报错时
# 将import tensorflow as tf改成下面两条语句即可
import tensorflow.compat.v1 as tf
tf.compat.v1.disable_eager_execution()
方法二
对应的语句替换
tensorflow1.0 | tensorflow2.0 |
---|---|
tf.truncated_normal() |
tf.random.truncated_normal() |
tf.placeholder() |
tf.compat.v1.placeholder () |
tf.train.AdamOptimizer(alpha).minimize(self.cost) |
tf.optimizers.Adam(alpha).minimize(self.cost) |