我正在尝试运行在colab上发现的非常简单的tensorflow代码,但似乎我缺少有关它的一些基本知识。
我已经尝试用xy_sum.eval(session = sess)替换xy_sum.eval(),但问题似乎仍然存在。 这是“我的”代码:
from __future__ import print_function
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")
xy_sum=tf.add(x,y,name="x_y_sum")
with tf.Session() as sess:
print(xy_sum.eval())
我希望输出为https://colab.research.google.com/notebooks/mlcc/tensorflow_programming_concepts.ipynb#scrollTo=Md8ze8e9geMi所示的13, 但是我从spyder得到的输出是
Traceback (most recent call last):
File "<ipython-input-35-67314fd48aa4>", line 1, in <module>
runfile('/home/***/.config/spyder-py3/New Folder/sommatf.py', wdir='/home/***/.config/spyder-py3/New Folder')
File "/home/***/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 786, in runfile
execfile(filename, namespace)
File "/home/***/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/home/***/.config/spyder-py3/New Folder/sommatf.py", line 15, in <module>
print(xy_sum.eval())
File "/home/***/python3.7/site-packages/tensorflow/python/framework/ops.py", line 695, in eval
return _eval_using_default_session(self, feed_dict, self.graph, session)
File "/home/***/python3.7/site-packages/tensorflow/python/framework/ops.py", line 5172, 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)`.
如果我按照建议尝试通过显式会话,则会得到:
raise ValueError("Cannot use the given session to evaluate tensor: "
ValueError: Cannot use the given session to evaluate tensor: the tensor's graph is different from the session's graph.
答案 0 :(得分:0)
- (BOOL) someMethodWithError: (NSError **) outError {
[self.something attempt];
if (!self.something.succeeded) {
if (nil != outError) {
*outError = [self makeSomeDescriptiveErrorFromSomething: self.something];
}
return NO;
}
else {
return YES;
}
}
使用默认图形。因此,为了使用图tf.Session
,您将不得不缩进会话初始化到g
块中,或者将图传递给会话at initialization。