PyTorch torch.vstack Funktion
PyTorch torch Referenzhandbuch
torch.vstackist eine Funktion in PyTorch, die verwendet wird, um Tensoren vertikal (entlang der Zeilen) zu stapeln.
Funktionsdefinition
torch.vstack(tensors, *, out=None)
Anwendungsbeispiel
Beispiel
import torch
# 1D-Tensoren vertikal stapeln
x1 = torch.tensor([1, 2, 3])
x2 = torch.tensor([4, 5, 6])
result = torch.vstack([x1, x2])
print("1D-Tensoren vertikal gestapelt:")
print(f" x1: {x1}")
print(f" x2: {x2}")
print(f" vstack:n{result}")
# 2D-Tensoren vertikal stapeln
y1 = torch.tensor([[1, 2], [3, 4]])
y2 = torch.tensor([[5, 6], [7, 8]])
result = torch.vstack([y1, y2])
print("\n2D-Tensoren vertikal gestapelt:")
print(f" y1:n{y1}")
print(f" y2:n{y2}")
print(f" vstack:n{result}")
# Mehrere Tensoren stapeln
z1 = torch.tensor([1, 2, 3])
z2 = torch.tensor([4, 5, 6])
z3 = torch.tensor([7, 8, 9])
result = torch.vstack([z1, z2, z3])
print("\nMehrere 1D-Tensoren gestapelt:")
print(f" result:n{result}")
# 1D-Tensoren vertikal stapeln
x1 = torch.tensor([1, 2, 3])
x2 = torch.tensor([4, 5, 6])
result = torch.vstack([x1, x2])
print("1D-Tensoren vertikal gestapelt:")
print(f" x1: {x1}")
print(f" x2: {x2}")
print(f" vstack:n{result}")
# 2D-Tensoren vertikal stapeln
y1 = torch.tensor([[1, 2], [3, 4]])
y2 = torch.tensor([[5, 6], [7, 8]])
result = torch.vstack([y1, y2])
print("\n2D-Tensoren vertikal gestapelt:")
print(f" y1:n{y1}")
print(f" y2:n{y2}")
print(f" vstack:n{result}")
# Mehrere Tensoren stapeln
z1 = torch.tensor([1, 2, 3])
z2 = torch.tensor([4, 5, 6])
z3 = torch.tensor([7, 8, 9])
result = torch.vstack([z1, z2, z3])
print("\nMehrere 1D-Tensoren gestapelt:")
print(f" result:n{result}")
Das Ausgabeergebnis ist:
一维张量垂直堆叠:
x1: tensor([1, 2, 3])
x2: tensor([4, 5, 6])
vstack:
tensor([[1, 2, 3],
[4, 5, 6]])
二维张量垂直堆叠:
y1:
tensor([[1, 2],
[3, 4]])
y2:
tensor([[5, 6],
[7, 8]])
vstack:
tensor([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
多个一维张量堆叠:
result:
tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
Andere Erweiterungen