,
Manuel de référence PyTorch torch
torch.atleast_3dest une fonction de PyTorch utilisée pour convertir les tenseurs d'entrée en au moins 3 dimensions. Si l'entrée a moins de 3 dimensions, des dimensions sont automatiquement ajoutées pour la rendre tridimensionnelle.
]]) Forme : torch.Size([3, 1, 1])
torch.atleast_3d(*tensors)
Exemple d'utilisation
]]) Forme : torch.Size([1, 1, 1])
import torch
# Convertir un scalaire en tenseur 3D
x = torch.atleast_3d(5)
print("Après conversion du scalaire :", x, "Forme :", x.shape)
# Sortie : Après conversion scalaire : tensor([[
# Convertir un tenseur 1D en 3D
x = torch.tensor([1, 2, 3])
y = torch.atleast_3d(x)
print("1D en 3D :", y, "Forme :", y.shape)
# Sortie : 1D en 3D : tensor([[
# Convertir un tenseur 2D en 3D
x = torch.tensor([[1, 2], [3, 4]])
y = torch.atleast_3d(x)
print("2D en 3D :", y, "Forme :", y.shape)
# Sortie : 2D en 3D : tensor([[[1, 2], [3, 4]]]) Forme : torch.Size([1, 2, 2])
# Un tenseur déjà en 3D reste inchangé
x = torch.tensor([[[1, 2], [3, 4]]])
y = torch.atleast_3d(x)
print("tenseur 3D :", y, "Forme :", y.shape)
# Convertir un scalaire en tenseur 3D
x = torch.atleast_3d(5)
print("Après conversion du scalaire :", x, "Forme :", x.shape)
# Sortie : Après conversion scalaire : tensor([[
# Convertir un tenseur 1D en 3D
x = torch.tensor([1, 2, 3])
y = torch.atleast_3d(x)
print("1D en 3D :", y, "Forme :", y.shape)
# Sortie : 1D en 3D : tensor([[
# Convertir un tenseur 2D en 3D
x = torch.tensor([[1, 2], [3, 4]])
y = torch.atleast_3d(x)
print("2D en 3D :", y, "Forme :", y.shape)
# Sortie : 2D en 3D : tensor([[[1, 2], [3, 4]]]) Forme : torch.Size([1, 2, 2])
# Un tenseur déjà en 3D reste inchangé
x = torch.tensor([[[1, 2], [3, 4]]])
y = torch.atleast_3d(x)
print("tenseur 3D :", y, "Forme :", y.shape)
Autres extensions