pytorch delete model from gpu

19,377

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

Share:
19,377
Admin
Author by

Admin

Updated on June 13, 2022

Comments

  • Admin
    Admin almost 2 years

    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?