Reputation: 91
The below code runs fine in a tutorial but there is an error when I run it locally. Are there any installation errors or something else? Please help. This is link to that tutorial:
https://colab.research.google.com/notebooks/mlcc/tensorflow_programming_concepts.ipynb?utm_source=mlcc&utm_campaign=colab-external&utm_medium=referral&utm_content=tfprogconcepts-colab&hl=en#scrollTo=Md8ze8e9geMi
And the code:
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())
Below is the error that occurs:
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)`.
Upvotes: 0
Views: 1341
Reputation: 4183
If you create/use a Graph
object explicitly rather than using the default graph, you need to either (a) pass the graph object to your Session
constructor, or (b) create the session in the graph context.
graph = tf.Graph()
with graph.as_default():
build_graph()
with tf.Session(graph=graph) as sess:
do_stuff_with(sess)
or
graph = tf.Graph()
with graph.as_default():
build_graph()
with tf.Session() as sess:
do_stuff_with(sess)
Upvotes: 0
Reputation: 320
To simply get it working you can pass the session explicitly, as suggested by the error message:
print(my_sum.eval(session=sess))
To understand why it doesn't work exactly as the tutorial specifies it, you could start by comparing the versions of Python and TensorFlow to those used in the tutorial.
import tensorflow as tf
import platform
print("Python version: ", platform.python_version())
print("TensorFlow version", tf.__version__)
For the colab environment you linked, this prints:
Python version: 2.7.14
TensorFlow version 1.10.0
EDIT
Taking another look at your code sample, it's not an issue of version compatibility. The issue is that your copy of the tutorial did not properly preserve the indentation. The second with
block needs to be enclosed in the first.
# 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())
This ensures that g
is used as the default graph for the session, instead of creating a new one like MatthewScarpino points out your incorrectly-indented version does.
Upvotes: 1
Reputation: 5936
The problem is that you've created one graph (g
) and you're executing code in a separate graph (sess
). If you don't need two graphs, you can just use 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())
Upvotes: 2