Python 张量计算中的误差

Python 张量计算中的误差,python,python-3.x,tensorflow,python-3.5,tensor,Python,Python 3.x,Tensorflow,Python 3.5,Tensor,下面的代码在教程中运行良好,但在本地运行时出错。是否存在任何安装错误或其他问题?请帮忙。这是指向该教程的链接: 以及守则: import tensorflow as tf #create a graph g = tf.Graph() #establish the graph as the default graph with g.as_default(): x = tf.constant(8, name = "x_const") y = tf.constant(5, name =

下面的代码在教程中运行良好,但在本地运行时出错。是否存在任何安装错误或其他问题?请帮忙。这是指向该教程的链接:
以及守则:

import tensorflow as tf

#create a graph 
g = tf.Graph()

#establish the graph as the default 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")

#create the session
#this runs the default graph
with tf.Session() as sess:
 print(my_sum.eval())
以下是发生的错误:

gunjan@gunjan-Inspiron-3558:~/Desktop$ python tf1.py 
/home/gunjan/anaconda3/lib/python3.5/site- 
packages/h5py/__init__.py:34: FutureWarning: Conversion of the second 
argument of issubdtype from `float` to `np.floating` is deprecated. In 
future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
2018-08-20 22:10:41.619062: I 
tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports 
instructions that this TensorFlow binary was not compiled to use: AVX2 
FMA
Traceback (most recent call last):
File "tf1.py", line 15, in <module>
print(my_sum.eval())
File "/home/gunjan/anaconda3/lib/python3.5/site- 
packages/tensorflow/python/framework/ops.py", line 680, in eval
return _eval_using_default_session(self, feed_dict, self.graph, 
session)
File "/home/gunjan/anaconda3/lib/python3.5/site- 
packages/tensorflow/python/framework/ops.py", line 4942, in 
_eval_using_default_session
raise ValueError("Cannot use the default session to evaluate tensor: "
ValueError: 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)`.
gunjan@gunjan-Inspiron-3558:~/Desktop$python tf1.py
/home/gunjan/anaconda3/lib/python3.5/site-
packages/h5py/__init__.py:34:未来警告:第二个的转换
issubdtype的参数从'float'到'np.floating'已弃用。在里面
将来,它将被视为`np.float64==np.dtype(float.type`。
from.\u conv导入寄存器\u转换器作为\u寄存器\u转换器
2018-08-20 22:10:41.619062:I
tensorflow/core/platform/cpu\u feature\u guard.cc:141]您的cpu支持
未编译此TensorFlow二进制文件以使用的指令:AVX2
FMA
回溯(最近一次呼叫最后一次):
文件“tf1.py”,第15行,在
打印(my_sum.eval())
文件“/home/gunjan/anaconda3/lib/python3.5/site-
packages/tensorflow/python/framework/ops.py”,第680行,eval中
使用默认会话(self、feed、dict、self.graph、,
(会议)
文件“/home/gunjan/anaconda3/lib/python3.5/site-
packages/tensorflow/python/framework/ops.py”,第4942行,在
_使用默认会话评估
raise VALUERROR(“无法使用默认会话计算张量:”
ValueError:无法使用默认会话计算tensor:
张量图与会话图不同。传递一个显式
会话到'eval(session=sess)`。

要简单地让它工作,您可以按照错误消息的建议显式地传递会话:

print(my_sum.eval(session=sess))
要理解为什么它不能完全按照教程中的说明工作,可以从比较Python和TensorFlow的版本与教程中使用的版本开始

import tensorflow as tf
import platform
print("Python version: ", platform.python_version())
print("TensorFlow version", tf.__version__)
对于链接的colab环境,这将打印:

Python version:  2.7.14
TensorFlow version 1.10.0
编辑

再看看您的代码示例,这不是版本兼容性问题。问题是您的教程副本没有正确保留缩进。第二个
with
块需要包含在第一个块中

# Establish the graph as the "default" graph.
with g.as_default():
  # ...

  # Now create a session.
  # The session will run the default graph.
  with tf.Session() as sess:
    print(my_sum.eval())

这可确保将
g
用作会话的默认图形,而不是创建一个新图形,如MatthewScarpino指出的缩进不正确的版本所做的那样。

要简单地使其工作,您可以显式地传递会话,如错误消息所示:

print(my_sum.eval(session=sess))
要理解为什么它不能完全按照教程中的说明工作,可以从比较Python和TensorFlow的版本与教程中使用的版本开始

import tensorflow as tf
import platform
print("Python version: ", platform.python_version())
print("TensorFlow version", tf.__version__)
对于链接的colab环境,这将打印:

Python version:  2.7.14
TensorFlow version 1.10.0
编辑

再看看您的代码示例,这不是版本兼容性问题。问题是您的教程副本没有正确保留缩进。第二个
with
块需要包含在第一个块中

# Establish the graph as the "default" graph.
with g.as_default():
  # ...

  # Now create a session.
  # The session will run the default graph.
  with tf.Session() as sess:
    print(my_sum.eval())

这可以确保将
g
用作会话的默认图形,而不是像MatthewScarpino指出的缩进不正确的版本那样创建一个新图形。

问题是您创建了一个图形(
g
),并且正在一个单独的图形(
sess
)中执行代码。如果不需要两个图形,则可以使用
sess

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

#create the session
#this runs the default graph
with tf.Session() as sess:
    print(my_sum.eval())

问题是您创建了一个图(
g
),并且正在一个单独的图(
sess
)中执行代码。如果不需要两个图,您可以使用
sess

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

#create the session
#this runs the default graph
with tf.Session() as sess:
    print(my_sum.eval())

如果显式创建/使用
图形
对象而不是使用默认图形,则需要(a)将图形对象传递给
会话
构造函数,或者(b)在图形上下文中创建会话

graph = tf.Graph()
with graph.as_default():
    build_graph()

with tf.Session(graph=graph) as sess:
    do_stuff_with(sess)


如果显式创建/使用
图形
对象而不是使用默认图形,则需要(a)将图形对象传递给
会话
构造函数,或者(b)在图形上下文中创建会话

graph = tf.Graph()
with graph.as_default():
    build_graph()

with tf.Session(graph=graph) as sess:
    do_stuff_with(sess)


我有python 3.5.2。如何设置与colab环境相同的环境?我的tf版本相同。看起来这不是版本兼容性问题。请参阅我关于缩进的修订答案。我有python 3.5.2。如何设置与colab环境相同的环境?我的tf版本相同。看起来这不是版本兼容性问题苏。请看我关于缩进的修订答案。但是代码在给定链接的环境中运行良好。请看一看。但是代码在给定链接的环境中运行良好。请看一看。