【问题标题】:Why does my Python program run in an IDE(pycharm) but not when i try it from command line?为什么我的 Python 程序在 IDE(pycharm) 中运行,但当我从命令行尝试时却没有?
【发布时间】:2018-12-07 10:20:28
【问题描述】:

我是 TensorFlow 的新手。有问题的代码如下:

import tensorflow as tf

g = tf.Graph()

with g.as_default():

x = tf.constant(8, name="x_const")
y = tf.constant(5, name="y_const")
my_sum = tf.add(x, y, name="x_y_sum")

with tf.Session() as sess:
print(my_sum.eval())

上面的代码在 PyCharm 中运行没有任何错误,给出了正确的结果。但是当我从命令行尝试相同的代码时,我得到了一些我提供的屏幕截图的错误。 screenshot of the error

我无法理解错误的原因。错误状态中的主线:

"Cannot use the default session to evaluate tensor: the tensor's graph is 
different from the session's graph.Pass an explicit session to 
'eval(session=sess)'"

但即使我给出明确的会话,它也会显示几乎相同的错误。我也不明白为什么它在 PyCharm IDE 中运行时没有任何错误。

我的系统:

  • Windows 7 Ultimate(64 位)
  • 4GB 内存
  • 英特尔奔腾 2020M 处理器
  • 英特尔高清显卡
  • Python 3.6.5(64 位)
  • Tensorflow 版本 1.5.0

【问题讨论】:

    标签: python python-3.x tensorflow pycharm


    【解决方案1】:

    将图表传递给会话。

    with tf.Session(graph=g) as sess:
        print(my_sum.eval())
    

    【讨论】:

    • 成功了。谢谢。但我仍然不明白为什么代码在 PyCharm 中工作而没有将默认图形传递给会话。
    猜你喜欢
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    相关资源
    最近更新 更多