Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.device function

PyTorch torch.device function


PyTorch torch reference manual 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'

)

print

(

"CPU device:"

,

cpu_device

)

Create a CUDA device

if

torch.

cuda

.

is_available

(

)

:

    cuda_device

=

torch.

device

(

'cuda:0'

)

   

print

(

"CUDA device:"

,

cuda_device

)

   

Create a tensor on the GPU

    x

=

torch.

zeros

(

3

,

4

,

device

=

cuda_device

)

   

print

(

"Tensor device:"

,

x.

device

)


PyTorch torch reference manual PyTorch torch reference manual