1

I create a tensorflow graph and define some tensors and run some stuff. When I'm done, I'd like to delete the graph that I made, and free up all of the resources. How can I do that thing?

temporary_graph = tf.Graph()
with temporary_graph.as_default(), tf.Session() as sess:
    foo = tf.placeholder(tf.float32, (2,2))
    bar = foo@foo
    res = sess.run(bar, feed_dict={foo: np.ones((2,2))})

print(res)
delete_graph_and_free_up_resources(temporary_graph)

This answer claims that the context manager cleans up the graph, but this isn't the case, and the docs don't claim such a thing:

>>> temporary_graph.get_operations()
[<tf.Operation 'Placeholder' type=Placeholder>, <tf.Operation 'matmul' type=MatMul>]

What is the best way to dispose of a graph?

Him
  • 5,257
  • 3
  • 26
  • 83
  • You can use [sess.close()](https://www.tensorflow.org/versions/r1.14/api_docs/python/tf/Session#close) to free all resources associated with session. – Hamed Oct 17 '19 at 15:47
  • @Hamed, but that doesn't clear up the graph. The actual values in the tensors go away with the session, but the graph remains. – Him Oct 17 '19 at 15:53

2 Answers2

1

It is not so simple, in order to free the resources that a graph is using you need to lose every reference to that graph, so Python can request to have it deleted from memory. That means deleting direct references to the graph, but also objects referencing the graph (and transitively). That includes operations, tensors and sessions, among other things. In your example, you would need to do:

del temporary_graph, sess, foo, bar, res

And that should make it possible to have the memory freed (not sure if you might need to call the garbage collector in some cases).

As you may not, you can not do this in a function, as it depends on the live references in your program. However, if you keep all references related to the graph within a function or object you should be able to do it fine.

jdehesa
  • 58,456
  • 7
  • 77
  • 121
0

I'm using tensorflow keras, but my approach is to simply clear the session:

tensorflow.keras.backend.clear_session()
geometrikal
  • 3,195
  • 2
  • 29
  • 40