PyTorch torch.hsplit-Funktion
Pytorch torch-Referenzhandbuch
torch.hsplitEs ist eine Funktion in PyTorch, die verwendet wird, um Tensoren horizontal (entlang der Spalten) aufzuteilen.
Funktionsdefinition
torch.hsplit(input, indices_or_sections)
Anwendungsbeispiel
Beispiel
import torch
# Horizontale Aufteilung eines 1D-Tensors
x = torch.arange(10)
print("Originaler 1D-Tensor:")
print(x)
result = torch.hsplit(x, 2)
print("Aufgeteilt in 2 gleiche Teile:")
for i, t in enumerate(result):
print(f" Block {i}: {t}")
# Horizontale Aufteilung eines 2D-Tensors
y = torch.arange(12).reshape(3, 4)
print("\nOriginaler 2D-Tensor:")
print(y)
result = torch.hsplit(y, 2)
print("Aufgeteilt in 2 Teile entlang der Spalten:")
for i, t in enumerate(result):
print(f" Block {i}:\n{t}")
# Nach Indizes aufteilen
result = torch.hsplit(y, [1, 3])
print("\nNach Indizes [1, 3] aufgeteilt:")
for i, t in enumerate(result):
print(f" Block {i}:\n{t}")
# Horizontale Aufteilung eines 1D-Tensors
x = torch.arange(10)
print("Originaler 1D-Tensor:")
print(x)
result = torch.hsplit(x, 2)
print("Aufgeteilt in 2 gleiche Teile:")
for i, t in enumerate(result):
print(f" Block {i}: {t}")
# Horizontale Aufteilung eines 2D-Tensors
y = torch.arange(12).reshape(3, 4)
print("\nOriginaler 2D-Tensor:")
print(y)
result = torch.hsplit(y, 2)
print("Aufgeteilt in 2 Teile entlang der Spalten:")
for i, t in enumerate(result):
print(f" Block {i}:\n{t}")
# Nach Indizes aufteilen
result = torch.hsplit(y, [1, 3])
print("\nNach Indizes [1, 3] aufgeteilt:")
for i, t in enumerate(result):
print(f" Block {i}:\n{t}")
Das Ausgabeergebnis ist:
原始一维张量:
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
平均分为 2 份:
块 0: tensor([0, 1, 2, 3, 4])
Chunk 1: tensor([5, 6, 7, 8, 9])
原始二维张量:
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
沿列分为 2 份:
块 0:
tensor([[0, 1],
[4, 5],
[8, 9]])
Chunk 1:
tensor([[ 2, 3],
[ 6, 7],
[10, 11]])
按索引 [1, 3] 分割:
块 0:
tensor([[0],
[4],
[8]])
Chunk 1:
tensor([[ 1, 2],
[ 5, 6],
[ 9, 10]])
Chunk 2:
tensor([[ 3],
[ 7],
[11]])
Weitere Erweiterungen