Do not use tf.reset_default_graph() to clear neste

2020-08-22 07:42发布

问题:

I have a bunch of functions, which create portions of computation graph. In some of such functions I do

    with tf.name_scope("my_scope_name"):
        self._eye_n_components = tf.eye(se...

At the beginning of topmost function I call

tf.reset_default_graph()

and then call those partial functions and also they can call each other.

Unfortunately, I get an error

Error: Do not use tf.reset_default_graph() to clear nested graphs. If you need a cleared graph, exit the nesting and create a new graph.

Several questions.

1) What is nesting and how to "exit nesting"?

2) How to create new graph?

3) How to catch, where I am entering the nesting?

4) How to clear entire graph so that tensorflow does not think I am trying to clear nested one?

回答1:

This error message is displayed when you call tf.reset_default_graph() in one of the following scenarios:

  • Inside a with graph.as_default(): block.
  • Inside a with tf.Session(): block.
  • Between creating a tf.InteractiveSession and calling sess.close().

Each of these scenarios involves registering a default (and potentially "nested") tf.Graph object, which will be unregistered when you exit the block (or close the tf.InteractiveSession). Resetting the default graph in those scenarios would leave the system in an inconsistent state, so you should ensure to exit the block (or close the tf.InteractiveSession) before calling tf.reset_default_graph().