Skip to main content
 首页 » 编程设计

python之为什么我的 Python 程序在 IDE(pycharm) 中运行,但当我从命令行尝试时却没有

2024年12月31日12bluestorm

我是 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)'" 

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

我的系统:

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

请您参考如下方法:

将图形传递给 session 。

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