python - Keras/Tensorflow疑似内存泄漏

标签 python tensorflow keras memory-leaks

我正在使用 Keras (2.4) 和 Tensorflow (2.3.1) 后端。我正在使用网格搜索调整一些元参数,以便多次调用模型创建和 model.fit 方法(训练是在具有单个 GPU 的机器上完成的)。

Tensorflow 存在已记录的内存泄漏问题(e.g see here,但还有更多报告,只需在 Google 中搜索tensorflow 内存泄漏)。我过去遇到过这个问题,并设法使用小批量大小和手动调用 Python 垃圾收集器的组合将其保持在最低水平。但现在在另一个项目中,我怀疑我再次遇到了它。

我附上一张监控 RAM 使用情况的图像。 15 小时,我的问题很简单 - 看起来这可能是内存泄漏的结果吗?一方面,最大使用量随着时间的推移而增长。但是,另一方面,似乎取消分配(也许当模型实例被销毁并使用新参数创建时?)将大部分已用内存释放回(几乎)到初始级别。 enter image description here

最佳答案

tf.keras.backend.clear_session()怎么样
关注document

Keras manages a global state, which it uses to implement the Functional model-building API and to uniquify autogenerated layer names.

If you are creating many models in a loop, this global state will consume an increasing amount of memory over time, and you may want to clear it. Calling clear_session() releases the global state: this helps avoid clutter from old models and layers, especially when memory is limited.

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

相关文章:

go - Go 中的 Tensorflow 服务

python - TensorFlow:tf.add_n 的乘积等价物

python - 当数据集不适合内存时,Keras 优化器状态

optimization - Keras 中的 Adam 优化器有动量选项吗?

python - 导入 cv2 时 DLL 加载失败错误

python - 如何在不删除旧数据的情况下用excel中的xlsxwriter写入现有文件

python - 堆的算法置换生成器

python - 如何获取Keras模型所有层的输出?

python - 如何处理使用 simpleITK 加载的图像中的负像素值

python - 检查目标 : expected activation_2 to have shape (512, 时出错,但得到形状为 (1,) 的数组