Open In App

How to Move a Torch Tensor from CPU to GPU and Vice Versa in Python?

In this article, we will see how to move a tensor from CPU to GPU and from GPU to CPU in Python.

Why do we need to move the tensor?

This is done for the following reasons: 



Now that we know why do we need to do these operations, let’s see how can we do it in PyTorch tensor.

CPU to GPU

To move our Tensors from CPU to GPU we use either one of these commands:



Tensor.cuda() 
Tensor.to("cuda")

Example: 

 

GPU to CPU 

Now for moving our Tensors from GPU to CPU, there are two conditions: 

  1. Tensor with required_grad = False, or
  2. Tensor with required_grad = True

Example 1: If required_grad = False, then you can simply do it as:

Tensor.cpu()

 

Example 2: If required_grad = True, then you need to use: 

Tensor.detach().cpu()

 

Article Tags :