import tensorflow as tfhello_constant = tf.constant('Hello World!')with tf.Session() as sess: output=sess.run(hello_constant) print(output)
执行出错:
module 'tensorflow' has no attribute 'Session'
起因:tensorflow2没有Session模块了
批改一些代码就能跑起来了,代码外面做了正文:
import tensorflow as tf# 批改1:增加上面这行代码tf.compat.v1.disable_eager_execution()hello_constant = tf.constant('Hello World!')# 批改2:tf.Session()批改为tf.compat.v1.Session()with tf.compat.v1.Session() as sess: output=sess.run(hello_constant) print(output)