What is the cpu() in pytorch


correct += pred_eq(target.data).cpu().sum()

What`s the meaning of cpu() ?
Can anyone understand that code ?

1 Like

This is used to move the tensor to cpu(). Some operations on tensors cannot be performed on cuda tensors so you need to move them to cpu first.

4 Likes

tensor.cuda() is used to move a tensor to GPU memory.
tensor.cpu() moves it back to memory accessible to the CPU.

2 Likes

But after doing tensor.cpu() when I check the device of tensor using tensor.device it gives the original cuda:0 where it was before moving to cpu. How can I be sure that the tensor is moved to CPU?

You have to assign the tensor after moving:

tensor = tensor.cpu()
# or using the new method
tensor = tensor.to('cpu)
11 Likes

Thanks @ptrblck. I see that .cpu() or .cuda() works differently for model and tensor. :+1:

4 Likes