Keras:完成训练过程后释放内存 [英] Keras: release memory after finish training process
问题描述
我使用Keras构建了一个基于CNN结构的自动编码器模型,完成训练过程后,我的笔记本电脑有64GB内存,但我注意到至少还有1/3的内存被占用,GPU也是一样内存也是.没有找到释放内存的好方法,只能通过关闭Anaconda Prompt命令窗口和jupyter notebook来释放内存.我不确定是否有人有好的建议.谢谢!
I built an autoencoder model based on CNN structure using Keras, after finish the training process, my laptop has 64GB memory, but I noticed that at least 1/3 of the memory is still occupied, and the same thing for the GPU memory, too. I did not find out a good method to release the memory, I could only release the memory by closing the Anaconda Prompt command window and jupyter notebook. I am not sure if anyone has a good suggestion. Thanks!
推荐答案
释放 RAM 内存
要释放 RAM 内存,只需按照@nuric 在注释中的建议执行 del Variables
.
这比释放 RAM 内存要棘手一些.有些人会建议您使用以下代码(假设您使用的是 keras)
This is a little bit trickier than releasing the RAM memory. Some people will suggest you the following code (Assuming you are using keras)
from keras import backend as K
K.clear_session()
然而,上面的代码并不适用于所有人.(即使你尝试了 del Models
,它仍然无法工作)
However, the above code doesn't work for all people. (Even when you try del Models
, it is still not going to work)
如果上述方法对您不起作用,请尝试以下方法(您需要安装 numba 库优先):
If the above method doesn't work for you, then try the following (You need to install the numba library first):
from numba import cuda
cuda.select_device(0)
cuda.close()
背后的原因是:Tensorflow 只是为 GPU 分配内存,而 CUDA 负责管理 GPU 内存.
The reason behind it is: Tensorflow is just allocating memory to the GPU, while CUDA is responsible for managing the GPU memory.
如果在您使用K.clear_session()
清除所有图形后,CUDA以某种方式拒绝释放GPU内存,那么您可以使用cuda库直接控制CUDA进行清除GPU内存.
If CUDA somehow refuses to release the GPU memory after you have cleared all the graph with K.clear_session()
, then you can use the cuda library to have a direct control on CUDA to clear up GPU memory.
这篇关于Keras:完成训练过程后释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!