PyTorch torch.row_stack Funktion
PyTorch torch Referenzhandbuch
torch.row_stackEs ist eine Funktion in PyTorch, die zum zeilenweisen Stapeln von Tensoren verwendet wird, äquivalent zum vertikalen Stapeln.
Funktionsdefinition
torch.row_stack(tensors, *, out=None)
Anwendungsbeispiel
Beispiel
import torch
# Zeilenweises Stapeln von 1D-Tensoren
x1 = torch.tensor([1, 2, 3])
x2 = torch.tensor([4, 5, 6])
result = torch.row_stack([x1, x2])
print("Zeilenstapelung von 1D-Tensoren:")
print(f" x1: {x1}")
print(f" x2: {x2}")
print(f" row_stack:n{result}")
# Zeilenweises Stapeln von 2D-Tensoren
y1 = torch.tensor([[1, 2, 3]])
y2 = torch.tensor([[4, 5, 6]])
result = torch.row_stack([y1, y2])
print("\nZeilenstapelung von 2D-Tensoren:")
print(f" y1:n{y1}")
print(f" y2:n{y2}")
print(f" row_stack:n{result}")
# Entspricht 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 ist äquivalent zu vstack:")
print(f" vstack: {result_vstack.tolist()}")
print(f" row_stack: {result_row_stack.tolist()}")
# Zeilenweises Stapeln von 1D-Tensoren
x1 = torch.tensor([1, 2, 3])
x2 = torch.tensor([4, 5, 6])
result = torch.row_stack([x1, x2])
print("Zeilenstapelung von 1D-Tensoren:")
print(f" x1: {x1}")
print(f" x2: {x2}")
print(f" row_stack:n{result}")
# Zeilenweises Stapeln von 2D-Tensoren
y1 = torch.tensor([[1, 2, 3]])
y2 = torch.tensor([[4, 5, 6]])
result = torch.row_stack([y1, y2])
print("\nZeilenstapelung von 2D-Tensoren:")
print(f" y1:n{y1}")
print(f" y2:n{y2}")
print(f" row_stack:n{result}")
# Entspricht 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 ist äquivalent zu vstack:")
print(f" vstack: {result_vstack.tolist()}")
print(f" row_stack: {result_row_stack.tolist()}")
Das Ausgabeergebnis ist:
一维张量行堆叠:
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]]
Andere Erweiterungen