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

转载链接:https://blog.csdn.net/qq_40575024/article/details/105862550

收起

出现问题:

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.disable_eager_execution()
x = tf.constant([[1.0, 2.0]]) #定义一个 2 阶张量等于[[1.0,2.0]]
w = tf.constant([[3.0], [4.0]]) #定义一个 2 阶张量等于[[3.0],[4.0]]
y = tf.matmul(x, w) #实现 xw 矩阵乘法
print (y) #打印出结果
# 初始化所有变量,也就是上面定义的a/b两个变量
#tf.compat.v1.Session() as session
with tf.compat.v1.Session() as sess:
    print (sess.run(y))
当出现"AttributeError: module 'tensorflow' has no attribute 'Session'"的错误时,通常是因为使用了错误的`tensorflow`版本。在新版的`tensorflow`中,`Session`被移除并且不再被支持。相反,使用`tf.compat.v1.Session()`来创建会话对象。所以,解决这个问题的方法是将代码中的`tf.Session()`替换为`tf.compat.v1.Session()`。引用<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Tensorflow 2.0 - AttributeError: moduletensorflow‘ has no attributeSession](https://blog.csdn.net/YINTENAXIONGNAIER/article/details/125660868)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [AttributeError: module 'tensorflow.compat.v1' has no attribute '](https://download.csdn.net/download/qq_38766019/86272235)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [AttributeError: moduletensorflow‘ has no attributeSession](https://blog.csdn.net/qq_42095950/article/details/130015868)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值