PyTorch torch.dtype Function


Pytorch torch 参考手册Pytorch torch Reference Manual

torch.dtypeIt is an object in PyTorch used to represent the data type of a tensor, not a function. It is used to get or specify the data type of a tensor.

Data Type Description

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位整数

Usage Examples

Example

import torch

# Create tensors of different data types
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)

# Get the data type of a tensor
print("x1 dtype:", x1.dtype)
print("x2 dtype:", x2.dtype)
print("x3 dtype:", x3.dtype)

Pytorch torch 参考手册Pytorch torch Reference Manual

Other Extensions