PyTorch torch.can_cast function
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
(
torch.
can_cast
(
torch.
float32
,
torch.
int32
)
)
True
(
torch.
can_cast
(
torch.
float64
,
torch.
int8
)
)
True
(
torch.
can_cast
(
torch.
int64
,
torch.
bool
)
)
True
Convert from bool
(
torch.
can_cast
(
torch.
bool
,
torch.
float32
)
)