tensorflow2 AttributeError: module 'tensorflow' has no attribute 'Session'

在更新到TensorFlow2后,遇到`AttributeError: module 'tensorflow' has no attribute 'Session'`的问题。原因是TF2移除了`tf.Session()`。解决方案包括使用`tf.compat.v1.Session()`或降级TF版本。注意,使用`tf.compat.v1.Session()`前可能需要添加`tf.compat.v1.disable_eager_execution()`,因为TF2默认启用eager execution模式。
摘要由CSDN通过智能技术生成

出现问题:

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

原因:

tensorflow2删除了 tf.Session()

解决方法:

tf.Session() 将改为tf.compat.v1.Session()
或者版本降级:pip install tensorflow==1.4

注意事项

tf.compat.v1.Session() 前加上tf.compat.v1.disable_eager_execution()。

tensorflow经典的方式是需要构建计算图,启动会话后,张量在图中流动进行计算。在tf 2.0最大的特色之一就在于eager execution,大大简化了之前这个繁琐的定义和计算过程。

eager execution有两个模式tf.compat.v1.enable_eager_execution()和tf.compat.v1.disable_eager_execution()。默认第一种,因此要改过来。

eg.

import tensorflow as tf #引入模块
tf.compat.v1
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值