发生此问题的版本
我使用的是tensorflow 1.15.0(python3.7.8,不过这个与python版本应该是无关的),根据查找的资料, 2.0 及以上的版本应该都是不直接包含这个模块的。因此如果直接运行以下代码会发生报错。
t0 = tf.constant(3, dtype=tf.int32)
sess = tf.Session()
print(sess.run(t0))
报错:
sess = tf.Session()
AttributeError: module 'tensorflow' has no attribute 'Session'
解决方案:
tf.compat.v1.disable_eager_execution()
sess = tf.compat.v1.Session()
t2 = tf.constant('hello!', dtype=tf.string)
print(sess.run(t2))