Q: What does it mean "The tensor's graph is different from the session's graph." I'm trying to run this very simple tensorflow code I found on colab, but it seems I'm missing something basic about it.
I already tried to replace xy_sum.eval() with xy_sum.eval(session=sess) but the problem seems to persist.
Here's "my" code:
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())

I expect the output to be 13 as shown on https://colab.research.google.com/notebooks/mlcc/tensorflow_programming_concepts.ipynb#scrollTo=Md8ze8e9geMi,
but the ouput i get from spyder is
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)`.

if I try, as suggested, to pass an explicit session I get:
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.


A: The tf.Session uses the default graph. So in order to use the graph g you would either have to indent the session initialization into the with g.as_default(): block, or pass the graph to the session at initialization.
