Clear PyTorch Cache: A Guide to Freeing GPU Memory
To clear the cache of PyTorch, follow these steps:
- Import the 'torch' module:
import torch
- Clear the cache using the 'torch.cuda.empty_cache()' function:
torch.cuda.empty_cache()
This function releases all unoccupied cached memory currently held by the PyTorch CUDA allocator. It is helpful when you want to free up GPU memory previously allocated by PyTorch.
Note that this function works only when using PyTorch with CUDA support (i.e., if GPUs are available). If not using GPUs or PyTorch without CUDA, this function is not applicable.
Call 'torch.cuda.empty_cache()' as needed during your program's execution to clear the cache and release GPU memory.
原文地址: https://www.cveoy.top/t/topic/Seg 著作权归作者所有。请勿转载和采集!