python - tf.reset_default_graph 内存泄漏

标签 python session tensorflow graph memory-leaks

我正在尝试在 TensorFlow 中进行一些自动超参数优化的实验,因此我的程序的主循环需要重复创建一个图、运行它、销毁它以释放内存以供下一次迭代。为此,我有:

tf.reset_default_graph()
sess = tf.InteractiveSession()

这似乎在所有方面都有效,除了一种:每次循环时,程序都会泄漏数百兆字节的内存。我没有分配任何其他类型的复杂数据结构,这些数据结构可能会成为内存泄漏的候选者,因此我可能错误地使用了 tf.reset_default_graph

我做错了什么?

节目地址为https://github.com/russellw/tf-examples/blob/master/sko_lstm.py

最佳答案

替换tf.InteractiveSession()with tf.Session():陈述。前者设计为在控制台中使用,因此它会启动,使其成为默认值,并且不会释放资源,除非您调用 sess.close()手动(这很容易忘记,最重要的是你应该考虑异常(exception))。结果,您的代码最终会产生大量并行 session ,每个 session 都持有其资源。 tf.reset_default_graph() session 处于事件状态时不会释放这些资源。

with语句确保上下文管理器将调用 __exit__并且 session 将关闭。

关于python - tf.reset_default_graph 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49942110/

相关文章:

python - 用逗号替换字母和数字之间的空格?

session - Captcha Servlet 导致 java.lang.IllegalStateException : PWC3999: Cannot create a session after the response has been committed

python - 通过与第二个列表比较从列表中删除项目,Python

python - 使用 setup.py 将 .pyx 编译为 .so

php - Joomla 增加前端 session 超时限制

machine-learning - 如何预测 tensorflow 模型的输出?

python - BiLSTM_Classifier 中的输入/输出/循环 dropout 层以及它们如何影响模型和预测

Tensorflow 对象检测 API : Train from exported model checkpoint

类结构内部的 Python::tkinter OptionMenu 方法不起作用

magento - 重写 Magento session 的最后一个 URL