Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove nodes from graph or reset entire default graph

When working with the default global graph, is it possible to remove nodes after they've been added, or alternatively to reset the default graph to empty? When working with TF interactively in IPython, I find myself having to restart the kernel repeatedly. I would like to be able to experiment with graphs more easily if possible.

like image 941
Mohammed AlQuraishi Avatar asked Nov 17 '15 19:11

Mohammed AlQuraishi


People also ask

What is TF Reset_default_graph ()?

Clears the default graph stack and resets the global default graph.

What is default graph in TensorFlow?

A graph is like a TODO: list. You may use more than one graphs (created with tf. Graph() in the same process, but one is the default. Note you will have to use different sessions for each graph, but each graph can be used in multiple sessions. Even more, a session allows executing graphs or part of graphs.

What is TF graph?

Graph execution means that tensor computations are executed as a TensorFlow graph, sometimes referred to as a tf. Graph or simply a "graph." Graphs are data structures that contain a set of tf. Operation objects, which represent units of computation; and tf.


2 Answers

Update 11/2/2016

tf.reset_default_graph()

Old stuff

There's reset_default_graph, but not part of public API (I think it should be, does someone wants to file an issue on GitHub?)

My work-around to reset things is this:

from tensorflow.python.framework import ops ops.reset_default_graph() sess = tf.InteractiveSession() 
like image 87
Yaroslav Bulatov Avatar answered Sep 20 '22 23:09

Yaroslav Bulatov


By default, a session is constructed around the default graph. To avoid leaving dead nodes in the session, you need to either control the default graph or use an explicit graph.

  • To clear the default graph, you can use the tf.reset_default_graph function.

    tf.reset_default_graph() sess = tf.InteractiveSession() 
  • You can also construct explicitly a graph and avoid using the default one. If you use a normal Session, you will need to fully create the graph before constructing the session. For InteractiveSession, you can just declare the graph and use it as a context to declare further changes:

    g = tf.Graph() sess = tf.InteractiveSession(graph=g) with g.asdefault():     # Put variable declaration and other tf operation     # in the graph context     ....     b = tf.matmul(A, x)     ....   sess.run([b], ...) 

EDIT: For recent versions of tensorflow (1.0+), the correct function is g.as_default.

like image 23
Thomas Moreau Avatar answered Sep 18 '22 23:09

Thomas Moreau