To(device) changes the value of input

   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

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?

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