PyTorch torch.can_cast 関数
torch.can_castPyTorch で型変換が実行可能かどうかをチェックするための関数です。ソースデータ型からターゲットデータ型に変換できるかどうかをチェックします。
関数定義
torch.can_cast(from_dtype, to_dtype)
使用例
実例
import torch
# 型変換が実行可能かどうかをチェック
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
# bool からの変換
print(torch.can_cast(torch.bool, torch.float32)) # True
# 型変換が実行可能かどうかをチェック
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
# bool からの変換
print(torch.can_cast(torch.bool, torch.float32)) # True
その他の拡張
Pytorch torch リファレンスマニュアル