14

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

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Noa Yehezkel
  • 468
  • 2
  • 4
  • 20
  • 1
    Possible duplicate of [How to convert Pytorch autograd.Variable to Numpy?](https://stackoverflow.com/questions/44340848/how-to-convert-pytorch-autograd-variable-to-numpy) – Fábio Perez Nov 25 '18 at 12:20

3 Answers3

9

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

tensor.detach().cpu().numpy()
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
azizbro
  • 3,069
  • 4
  • 22
  • 36
  • 4
    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: https://discuss.pytorch.org/t/should-it-really-be-necessary-to-do-var-detach-cpu-numpy/35489/8?u=zayd – ZaydH Sep 14 '19 at 08:05
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.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Umang Gupta
  • 15,022
  • 6
  • 48
  • 66
  • **NOTE:** Using `tensor.data` without detaching may have unintended consequences, [as explained here](https://github.com/pytorch/pytorch/issues/6990#issuecomment-384680164) and [here](https://pytorch.org/blog/pytorch-0_4_0-migration-guide/#what-about-data). – Mateen Ulhaq Jul 29 '22 at 06:28
5
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.
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135