Fonction torch.row_stack de PyTorch
Manuel de référence Pytorch torch
torch.row_stackC'est une fonction de PyTorch utilisée pour empiler des tenseurs par lignes, équivalent à un empilement vertical.
Définition de fonction
torch.row_stack(tensors, *, out=None)
Exemple d'utilisation
Exemple
import torch
# Empilement en lignes de tenseurs 1D
x1 = torch.tensor([1, 2, 3])
x2 = torch.tensor([4, 5, 6])
result = torch.row_stack([x1, x2])
print("Empilement en lignes de tenseurs 1D :")
print(f" x1: {x1}")
print(f" x2: {x2}")
print(f" row_stack:n{result}")
# Empilement en lignes de tenseurs 2D
y1 = torch.tensor([[1, 2, 3]])
y2 = torch.tensor([[4, 5, 6]])
result = torch.row_stack([y1, y2])
print("\nEmpilement en lignes de tenseurs 2D :")
print(f" y1:n{y1}")
print(f" y2:n{y2}")
print(f" row_stack:n{result}")
# Équivalent à vstack
z1 = torch.tensor([1, 2, 3])
z2 = torch.tensor([4, 5, 6])
result_vstack = torch.vstack([z1, z2])
result_row_stack = torch.row_stack([z1, z2])
print("\nrow_stack est équivalent à vstack :")
print(f" vstack: {result_vstack.tolist()}")
print(f" row_stack: {result_row_stack.tolist()}")
# Empilement en lignes de tenseurs 1D
x1 = torch.tensor([1, 2, 3])
x2 = torch.tensor([4, 5, 6])
result = torch.row_stack([x1, x2])
print("Empilement en lignes de tenseurs 1D :")
print(f" x1: {x1}")
print(f" x2: {x2}")
print(f" row_stack:n{result}")
# Empilement en lignes de tenseurs 2D
y1 = torch.tensor([[1, 2, 3]])
y2 = torch.tensor([[4, 5, 6]])
result = torch.row_stack([y1, y2])
print("\nEmpilement en lignes de tenseurs 2D :")
print(f" y1:n{y1}")
print(f" y2:n{y2}")
print(f" row_stack:n{result}")
# Équivalent à vstack
z1 = torch.tensor([1, 2, 3])
z2 = torch.tensor([4, 5, 6])
result_vstack = torch.vstack([z1, z2])
result_row_stack = torch.row_stack([z1, z2])
print("\nrow_stack est équivalent à vstack :")
print(f" vstack: {result_vstack.tolist()}")
print(f" row_stack: {result_row_stack.tolist()}")
Le résultat de sortie est:
一维张量行堆叠:
x1: tensor([1, 2, 3])
x2: tensor([4, 5, 6])
row_stack:
tensor([[1, 2, 3],
[4, 5, 6]])
二维张量行堆叠:
y1:
tensor([[1, 2, 3]])
y2:
tensor([[4, 5, 6]])
row_stack:
tensor([[1, 2, 3],
[4, 5, 6]])
row_stack 等价于 vstack:
vstack: [[1, 2, 3], [4, 5, 6]]
row_stack: [[1, 2, 3], [4, 5, 6]]
Autres extensions