Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 1 - A Simple Regression Problem #27

Closed
xihajun opened this issue Jun 15, 2022 · 2 comments
Closed

Chapter 1 - A Simple Regression Problem #27

xihajun opened this issue Jun 15, 2022 · 2 comments

Comments

@xihajun
Copy link

xihajun commented Jun 15, 2022

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
Copy link
Owner

dvgodoy commented Jun 15, 2022

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
Copy link
Author

xihajun commented Jun 17, 2022

Many thanks Daniel! That's really helpful 👍

Best,
Jun

@xihajun xihajun closed this as completed Jun 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants