PyTorch torch.result_type 関数
torch.result_typePyTorch において、操作結果の型を決定するために使用される関数です。テンソルまたはスカラーを入力として受け取り、関連する操作を実行した後に使用すべきデータ型を返します。
関数定義
torch.result_type(tensor, scalar)
パラメータの説明
tensor: 入力テンソルまたはデータ型scalar: スカラー値または別のテンソル
使用例
例
import torch
# 異なる型のテンソルを作成
a = torch.tensor([1, 2, 3], dtype=torch.float32)
b = torch.tensor([4, 5, 6], dtype=torch.float64)
# 結果の型を取得
result_dtype = torch.result_type(a, b)
print("float32 と float64 の結果型:", result_dtype)
# スカラーを使用
c = torch.tensor([1, 2, 3], dtype=torch.int32)
result_dtype2 = torch.result_type(c, 1.5)
print("int32 と 1.5 の結果型:", result_dtype2)
# 異なる型のテンソルを作成
a = torch.tensor([1, 2, 3], dtype=torch.float32)
b = torch.tensor([4, 5, 6], dtype=torch.float64)
# 結果の型を取得
result_dtype = torch.result_type(a, b)
print("float32 と float64 の結果型:", result_dtype)
# スカラーを使用
c = torch.tensor([1, 2, 3], dtype=torch.int32)
result_dtype2 = torch.result_type(c, 1.5)
print("int32 と 1.5 の結果型:", result_dtype2)
出力結果は次のとおりです:
float32 和 float64 的结果类型: torch.float64 int32 和 1.5 的结果类型: torch.float32
その他の拡張
Pytorch torch リファレンスマニュアル