【问题标题】:I installed the environment according to the official Tensorflow tutorial, but the sample code is running incorrectly我按照Tensorflow官方教程安装环境,但是示例代码运行错误
【发布时间】:2020-03-07 02:53:30
【问题描述】:

我按照Tensorflow官方教程安装了环境,但是示例代码运行不正常:

  1. 安装 anaconda
  2. 通过conda create --name tensorflow python=3.7.4安装python
  3. 通过pip install --upgrade --ignore-installed tensorflow安装tensorflow
  4. 创建一个新项目并选择我安装的现有解释器
  5. 在pycharm中运行代码

有没有我遗漏的步骤?

import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
hello = tf.constant("hello world")
sess = tf.compat.v1.Session()
print(sess.run(hello))

Traceback(最近一次调用最后一次):

  File "E:/dp/dp_01/test.py", line 6, in <module>
    print(sess.run(hello))
  File "D:\Users\46173\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\client\session.py", line 956, in run
    run_metadata_ptr)
  File "D:\Users\46173\Anaconda3\envs\tensorflow\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().

【问题讨论】:

  • 我认为这不是官方的 TF 教程。从官网查看这个beginner教程

标签: python tensorflow pycharm anaconda


【解决方案1】:

事情是这样的

Tensorflow core r2.0 默认启用了 Eager Execution。所以,不改变它 我们只需要更改我们的代码

with tf.compat.v1.Session() as sess:
  os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
  hello = tf.constant("hello world")
  print(sess.run(hello))

【讨论】:

    猜你喜欢
    • 2019-11-01
    • 1970-01-01
    • 2020-01-03
    • 2020-11-02
    • 2020-06-12
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多