Managing Nodes Working with multiple graphs in TensorFlow

Most of the scenario in development requires only one graph for the computation in tensorflow .  Still there may be some situation where we need more tensor graph for computation  . In this multi graph scenario if you create a new node then It will automatically add to the default graph . Hence if you need to manage multiple graphs in TensorFlow , you need to set them default before creating new nodes for this .

How to add new node with different graphs :

  1. Refer the below code . Here we will see all new created node is associated with default graph .
new_node = tf.Variable(1)
new_node .graph is tf.get_default_graph()

output  –

True

2. Now we will create a new graph.

graph = tf.Graph()

3. After the creation of new graph , set it as default graph for working temporary and associate a new node with it.

with graph.as_default():
   new_node_2 = tf.Variable(2)

Conclusion –

Every new node is from the default graph. If you create a new graph in TensorFlow , you need to set it default and then add the new node with it. Let’s check the result-

>>> node_2.graph is graph
True
>>> new_node_2.graph is tf.get_default_graph()
False

You may see the newly created node is associated with new graph . It is because we have set it as default for a temporary basis. Once the original or previous graph gets back and resume, now on checking the node association with default graph , we get it is not from default graph . I hope this article must help in clearing the concept of  multiple graphs in TensorFlow .

Data Science Learner has started creating tutorials on tensorflow and Deep Learning concepts . If you do not want to miss them ,Immediately subscribe Data Science Learner . You will get latest update on Deep Learning .

Thanks 

Data Science Learner Team