PyTorch torch.dtype 関数
torch.dtypePyTorch においてテンソルのデータ型を表すために使用されるオブジェクトであり、関数ではありません。テンソルのデータ型を取得または指定するために使用されます。
データ型の説明
torch.float32 - 32位浮点数 torch.float64 - 64位浮点数(双精度) torch.float16 - 16位浮点数(半精度) torch.bfloat16 - 16位浮点数(Brain Float) torch.complex32 - 32位复数 torch.complex64 - 64位复数 torch.complex128 - 128位复数 torch.int8 - 8位整数 torch.int16 - 16位整数 torch.int32 - 32位整数 torch.int64 - 64位整数 torch.bool - 布尔类型 torch.uint8 - 无符号8位整数
使用例
実例
import torch
# 異なるデータ型のテンソルを作成
x1 = torch.tensor([1.0, 2.0, 3.0])
x2 = torch.tensor([1, 2, 3], dtype=torch.int32)
x3 = torch.tensor([True, False, True], dtype=torch.bool)
# テンソルのデータ型を取得
print("x1 dtype:", x1.dtype)
print("x2 dtype:", x2.dtype)
print("x3 dtype:", x3.dtype)
# 異なるデータ型のテンソルを作成
x1 = torch.tensor([1.0, 2.0, 3.0])
x2 = torch.tensor([1, 2, 3], dtype=torch.int32)
x3 = torch.tensor([True, False, True], dtype=torch.bool)
# テンソルのデータ型を取得
print("x1 dtype:", x1.dtype)
print("x2 dtype:", x2.dtype)
print("x3 dtype:", x3.dtype)
その他の拡張
Pytorch torch リファレンスマニュアル