TensorFlow(踩坑):RuntimeError: The Session graph is empty.

TensorFlow–session

刚安装完TensorFlow都会测试是否安装成功:

import tensorflow as tf
one = tf.constant([[3,3]])
two = tf.constant([[2],[2]])
p = tf.matmul(one,two)
sess = tf.compat.v1.Session()
re=sess.run(p)
print(re)

这是一个简单的矩阵相乘的例子。但是由于TensorFlow版本的原因会有某些坑。在此记录两个。

第一个:

    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().

解决方案:
将上述例子代码改为

import tensorflow as tf

g = tf.Graph()
with g.as_default():
    one = tf.constant([[3,3]])
    two = tf.constant([[2],[2]])
    p = tf.matmul(one,two)
    sess = tf.compat.v1.Session(graph=g)
    re=sess.run(p)
    print(re)

运行结果:

[[12]]

Process finished with exit code 0

第二个:

I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2

这个不会影响运行结果,意思是当前TensorFlow版本不支持AVX2.
出于强迫症,看不得红色提示。
可以在代码中加入以下两行忽略此提示:


import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

END

发布了15 篇原创文章 · 获赞 16 · 访问量 5761

猜你喜欢

转载自blog.csdn.net/weixin_42768004/article/details/103322770