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

PyTorch torch.can_cast function

PyTorch torch.can_cast function


PyTorch torch reference manual PyTorch torch reference manual

torch.can_cast is a function in PyTorch used to check whether a type conversion can be performed. It checks whether conversion from a source data type to a target data type is possible.

Function definition

torch.can_cast(from_dtype, to_dtype)


Usage examples

Example

import

torch

Check whether a type conversion can be performed

print

(

torch.

can_cast

(

torch.

float32

,

torch.

int32

)

)

 

True

print

(

torch.

can_cast

(

torch.

float64

,

torch.

int8

)

)

   

True

print

(

torch.

can_cast

(

torch.

int64

,

torch.

bool

)

)

     

True

Convert from bool

print

(

torch.

can_cast

(

torch.

bool

,

torch.

float32

)

)

   

True


PyTorch torch reference manual PyTorch torch reference manual