AttributeError: module ‘tensorflow‘ has no attribute ‘Session‘

*AttributeError: module ‘tensorflow’ has no attribute 'Session

*’)

原代码

`import tensorflow as tf
sess= tf.Session()
a = tf.constant(1)
b = tf.constant(2)
print(sess.run(a+b))`

这是一个最简单的tensorflow程序,运行之后

sess= tf.Session()
AttributeError: module 'tensorflow' has no attribute 'Session'

出现了这个结果,解决办法如下

import tensorflow as tf
tf.compat.v1.disable_eager_execution()
#保证sess.run()能够正常运行
sess = tf.compat.v1.Session()
a = tf.constant(1)
b = tf.constant(2)
print(sess.run(a+b))

在第二行加了一行代码

tf.compat.v1.disable_eager_execution()

然后将sess= tf.Session()
改为了
sess = tf.compat.v1.Session()
之后程序就可以正常运行

2021-08-14 20:58:18.317322: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1264]      0 
2021-08-14 20:58:18.317617: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1277] 0:   N 
2021-08-14 20:58:18.318185: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1418] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 2816 MB memory) -> physical GPU (device: 0, name: NVIDIA GeForce GTX 950M, pci bus id: 0000:0a:00.0, compute capability: 5.0)
3
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值