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

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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器