[学习笔记]module 'tensorflow' has no attribute 'Session'

import tensorflow as tf
hello_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)
作者:qy原文地址:https://segmentfault.com/a/1190000041914892

%s 个评论

要回复文章请先登录注册