mrgloom
mrgloom

Reputation: 21602

Tensorflow: why tf.get_default_graph() is not current graph?

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

Upvotes: 1

Views: 2739

Answers (2)

Hassan Shoayb
Hassan Shoayb

Reputation: 11

Using tf.get_default_graph() is deprecated, Instead used tf.compat.v1.get_default_graph() it works for me really fine.

Upvotes: 0

javidcf
javidcf

Reputation: 59681

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.

Upvotes: 4

Related Questions