PyTorch torch.device function
PyTorch torch reference manual
torch.device is a PyTorch function for creating a device object, used to specify the device on which a tensor is stored.
Function definition
torch.device(device)
Usage examples
Example
import
torch
Create a CPU device
cpu_device
=
torch.
device
(
'cpu'
)
(
"CPU device:"
,
cpu_device
)
Create a CUDA device
if
torch.
cuda
.
is_available
(
)
:
cuda_device
=
torch.
device
(
'cuda:0'
)
(
"CUDA device:"
,
cuda_device
)
Create a tensor on the GPU
x
=
torch.
zeros
(
3
,
4
,
device
=
cuda_device
)
(
"Tensor device:"
,
x.
device
)