Issue
In this example tf.get_default_graph()
points to new graph? when this new graph is created? why it not point to already existing graph
?
import tensorflow as tf
print('tf.__version__', tf.__version__)
graph = tf.Graph()
with graph.as_default():
x = tf.constant([[-2.25 + 4.75j], [-3.25 + 5.75j]])
y = tf.abs(x)
print(tf.get_default_graph())
print(graph)
graph == tf.get_default_graph()
Output:
tf.__version__ 1.14.0
<tensorflow.python.framework.ops.Graph object at 0x13ca5f128>
<tensorflow.python.framework.ops.Graph object at 0x13ca5f4e0>
False
Solution
By default, TensorFlow creates a "root" default graph, which will be the graph to use when no other graph has been designed as default with .as_default()
. You cannot "set" this base default graph, as it is created internally by TensorFlow, although you can drop it and replace it with a new graph with tf.reset_default_graph()
.
In your example, you create a new graph graph
, and then make a context manager with that graph as default.
graph = tf.Graph()
with graph.as_default():
x = tf.constant([[-2.25 + 4.75j], [-3.25 + 5.75j]])
y = tf.abs(x)
The tf.constant
and tf.abs
operations in there will be created within graph
, because that is the default graph inside that block.
However, once the block finishes, the context manager that makes graph
the default graph is finished too, so you are left with no explicitly set default graph. This means that the default graph will now be the one that TensorFlow created internally. If you call tf.reset_default_graph()
and then tf.get_default_graph()
again you will see that you get now a different graph.
So, if you want to use some specific graph as default, you always need to use the .as_default()
context to make it so, and the graph will stop being the default when you are out of that.
Answered By - jdehesa
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.