# To(device) changes the value of input

**URL:** <https://discuss.pytorch.org/t/to-device-changes-the-value-of-input/167491>\
**Category:** Uncategorized\
**Created:** [December 4, 2022, 12:26pm UTC](https://discuss.pytorch.org/t/to-device-changes-the-value-of-input/167491 "2022-12-04T12:26:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wilsonng.colab](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/wilsonng.colab/32/47650_2.png) [@wilsonng.colab](https://discuss.pytorch.org/u/wilsonng.colab)\
**Post date:** [December 4, 2022, 12:26pm UTC](https://discuss.pytorch.org/t/to-device-changes-the-value-of-input/167491/1 "2022-12-04T12:26:15Z")

</div>

```auto
   for images, _in train_dataloader:
     print(images.min(), images.max())
     images= images.to(device)
     print(images.min(), images.max())
     ...

```

After to(device), the range of images changed from [-1, 1] to (-inf, int).  
To be more specific, the value of images after to(device) could become Nan or any arbitrary values.  
What could be the possible reason for it?

Here are the images before to(device) and after:  
I don’t understand why there could be some random horizontal lines or vertical lines which are pixels with arbitrary values

 ![image](https://discuss.pytorch.org/uploads/default/original/3X/b/0/b0877842d63528616a154910b178a5377c09fda6.png)

---

<div class="post-metadata">

**Author:** ![wilsonng.colab](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/wilsonng.colab/32/47650_2.png) [@wilsonng.colab](https://discuss.pytorch.org/u/wilsonng.colab)\
**Post date:** [December 4, 2022, 3:58pm UTC](https://discuss.pytorch.org/t/to-device-changes-the-value-of-input/167491/2 "2022-12-04T15:58:10Z")

</div>

I found this problem has to do with pin\_memory=True in DataLoader.  
When I set pin\_memory=False, the problem gone.

Can I know why does this problem occur when pin\_memory=True?

---

<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:** [December 4, 2022, 7:46pm UTC](https://discuss.pytorch.org/t/to-device-changes-the-value-of-input/167491/3 "2022-12-04T19:46:24Z")

</div>

You need to synchronize the CPU on the used stream if you are using async device2host copies, otherwise you’ll run into data corruption.
