I want to make a cross validation in my project based on Pytorch. And I didn't find any method that pytorch provided to delete the current model and empty the memory of GPU. Could you tell that how can I do it?
Like said above: if you want to free the memory on the GPU you need to get rid of all references pointing on the GPU object. Then it will be freed automatically. So assuming model is on GPU: model=model. cpu() will free the GPU-memory if you don't keep any other references to of model, but model_cpu=model.
Freeing memory in PyTorch works as it does with the normal Python garbage collector. This means once all references to an Python-Object are gone it will be deleted.
You can delete references by using the del
operator:
del model
You have to make sure though that there is no reference to the respective object left, otherwise the memory won't be freed.
So once you've deleted all references of your model
, it should be deleted and the memory freed.
If you want to learn more about memory management you can take a look here: https://pytorch.org/docs/stable/notes/cuda.html#cuda-memory-management
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With