dvgodoy / PyTorchStepByStep

Official repository of my book: "Deep Learning with PyTorch Step-by-Step: A Beginner's Guide"
https://pytorchstepbystep.com
MIT License
834 stars 310 forks source link

Chapter 1 - A Simple Regression Problem #27

Closed xihajun closed 2 years ago

xihajun commented 2 years ago
There is only one thing left to do; turn our tensor into a GPU tensor. That is what [to()](https://bit.ly/32Mgxjc) is good for. It sends a tensor to the specified device.

Hi Dan,

I love your book and tutorials! May I kindly ask does to() method copy the data input the device (GPU or CPU) memory directly?

The reason I am asking is that you mentioned before that torch.as_tensor(x_train) will shares the underlying data with the original Numpy array, but when we used torch.as_tensor(x_train).to(device) I found that x_train data won't change.

Do I understand it correctly?

Best, Jun

dvgodoy commented 2 years ago

Hi Jun,

Thank you for your kind words :-)

You got it absolutely right - once you send data to the GPU, it needs to be copied there. For CPU tensors, the data is stored in the computer's RAM, and it can be accessed by both Numpy and PyTorch, and the underlying data is shared by them.

But, the moment you send data to the GPU, it will be copied to the GPU's RAM, and it won't be shared with Numpy anymore.

Numpy does not support GPUs, which is the reason why we have to use .cpu() to bring the tensor back to the main RAM before turning it into a Numpy array.

Hope it helps :-) Best, Daniel

xihajun commented 2 years ago

Many thanks Daniel! That's really helpful 👍

Best, Jun