PyTorch torch.can_cast función
Pytorch torch manual de referencia
torch.can_castEs una función en PyTorch utilizada para comprobar si una conversión de tipo se puede ejecutar. Comprueba si se puede convertir desde el tipo de datos de origen al tipo de datos de destino.
Definición de la función
torch.can_cast(from_dtype, to_dtype)
Ejemplo de uso
Ejemplo
import torch
# Comprueba si la conversión de tipo es ejecutable
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
# Convertir desde bool
print(torch.can_cast(torch.bool, torch.float32)) # True
# Comprueba si la conversión de tipo es ejecutable
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
# Convertir desde bool
print(torch.can_cast(torch.bool, torch.float32)) # True
Otras extensiones