也许你已经安装好了tf2.0.0及以上版本,实话实说,走在前沿的人也会跌进很多坑中,在进行测试时,使用以下语句
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
出错,首先会停在Adding visible gpu devices: 0这一句,等待两分钟后返回错误,module 'tensorflow' has no attribute 'Session'
发现是tensorflow2.0以上版本移除了tf.Session(),改为了 tf.compat.v1.Session()
改完后运行,又报错,RuntimeError: The Session graph is empty. Add operations to the graph before calling run().
问题出在,teosorflow 2.0的工作机制,TensorFlow core v2 have enabled eager execution by default so doesn`t need to write
tf.compat.v1.Session() and use .run function,就是说2.0 版本开始,默认情况下已启用 eager execution,不需要编写
修改为以下
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
hello = tf.constant(