In [1]:
import torch
In [2]:
x = torch.tensor(
[
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
]
)
x
Out[2]:
Tips¶
The methods
Tensor.cpu
,Tensor.cuda
andTensor.to
are not in-palce. Instead, they return new copies of Tensors!There are basicially 2 ways to move a tensor and a module (notice that a model is a model too) to a specific device in PyTorch. The first (old) way is to call the methods
Tensor.cpu
and/orTensor.cuda
. The second (new) way is to call the methodTensor.to
.Tensor.to
is preferred overTensor.cpu
/Tensor.cuda
as it is more flexible while almost as easy to use.
In [5]:
x.cpu()
Out[5]:
In [6]:
x.cuda()
In [3]:
x.to("cpu")
Out[3]:
In [9]:
x.to(torch.device("cpu:0"))
Out[9]:
In [4]:
x.to("cuda")
In [10]:
x.to(torch.device("cuda:1"))
In [ ]: