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

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值