当前位置: 代码迷 >> 综合 >> tensorflow1.0和tensorflow2.0兼容问题
  详细解决方案

tensorflow1.0和tensorflow2.0兼容问题

热度:22   发布时间:2023-12-23 19:02:44.0

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)
  相关解决方案