我根据Tensorflow的官方教程安装了环境,但是示例代码无法正确运行:
conda create --name tensorflow python=3.7.4
安装python pip install --upgrade --ignore-installed tensorflow
安装tensorflow 我错过了任何一步吗?
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))
回溯(最近通话最近一次):
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().
答案 0 :(得分:0)
事情是
默认情况下,Tensorflow核心r2.0已启用急切执行。因此,无需更改 我们只需要更改我们的代码
with tf.compat.v1.Session() as sess:
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
hello = tf.constant("hello world")
print(sess.run(hello))