Tensorflow 删除图并释放资源 [英] Tensorflow delete graph and free up resources

查看:94
本文介绍了Tensorflow 删除图并释放资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个张量流图并定义了一些张量并运行了一些东西.完成后,我想删除我制作的图表,并释放所有资源.我该怎么做?

temporary_graph = tf.Graph()使用 temp.as_default(), tf.Session() 作为 sess:foo = tf.placeholder(tf.float32, (2,2))酒吧 = foo@foores = sess.run(bar, feed_dict={foo: np.ones((2,2))})打印(资源)delete_graph_and_free_up_resources(temporary_graph)

这个答案声称上下文管理器会清理图表,但事实并非如此,文档不要声称这样的事情:

<预><代码>>>>临时图.get_operations()[<tf.Operation 'Placeholder' type=Placeholder>, <tf.Operation 'matmul' type=MatMul>]

处理图形的最佳方法是什么?

解决方案

这不是那么简单,为了释放一个图正在使用的资源,你需要丢失对该图的所有引用,因此 Python 可以请求将其从内存中删除.这意味着删除对图形的直接引用,但也删除引用图形的对象(和传递性).这包括操作、张量和会话等.在您的示例中,您需要执行以下操作:

del temporal_graph, sess, foo, bar, res

这应该可以释放内存(不确定您是否需要调用 垃圾收集器 在某些情况下).

虽然您可能不会,但您可以在函数中执行此操作,因为这取决于程序中的实时引用.但是,如果您将与图形相关的所有引用保留在一个函数或对象中,您应该能够做得很好.

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?

解决方案

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.

这篇关于Tensorflow 删除图并释放资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆