Clarify cpu() function

Hi everyone,
can someone explain to me, if I have some tensor and I’m using GPU, and I want to convert the tensor to NumPy without gradient, example:
img_var.detach().cpu().numpy()

img_var is tensor, so my question is if I’m only writing img_var.cpu().numpy() it’s not the same as what I write above.

Thanks!

Hi,
Let’s see what each method does:

1.tensor.detach(): creates a tensor that shares storage with tensor that does not require grad. You should use detach() when attempting to remove a tensor from a computation graph, and clone as a way to copy the tensor while still keeping the copy as a part of the computation graph it came from.

2.tensor.cpu(): moves the tensor back to memory accessible to the CPU.

3.tensor.numpy(): converts tensor to numpy array np.array.

So img_var.cpu().numpy() is not same as img_var.detach().cpu().numpy() because it will move tensor to cpu without creating a storage shared copy of it. And would result in error for next forward pass of your graph computation.

OK,
Thank you very much.