# Converting numpy array to tensor on GPU

**URL:** <https://discuss.pytorch.org/t/converting-numpy-array-to-tensor-on-gpu/19423>\
**Category:** Uncategorized\
**Created:** [June 8, 2018, 6:19pm UTC](https://discuss.pytorch.org/t/converting-numpy-array-to-tensor-on-gpu/19423 "2018-06-08T18:19:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tejus-gupta](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tejus-gupta/32/5084_2.png) [@tejus-gupta](https://discuss.pytorch.org/u/tejus-gupta)\
**Post date:** [June 8, 2018, 6:19pm UTC](https://discuss.pytorch.org/t/converting-numpy-array-to-tensor-on-gpu/19423/1 "2018-06-08T18:19:08Z")

</div>

```auto
import torch
from skimage import io

img = io.imread('input.png')

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)

img =torch.tensor(img, device=device).float()
print(img.device)

```

Output

```auto
cuda:0
cpu

```

I am not able to convert a numpy array into a torch tensor on GPU. What is the right way to do this?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [June 8, 2018, 6:32pm UTC](https://discuss.pytorch.org/t/converting-numpy-array-to-tensor-on-gpu/19423/2 "2018-06-08T18:32:57Z")

</div>

You should transform numpy arrays to PyTorch tensors with `torch.from_numpy`.  
Otherwise some weird issues might occur.

```auto
img = torch.from_numpy(img).float().to(device)

```

---

<div class="post-metadata">

**Author:** ![tejus-gupta](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tejus-gupta/32/5084_2.png) [@tejus-gupta](https://discuss.pytorch.org/u/tejus-gupta)\
**Post date:** [June 8, 2018, 6:37pm UTC](https://discuss.pytorch.org/t/converting-numpy-array-to-tensor-on-gpu/19423/3 "2018-06-08T18:37:03Z")

</div>

That works. Thanks! 😄

---

<div class="post-metadata">

**Author:** ![Nkululeko\_Thangelane](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nkululeko_thangelane/32/8942_2.png) [@Nkululeko\_Thangelane](https://discuss.pytorch.org/u/Nkululeko_Thangelane)\
**Post date:** [January 27, 2019, 6:52am UTC](https://discuss.pytorch.org/t/converting-numpy-array-to-tensor-on-gpu/19423/4 "2019-01-27T06:52:26Z")

</div>

Thank you man this was helpful

---

<div class="post-metadata">

**Author:** ![arjunsankarlal](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/arjunsankarlal/32/8191_2.png) [@arjunsankarlal](https://discuss.pytorch.org/u/arjunsankarlal)\
**Post date:** [March 28, 2019, 12:21pm UTC](https://discuss.pytorch.org/t/converting-numpy-array-to-tensor-on-gpu/19423/5 "2019-03-28T12:21:04Z")

</div>

How to achieve this in Pytorch version 0.3.0?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [March 28, 2019, 3:08pm UTC](https://discuss.pytorch.org/t/converting-numpy-array-to-tensor-on-gpu/19423/6 "2019-03-28T15:08:11Z")

</div>

You should use `.cpu()` or `.cuda()` instead of the `.to(device)` method. The others should also exist in `0.3.0`.
