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

PyTorch torch.set_default_device function

PyTorch torch.set_default_device function


PyTorch torch reference manual PyTorch torch reference manual

torch.set_default_device is a function in PyTorch used to set the default device.

Function definition

torch.set_default_device(device)


Usage examples

Example

import

torch

Set the default device

if

torch.

cuda

.

is_available

(

)

:

    torch.

set_default_device

(

'cuda'

)

    x

=

torch.

zeros

(

3

,

4

)

   

print

(

"Default device:"

,

x.

device

)

    torch.

set_default_device

(

'cpu'

)

else

:

   

print

(

"CUDA is not available"

)


PyTorch torch reference manual PyTorch torch reference manual