PyTorch torch.can_cast Funktion
PyTorch torch Referenzhandbuch
torch.can_castEs ist eine Funktion in PyTorch, mit der überprüft wird, ob eine Typkonvertierung durchgeführt werden kann. Sie prüft, ob eine Konvertierung vom Quelldatentyp in den Zieldatentyp möglich ist.
Funktionsdefinition
torch.can_cast(from_dtype, to_dtype)
Anwendungsbeispiel
Beispiel
import torch
# Überprüfen, ob die Typkonvertierung durchgeführt werden kann
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
# Aus bool konvertieren
print(torch.can_cast(torch.bool, torch.float32)) # True
# Überprüfen, ob die Typkonvertierung durchgeführt werden kann
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
# Aus bool konvertieren
print(torch.can_cast(torch.bool, torch.float32)) # True
Andere Erweiterungen