18

How do I convert a torch.Tensor (on GPU) to a numpy.ndarray (on CPU)?

0

4 Answers 4

14

Use .detach() to convert from GPU / CUDA Tensor to numpy array:

tensor.detach().cpu().numpy()
Sign up to request clarification or add additional context in comments.

1 Comment

You only need to call detach if the Tensor has associated gradients. When detach is needed, you want to call detach before cpu. Otherwise, PyTorch will create the gradients associated with the Tensor on the CPU then immediately destroy them when numpy is called. Calling detach first eliminates that superfluous step. For more information see: discuss.pytorch.org/t/…
9
some_tensor.detach().cpu().numpy()

  • .detach() detaches from the backward graph to avoid copying gradients.
  • .cpu() moves the data to CPU.
  • .numpy() converts the torch.Tensor to a np.ndarray.

Comments

5

If the tensor is on gpu or cuda, copy the tensor to cpu and convert it to numpy array using:

tensor.data.cpu().numpy()

If the tensor is on cpu already you can do tensor.data.numpy(). However, you can also do tensor.data.cpu().numpy(). If the tensor is already on cpu, then the .cpu() operation will have no effect. And this could be used as a device-agnostic way to convert the tensor to numpy array.

2 Comments

NOTE: Using tensor.data without detaching may have unintended consequences, as explained here and here.
Is there any option to convert the tensor to integers?
2
tensor.numpy(force=True)

Per documentation:

If force is True this is equivalent to calling t.detach().cpu().resolve_conj().resolve_neg().numpy(). If the tensor isn’t on the CPU or the conjugate or negative bit is set, the tensor won’t share its storage with the returned ndarray. Setting force to True can be a useful shorthand.

Edit:

Documentation link: https://pytorch.org/docs/stable/generated/torch.Tensor.numpy.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.