La función torch.dtype de PyTorch
Manual de referencia de PyTorch torch
torch.dtypeEs un objeto en PyTorch utilizado para representar el tipo de dato de los tensores, no una función. Se usa para obtener o especificar el tipo de dato de un tensor.
Descripción de los tipos de datos
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位整数
Ejemplo de uso
Ejemplo
import torch
# Crear tensores de diferentes tipos de datos
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)
# Obtener el tipo de dato de un tensor
print("x1 dtype:", x1.dtype)
print("x2 dtype:", x2.dtype)
print("x3 dtype:", x3.dtype)
# Crear tensores de diferentes tipos de datos
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)
# Obtener el tipo de dato de un tensor
print("x1 dtype:", x1.dtype)
print("x2 dtype:", x2.dtype)
print("x3 dtype:", x3.dtype)
Otras extensiones