tensorflow2.0版本及之后的版本在使用Session模块时报错:AttributeError: module ‘tensorflow’ has no attribute ‘Session’
AttributeError: module 'tensorflow' has no attribute 'Session'
错误的意思是tensortflow模块没有Session属性,后来查阅资料发现,tensorflow2.0版本中的确没有Session这个属性。
如果安装的是tensorflow2.0版本又想利用Session属性,可以在tf.Session()文件头添加导入模块处,将import tensorflow as tf更改为:

import tensorflow.compat.v1 as tf

就可以正常运行了。

如果运行后出现如下错误:
RuntimeError: tf.placeholder() is not compatible with eager execution.
查阅资料发现,原因是2.0与1.0版本不兼容。这是因为在运行**tf.compat.v1.placeholder(dtype, shape = None, name = None)**的时候急切执行了这条语句,但是我们一般都是在一个Session前先去定义placeholder,但是不会去执行,然后再在Sesion上下文管理器中去传入我们的数据,然后执行。
tensorflow的官网对disable_eager_execution()方法是这样解释的:
翻译过来为:此函数只能在创建任何图、运算或张量之前调用。它可以用于从TensorFlow 1.x到2.x的复杂迁移项目的程序开头。这里给出一个方法(对我有效)

在程序开始部分添加以下代码:

tf.compat.v1.disable_eager_execution()

例子
Error

import tensorflow as tf
import numpy as np
# tf.compat.v1.disable_eager_execution()
x = tf.compat.v1.placeholder(tf.float32, shape=(1024, 1024))
y = tf.matmul(x, x)with tf.compat.v1.Session() as sess:print(sess.run(y))# rand_array = np.random.rand(1024, 1024)# print(sess.run(y, feed_dict={x: rand_array}))

Correct

import tensorflow as tf
import numpy as np
tf.compat.v1.disable_eager_execution()
x = tf.compat.v1.placeholder(tf.float32, shape=(1024, 1024))
y = tf.matmul(x, x)with tf.compat.v1.Session() as sess:# print(sess.run(y))rand_array = np.random.rand(1024, 1024)print(sess.run(y, feed_dict={x: rand_array}))

参考资料:
1、eager execution not working with placeholders #18165

tensorflow2.0及以上版本在使用Session模块时报错:AttributeError: module ‘tensorflow‘ has no attribute ‘Session‘ 已解决相关推荐

  1. AttributeError: module 'tensorflow' has no attribute 'Session'错误解决

    Ubuntu刚安装好Tensorflow以后运行 import tensorflow as tf tf.Session() 报错AttributeError: module 'tensorflow' ...

  2. AttributeError: module ‘tensorflow‘ has no attribute ‘Session‘错误解决

    AttributeError: module 'tensorflow' has no attribute 'Session'错误解决 根据网上教程安装tensorflow环境时,最后都会有个测试代码用 ...

  3. AttributeError: module 'tensorflow' has no attribute 'Session'.解决办法

    # 定义计算图 tens1 = tf.constant([1,2,3])# 创建一个会话 sess = tf.Session()# 使用这个创建好的会话来得到关心的运算的结果.比如可以调用 sess. ...

  4. AttributeError: module 'tensorflow' has no attribute 'Session'

    tf.Session()是旧版本的语法,如果要跑,加入下面代码 https://github.com/tensorflow/tensorflow/issues/34658 import tensorf ...

  5. module 'tensorflow' has no attribute 'Session'

    AttributeError: module 'tensorflow' has no attribute 'Session'错误解决 原创陆羽飞 最后发布于2019-10-07 13:32:07 阅读 ...

  6. 解决Tensorflow2.0出现:AttributeError: module 'tensorflow' has no attribute 'get_default_graph'的问题

    问题描述 在使用tensorflow2.0时,遇到了这个问题: AttributeError: module 'tensorflow' has no attribute 'get_default_gr ...

  7. tf2.0环境下“module ‘tensorflow‘ has no attribute ‘log‘”的解决办法

    写在这里的初衷,一是备忘,二是希望得到高人指点,三是希望能遇到志同道合的朋友. 目录 一.问题 二.根本原因 三.解决办法 方法一 方法二 一.问题 目前,尝试着通过tensorflow对医学图像进行 ...

  8. tensorflow版本问题导致的错误AttributeError: module ‘tensorflow‘ has no attribute ‘***‘

    问题解决--AttributeError: module 'tensorflow' has no attribute 'placeholder' - yuanyuanWu11 - 博客园 tensor ...

  9. 解决Tensorflow2.0出现:AttributeError: module ‘tensorflow‘ has no attribute ‘get_default_graph‘的问题

    https://blog.csdn.net/public669/article/details/99686151

最新文章

  1. electron 剪贴板 截图_用electron开发了一个屏幕截图工具
  2. 【OGG】OGG的单向复制配置-支持DDL(二)
  3. 制度化规范化标准化精细化_精细化管理ORTCC模型在管理中的运用(下)
  4. int类型存小数 mysql_MySQL面试题-数据类型
  5. php分解质因数,用PHP如何实现将一个整数分解为质因数的积?
  6. java对外查询接口注意的地方_Java接口注意点
  7. Java版二叉树遍历,查找,顺序化存储代码实现
  8. 基于TCP协议用多线程实现并发服务器,实现思路、算法和demo
  9. PaddleDetection的学习笔记
  10. C# WPD PortableDeviceApiLib获取便携设备列表
  11. [NOIP2000]方格取数
  12. python基础--面向对象之绑定非绑定方法
  13. 基于C# winform的操作系统课程设计:SPOOLING假脱机输入输出技术模拟
  14. Beaglebone Black - 准备
  15. 常用的JavaScript位操作(Bitwise)
  16. Oracle 层次查询、递归
  17. 方法: 跳转App Store更新你应用的URL究竟该怎么写
  18. java操作excel的工具
  19. EBC动作片 记录EBC 的 景 和 人
  20. Python之面相对象-封装 多态 约束 super()

热门文章

  1. 未来客:人人都可以构建软件或系统
  2. CocoaPods打包静态库
  3. 记录使用IDEA部署Tomcat时提示错误:the selected directory is not a TomEE home
  4. JQ中使用FormData+Ajax发送请求及使用express接收处理FormData数据
  5. 漫谈 | “黎曼猜想”和区块链加密算法到底有什么关系?
  6. 常用sql 全记录(添加中)
  7. Linux 如何通过命令仅获取IP地址
  8. 设计模式之:深入浅出 java 单例模式(Singleton)
  9. 计算机单招六百分好考吗,单招分数线一般多少 单招考过的几率有多少
  10. OpenJudge NOI 1.7 28:单词倒排