TensorFlow module ‘tensorflow‘ has no attribute ‘Session‘ 问题解决

tensorflow框架搭建问题解决 进入CMD 环境中,python -m pip install tensorflow(多次尝试)(pip3 install tensorflow) 安装成功! 此次安装没有安装CUDA,tensorflow的计算在单机CPU下进行。 或者用conda install tensorflow-gpu安装 使用conda install tensorflow-gpu安装tensorflow时未指定版本,导致安装了tensorflow2.1版本。而tf.Session是1.X版本tensorflow中的代码 阅读详情

问题描述

在初次尝试tensorflow 的时候遇到类似下面的问题:
module ‘tensorflow’ has no attribute ‘placeholder’
module ‘tensorflow’ has no attribute 'Session’

问题解决

导致该问题的原因是:
tensorflow的 V1 版本和V2版本不能直接兼容,需要在其前置中加入些代码设置环境:

import tensorflow.compat.v1 as tf

这个时候,如果你使用了placeholder的话,他又会报出错误:
RuntimeError: tf.placeholder() is not compatible with eager execution.
兼容问题又出现,那么再加入下述代码进行兼容设置:

tf.compat.v1.disable_eager_execution()

此外,在我的代码中还出现了 :ModuleNotFoundError: No module named ‘tensorflow.examples.tutorials’ 的错误。
这是由于我使用了一个MNIST的网络数据集,(from tensorflow.examples.tutorials.mnist import input_data)
这个数据集的即时下载方式已经改变,因此,如果想要拿到tutorials内部的函数,需要将该文件下载到本地,然后放在项目的根目录下。但是tutorials文件夹的下载在github上已经不再支持,因此在这里提供一个下载源,在我的优快云账号的资源里。
下面附上当时测试的代码,用来实现数字的图像识别的深度学习训练。

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow.compat.v1 as tf
print (tf.__path__)
tf.compat.v1.disable_eager_execution()
from tensorflow.examples.tutorials.mnist import input_data
DATA_DIR = '/data'

NUM_STEPS = 1000

MINIBATCH_SIZE = 100

# 直接使用内置方法即时检索数据集,ready_data_sets()方法将MNIST数据集下载到本地,第二个参数为读取标签的方式
data = input_data.read_data_sets(DATA_DIR, one_hot = True)

# 占位符和变量的定义, 图像本身x是一个占位符,none表示不指定每次使用的图片数量,784是图像的像素点个数,我们所提取的图像为 28×28
x = tf.placeholder(tf.float32, [None, 784])
print(x)
w = tf.Variable(tf.zeros([784, 10]))
print(w)
# y_true 和y_pred 分别表示真实和预测的标签
y_true = tf.placeholder(tf.float32, [None, 10])
y_pred = tf.matmul(x, w)

# 使用测量相似度的方法:交叉熵,也叫损失函数
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_pred, labels=y_true))
# 通过最小化损失函数的方式使得整体损失下降,得到最优化结果(梯度下降法)
gd_step  = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

# 定义评估步骤来测试模型的准确率
correct_mask = tf.equal(tf.argmax(y_pred,1), tf.argmax(y_true, 1))
accuracy = tf.reduce_mean(tf.cast(correct_mask, tf.float32))

with tf.Session() as sess:
    # Train
    # 初始化所有参数
    sess.run(tf.global_variables_initializer())
    for _ in range(NUM_STEPS):
        # 使用 MINIBATCH_SIZE常量控制每一步的样本数量
        batch_xs, batch_ys = data.train.next_batch(MINIBATCH_SIZE)
        # 模型学习过程
        sess.run(gd_step, feed_dict = {x:batch_xs, y_true: batch_ys})
    # Test 测试准确率
    ans = sess.run(accuracy, feed_dict = {x: data.test.images, y_true: data.test.labels})
print ("Accuracy: {:.4}".format(ans*100))
AttributeError: module ‘tensorflow‘ has no attribute ‘Session‘ 原因:tensorflow2.x不支持tensorflow1.x的session。代码中加入compat.v1就可以使用v1来兼容V1。 阅读详情

相关推荐

module 'tensorflow' has no attribute 'Session'

AttributeError: module 'tensorflow' has no attribute 'Session'错误解决 原创陆羽飞 最后发布于2019-10-07 13:32:07 阅读数 16728 收藏 展开 Ubuntu刚安装好Tensorflow以后运行 import tensorflow as tf tf.Session() 报错AttributeError: modu...

jacke121的专栏 1万+

tensorflow使用Session模块时报错:AttributeError: module ‘tensorflow‘ has no attribute ‘Session‘,已解决

安装好tensorflow2.0之后,当使用Session时,报错AttributeError: module ‘tensorflow’ has no attribute ‘Session’: 源代码: 1 2 3 4 5 6 7 8 9 import tensorflow as tf import os os.environ[“CUDA_VISIBLE_DEVICES”]=“0” a=tf.constant(2) b=tf.constant(3) with tf.Session() as sess: pr

UCAS2019的博客 1万+

告别环境配置噩梦:VSCode + MinGW 下 OpenGL (glad/glm/glfw) 一站式配置指南(含常见错误排查)

本文提供了一份详细的VSCode与MinGW下OpenGL开发环境配置指南,涵盖glad、glm和glfw三大核心库的安装与调试。通过逐步指导解决常见错误,帮助开发者快速搭建高效的OpenGL开发环境,特别适合初学者避免环境配置中的常见陷阱。

weixin_42557803的博客 491

讲解module ‘tensorflow‘ has no attribute ‘Session‘

通过阅读本篇博客,你应该已经了解了"module 'tensorflow' has no attribute 'Session'"错误的原因以及解决方案。记住,在TensorFlow 2.0版本及之后的版本中,Session对象已被弃用,你需要使用新的命令式编程风格来编写代码。如果有需要,可以将你的代码迁移到Eager Execution模式,或者考虑回退到TensorFlow 1.x版本。祝你使用TensorFlow开发愉快!

牛肉胡辣汤 2773

AMP架构下的核间通信:RPMSG与共享内存原理详解

本文以RK3568等异构多核硬件为例,深入浅出解析了AMP架构下核间通信的核心逻辑,阐明RPMSG并非替代共享内存,而是以共享内存为数据载体、硬件信箱为中断通知工具、VirtIO为数据管理规则,构建的标准化高层协议与框架,为开发者提供简洁API,实现不同操作系统核心间高效通信。

qq_61335271的博客 2051

从CAD到完美网格:HyperMesh几何清理全流程指南(2023最新版)

本文详细介绍了2023最新版HyperMesh在几何清理和网格划分方面的全流程操作指南。从CAD导入、几何修复到中面提取和网格生成,系统性地分享了实战验证的解决方案,帮助工程师高效处理复杂几何体,提升有限元模型质量。特别针对汽车和航空航天领域的常见问题,提供了智能化工具和优化技巧。

weixin_29306011的博客 228

module ‘tensorflow‘ has no attribute ‘ConfigProto‘/‘Session‘问题的解决

查看Tensorflow是GPU还是CPU可在终端输入以下代码 import tensorflow as tf a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a') b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name=...

sereasuesue的博客 1826

Matlab TreeBagger实战:用随机森林预测房价的5个关键步骤(附完整代码)

本文详细介绍了使用Matlab的TreeBagger工具包构建随机森林模型预测房价的完整流程。通过波士顿房价数据集实战,涵盖数据预处理、模型构建、参数调优、特征重要性分析及高级应用(如分位数回归)等5个关键步骤,并附有完整代码,帮助读者快速掌握这一强大的回归分析工具。

ol789012的博客 445

解决tensorflow报错:AttributeError: module ‘tensorflow.keras.backend‘ has no attribute ‘get_session‘ 问题

欢迎大家关注笔者,你的关注是我持续更博的最大动力 原创文章,转载告知,盗版必究 解决tensorflow报错:AttributeError: module 'tensorflow.keras.backend' has no attribute 'get_session' 问题 文章目录:1 错误原因分析2 错误解决方式 先说明以下我自己的环境: Ubuntu18.04 tensorflow-gpu2.2.0 1 错误原因分析 我是在代码中调用了sess = tf.keras.backen..

求真、务实 3万+

ICC2如何写出floorplan信息

不用-exclude 的话rows_tracks / vias /blockages / routing_rules信息都会写出去,即便用了-include {macro ports}也不行,所以不介意这些东西的话可以用以下两种种def方式去写floorplan。write_floorplan用法和write_def一样,因为写出来的文件夹里就包含def,用write_floorplan的好处和innovus saveFPlan一样,都能写出voltage area和bound信息。

m0_61544122的博客 1073

【WRF模拟】如何查看 WPS 的输入静态地理数据(二进制格式)?

如何查看 WPS 的输入静态地理数据(二进制格式)?

WW、forever的博客 638

解决报错问题,AttributeError: module ‘tensorflow‘ has no attribute ‘Session’

解决报错问题,AttributeError: module ‘tensorflow‘ has no attribute ‘Session'

qq_58728069的博客 1439

杰理JL701N内置flash录音配置教程

本文详细介绍了内置Flash录音功能的配置方法。主要内容包括:1) 基础模式配置(开启音乐/录音模式、Flash录音宏定义);2) 录音参数设置(格式基准值、存储区域定义、容量配置);3) MP3编码特殊处理;4) 下载时的Flash格式化操作;5) 录音开关和播放接口的使用说明。配置过程涉及多个关键文件的修改,并强调存储容量的精确分配。

qq_42014563的博客 599

Pycharm运行时关于AttributeError: module ‘tensorflow‘ has no attribute ‘Session‘的问题解决

Pycharm运行时关于AttributeError: module 'tensorflow' has no attribute 'Session'的问题解决

z1225353355的博客 819

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

TensorFlow版本问题的解决方案(module ‘tensorflow‘ has no attribute ×××、The Session graph is empty等)

假设有以下代码: import numpy as np import h5py import matplotlib.pyplot as plt import tensorflow as tf from tensorflow.python.framework import ops import tf_utils import time np.random.seed(1) y_hat = tf.constant(36, name='y_hat')# 定义y_hat为固定值36 y = tf.constan

qq_41409120的博客 763

贝叶斯统计 [韦来生编]

贝叶斯(Bayes)统计是近几十年来迅速发展起来的数理统计的一个重要分支。贝叶斯方法与经典统计方法的主要不同之处是进行统计推断时除了利用样本信息外,还要利用参数的先验信息,因此可以提高统计推断或统计决策的效果。它在经济、金融、生物、医学、自然科学和社会科学等许多领域具有广泛的应用。贝叶斯方法的研究已渗透到了统计学的几乎所有领域。作者在给中国科学技术大学概率论与数理统计专业本科生开设的“数理统计”课程中有一章专门讲授“贝叶斯方法和统计决策理论”,近十年来也给中国科学技术大学概率统计专业研究生开设过几次“贝叶斯分析”课。本书是在对过去讲稿的内容作了适当的增减和调整的基础上完成的。  本书共分六章。第1章是绪论,介绍了贝叶斯统计的若干基本概念,同时对必要的数理统计的基础知识有重点地作了回顾。第二章介绍了确定先验分布的若干可供选择的方法。第三章介绍了几类常见统计模型参数的后验分布的主要结果和计算方法。第四章和第五章分别介绍了“贝叶斯统计推断”和“贝叶斯统计决策”的内容。第六章介绍了贝叶斯统计计算的若干方法,包括蒙特卡洛方法和马尔可夫链蒙特卡洛(MCMC)方法以及相关统计软件的简介。本书的每一章附右大量的例题和习题,书末附有常用的几个表格和部分习题解答供读者查用。  大约可在54学时内讲授本书的第1章至第六章的主要内容,教师可根据学时需要选讲本书第1章至第六章的部分内容。书中标“*”号的章节可略去不讲,留给读者作为阅读材料。第六章例题中的R代码和部分例题的数据文件等可从作者个人主页下载。  作者在编写本书的过程中参考了J.O.Berger教授的《统计决策论及贝叶斯分析》和茆诗松教授编写的《贝叶斯统计》等书中的一些内容,以及《贝叶斯分析》一书中由张伟平博士编写的有关贝叶斯统计计算的一些内容。在此表示衷心感谢。  本书编写准备过程中,中国科学技术大学统计与金融系的几个研究生帮助完成了本书部分中文的录入和排版,作者对他们的辛勤工作表示真诚的感谢。高等教育出版社为本书的出版给予了大力支持。在此一并致谢。

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

出现==AttributeError: module ‘tensorflow’ has no attribute ‘Session’==因为在新的Tensorflow 2.0版本中已经移除了Session这一模块 新代码写法: import tensorflow as tf tf.compat.v1.Session()

Strange_程序员的的博客 706

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

AttributeError: module ‘tensorflow._api.v1.compat‘ has no attribute ‘v1‘

AttributeError: module ‘tensorflow._api.v1.compat’ has no attribute ‘v1’ 猜测原因:TensorFlow不同版本的API有出入,用法不兼容 解决:修改API即可。 将代码 : config = tf.compat.v1.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.8 sess = tf.compat.v1.session(config=c

向来萧瑟也无畏 9678

tensorflow使用报错:AttributeError: module ‘tensorflow‘ has no attribute ‘Session‘

tensorflow使用报错:AttributeError: module 'tensorflow' has no attribute 'Session' 安装完tensorflow后运行: import tensorflow as tf tf.Session() 报错:AttributeError: module ‘tensorflow’ has no attribute ‘Session’ 然后修改为: tf.compat.v1.Session() ...

qq_36333776的博客 897

报错:AttributeError: module ‘tensorflow‘ has no attribute ‘session‘

AttributeError: module ‘tensorflow’ has no attribute ‘session’ 出现问题: AttributeError: module 'tensorflow' has no attribute 'session' 解决方法: 方法一: 将 import tensorflow as tf 改为 import tensorflow.compat.v1 as tf tf.disable_v2_behavior() 方法二: 可能是程序中session的拼写

weixin_45305612的博客 971

Python错误:AttributeError: module ‘tensorflow’ has no attribute ‘Session’(已解决)

Python错误:AttributeError: module ‘tensorflow’ has no attribute ‘Session’(已解决) Windows刚安装好Tensorflow以后运行 import tensorflow as tf hello = tf.constant('hello world') sess = tf.Session() print(sess.run(hello)) 结果报错 AttributeError T

qq_40241985的博客 2945

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

关注微信公共号:小程在线 关注优快云博客:程志伟的博客 源代码: import tensorflow as tf reset_graph() x = tf.Variable(3, name="x") y = tf.Variable(4, name="y") f = x*x*y + y + 2 sess = tf.Session() sess.run(x.initializer) sess.run(y.initializer) result = sess.run(f) print

程志伟的博客 263

AttributeError: module ‘tensorflow‘ has no attribute ‘session‘的解决方案

待检测程序: import tensorflow as tf hello =tf.constant('hello, tensorflow') #print('Hello python') sess = tf.session() 问题描述,仍旧接着上一篇,没想到,解决了cudart64_110.dllnot found的问题又出现了AttributeError: module 'tensorflow' has no attribute 'session'的问题。 经网络查询资料,https:/.

阅读修身 294

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

运行sess=tf.Session()报错,可以把这一句改成 tf.compat.v1.disable_eager_execution() sess = tf.compat.v1.Session() 参考:AttributeError: module 'tensorflow' has no attribute 'Session'.解决办法_MclarenSenna的博客-优快云博客

小汤圆的博客 540
上一篇: VBA 指定某列相同的内容合并,对应的其它列内容相加
下一篇: HTML+JS 特效- 浮动气泡
Volavion
博客等级 码龄9年 3788粉丝 36原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Volavion

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值