I am new to PyTorch, and I am exploring the functionality of .to() method. As per the documentation for the CUDA tensors, I see that it is possible to transfer the tensors between the CPU and GPU memory.
# let us run this cell only if CUDA is available
if torch.cuda.is_available():
# creates a LongTensor and transfers it to GPU as torch.cuda.LongTensor
a = torch.full((10,), 3, device=torch.device("cuda"))
# transfers it to CPU, back to being a torch.LongTensor
b = a.to(torch.device("cpu"))
In this context, I would like to know if it always necessary to transfer back the tensors from the GPU to CPU, perhaps to free the GPU memory? Doesn't, the runtime automatically clear the GPU memory?
Apart from its use of transferring data between CPU and GPU, I would like to know the recommended usage for .to() method (from the memory perspective). Thanks in advance.