PyTorch torch.can_cast Function


Pytorch torch 参考手册Pytorch torch Reference Manual

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

Function Definition

torch.can_cast(from_dtype, to_dtype)

Usage Example

Example

import torch

# Check whether the 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 参考手册Pytorch torch Reference Manual

Other Extensions