ID-xuejun 2019-12-01 21:21 采纳率: 57.1%
浏览 4017
已采纳

Tensorflow 测试helloWorld,显示 The session graph is empty

准备学Tensorflow ,win10装好了环境,Anaconda +pycharm+tensorflow,
准备测试简单的hello world程序,结果显示“The session graph is empty”,是什么问题呀?大神们帮忙解决一下!

原始代码是

import tensorflow as tf
a = tf.constant("Hello,world!")
sess = tf.Session()
print(sess.run(a))
sess.close()

这个显示错误:**module 'tensorflow' has no attribute 'Session'**

后按照网上提示,只有tensorflow 1 有,于是改成了下面的:

import tensorflow as tf
a = tf.constant("Hello,world!")
sess = tf.compat.v1.Session()
print(sess.run(a))
sess.close()

这个还是有错误,显示“The session graph is empty”
Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Traceback (most recent call last):
File "C:/Users/Dell/PycharmProjects/Helloworld/helloWorldTen.py", line 5, in
print(sess.run(a))
File "E:\Anaconda3\lib\site-packages\tensorflow_core\python\client\session.py", line 956, in run
run_metadata_ptr)
File "E:\Anaconda3\lib\site-packages\tensorflow_core\python\client\session.py", line 1105, in _run
raise RuntimeError('The Session graph is empty. Add operations to the '
RuntimeError: The Session graph is empty. Add operations to the graph before calling run().

  • 写回答

4条回答 默认 最新

  • 小布丁jajaja 2020-04-15 19:47
    关注

    import tensorflow as tf
    tf.compat.v1.disable_eager_execution() #保证sess.run()能够正常运行
    hello = tf.constant('hello,tensorflow')
    sess= tf.compat.v1.Session()#版本2.0的函数
    print(sess.run(hello))
    -------这样就解决了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 提问应符合社区要求 9月27日
  • 已采纳回答 8月30日

悬赏问题

  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题
  • ¥15 Selenium+docker Chrome不能运行
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到
  • ¥15 请教一下,openwrt如何让同一usb储存设备拔插后设备符号不变?
  • ¥50 使用quartz框架进行分布式任务定时调度,启动了两个实例,但是只有一个实例参与调度,另外一个实例没有参与调度,不知道是为什么?请各位帮助看一下原因!!