Fonction torch.dstack de PyTorch
Manuel de référence Pytorch torch
torch.dstackIl s'agit d'une fonction de PyTorch utilisée pour empiler des tenseurs selon la profondeur (troisième dimension).
Définition de fonction
torch.dstack(tensors, *, out=None)
Exemple d'utilisation
Exemple
import torch
# Empilement en profondeur de tenseurs 2D
x1 = torch.tensor([[1, 2], [3, 4]])
x2 = torch.tensor([[5, 6], [7, 8]])
result = torch.dstack([x1, x2])
print("Empilement en profondeur de tenseurs 2D :")
print(f" x1:n{x1}")
print(f" x2:n{x2}")
print(f" dstack:n{result}")
# Empilement en profondeur de tenseurs 3D
y1 = torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
y2 = torch.tensor([[[9, 10], [11, 12]], [[13, 14], [15, 16]]])
result = torch.dstack([y1, y2])
print("n3D empilement en profondeur de tenseurs :")
print(f" y1:n{y1}")
print(f" y2:n{y2}")
print(f" dstack:n{result}")
print(f" forme de dstack : {result.shape}")
# Empilement en profondeur de tenseurs 1D
z1 = torch.tensor([1, 2, 3])
z2 = torch.tensor([4, 5, 6])
result = torch.dstack([z1, z2])
print("n1D empilement en profondeur de tenseurs :")
print(f" z1: {z1}")
print(f" z2: {z2}")
print(f" dstack:n{result}")
# Empilement en profondeur de tenseurs 2D
x1 = torch.tensor([[1, 2], [3, 4]])
x2 = torch.tensor([[5, 6], [7, 8]])
result = torch.dstack([x1, x2])
print("Empilement en profondeur de tenseurs 2D :")
print(f" x1:n{x1}")
print(f" x2:n{x2}")
print(f" dstack:n{result}")
# Empilement en profondeur de tenseurs 3D
y1 = torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
y2 = torch.tensor([[[9, 10], [11, 12]], [[13, 14], [15, 16]]])
result = torch.dstack([y1, y2])
print("n3D empilement en profondeur de tenseurs :")
print(f" y1:n{y1}")
print(f" y2:n{y2}")
print(f" dstack:n{result}")
print(f" forme de dstack : {result.shape}")
# Empilement en profondeur de tenseurs 1D
z1 = torch.tensor([1, 2, 3])
z2 = torch.tensor([4, 5, 6])
result = torch.dstack([z1, z2])
print("n1D empilement en profondeur de tenseurs :")
print(f" z1: {z1}")
print(f" z2: {z2}")
print(f" dstack:n{result}")
Résultat de sortie :
二维张量深度堆叠:
x1:
tensor([[1, 2],
[3, 4]])
x2:
tensor([[5, 6],
[7, 8]])
dstack:
tensor([[[ 1, 5],
[ 2, 6]],
[[ 3, 7],
[ 4, 8]]])
三维张量深度堆叠:
y1:
tensor([[[ 1, 2],
[ 3, 4]],
[[ 5, 6],
[ 7, 8]]])
y2:
tensor([[[ 9, 10],
[11, 12]],
[[13, 14],
[15, 16]]])
dstack:
tensor([[[ 1, 2, 9, 10],
[ 3, 4, 11, 12]],
[[ 5, 6, 13, 14],
[ 7, 8, 15, 16]]])
dstack 形状: torch.Size([2, 2, 4])
一维张量深度堆叠:
z1: tensor([1, 2, 3])
z2: tensor([4, 5, 6])
dstack:
tensor([[1, 4],
[2, 5],
[3, 6]])
Autres extensions