PyTorch torch.vsplit Funktion
PyTorch torch Referenzhandbuch
torch.vsplitist eine Funktion in PyTorch, die verwendet wird, um Tensoren vertikal (entlang von Zeilen) zu teilen.
Funktionsdefinition
torch.vsplit(input, indices_or_sections)
Anwendungsbeispiel
Beispiel
import torch
# Vertikale Aufteilung eines 2D-Tensors
x = torch.arange(12).reshape(4, 3)
print("Originaler 2D-Tensor:")
print(x)
result = torch.vsplit(x, 2)
print("Gleichmäßig in 2 Teile entlang der Zeilen aufteilen:")
for i, t in enumerate(result):
print(f" Block {i}:n{t}")
# Nach Index aufteilen
result = torch.vsplit(x, [1, 3])
print("nAufteilen nach Index [1, 3]:")
for i, t in enumerate(result):
print(f" Block {i}:n{t}")
# Vertikale Aufteilung eines 3D-Tensors
y = torch.arange(24).reshape(4, 3, 2)
print("n3D-Tensor:")
print(y)
result = torch.vsplit(y, 2)
print("nAufteilen in 2 Teile entlang der ersten Dimension:")
for i, t in enumerate(result):
print(f" Block {i}: {t.shape}")
# Vertikale Aufteilung eines 2D-Tensors
x = torch.arange(12).reshape(4, 3)
print("Originaler 2D-Tensor:")
print(x)
result = torch.vsplit(x, 2)
print("Gleichmäßig in 2 Teile entlang der Zeilen aufteilen:")
for i, t in enumerate(result):
print(f" Block {i}:n{t}")
# Nach Index aufteilen
result = torch.vsplit(x, [1, 3])
print("nAufteilen nach Index [1, 3]:")
for i, t in enumerate(result):
print(f" Block {i}:n{t}")
# Vertikale Aufteilung eines 3D-Tensors
y = torch.arange(24).reshape(4, 3, 2)
print("n3D-Tensor:")
print(y)
result = torch.vsplit(y, 2)
print("nAufteilen in 2 Teile entlang der ersten Dimension:")
for i, t in enumerate(result):
print(f" Block {i}: {t.shape}")
Das Ausgabeergebnis ist:
原始二维张量:
tensor([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
沿行平均分为 2 份:
块 0:
tensor([[0, 1, 2],
[3, 4, 5]])
Chunk 1:
tensor([[ 6, 7, 8],
[ 9, 10, 11]])
按索引 [1, 3] 分割:
块 0:
tensor([[0, 1, 2]])
Chunk 1:
tensor([[3, 4, 5],
[6, 7, 8]])
Chunk 2:
tensor([[ 9, 10, 11]])
三维张量:
tensor([[[ 0, 1],
[ 3, 4],
[ 5, 6]],
[[ 7, 8],
[ 9, 10],
[11, 12]],
[[13, 14],
[15, 16],
[17, 18]],
[[19, 20],
[21, 22],
[23, 24]]])
沿第一维分为 2 份:
块 0: torch.Size([2, 3, 2])
Chunk 1: torch.Size([2, 3, 2])
Andere Erweiterungen