PyTorch torch.tensor_split Funktion
PyTorch torch-Referenzhandbuch
torch.tensor_splitEs ist eine Funktion in PyTorch, mit der Tensoren nach Index oder Anzahl der Segmente aufgeteilt werden.
Funktionsdefinition
torch.tensor_split(input, indices_or_sections, dim=0)
Anwendungsbeispiel
Beispiel
import torch
x = torch.arange(12)
print("Original-Tensor:")
print(x)
# Nach Index aufteilen
result = torch.tensor_split(x, [2, 5, 8])
print("Aufteilen nach Indizes [2, 5, 8]:")
for i, t in enumerate(result):
print(f" Block {i}: {t}")
# Nach Anzahl der Segmente aufteilen (gleichmäßig in 3 Teile)
y = torch.arange(9)
result = torch.tensor_split(y, 3)
print("nDurchschnittliche Aufteilung in 3 Teile:")
for i, t in enumerate(result):
print(f" Block {i}: {t}")
# 2D-Tensor nach Spalten aufteilen
z = torch.arange(12).reshape(3, 4)
print("n2D Tensor:")
print(z)
result = torch.tensor_split(z, 2, dim=1)
print("In 2 Teile nach Spalte aufteilen:")
for i, t in enumerate(result):
print(f" Block {i}:n{t}")
x = torch.arange(12)
print("Original-Tensor:")
print(x)
# Nach Index aufteilen
result = torch.tensor_split(x, [2, 5, 8])
print("Aufteilen nach Indizes [2, 5, 8]:")
for i, t in enumerate(result):
print(f" Block {i}: {t}")
# Nach Anzahl der Segmente aufteilen (gleichmäßig in 3 Teile)
y = torch.arange(9)
result = torch.tensor_split(y, 3)
print("nDurchschnittliche Aufteilung in 3 Teile:")
for i, t in enumerate(result):
print(f" Block {i}: {t}")
# 2D-Tensor nach Spalten aufteilen
z = torch.arange(12).reshape(3, 4)
print("n2D Tensor:")
print(z)
result = torch.tensor_split(z, 2, dim=1)
print("In 2 Teile nach Spalte aufteilen:")
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, 10, 11])
按索引 [2, 5, 8] 分割:
块 0: tensor([0, 1])
Chunk 1: tensor([2, 3, 4])
Chunk 2: tensor([5, 6, 7])
Chunk 3: tensor([ 8, 9, 10, 11])
平均分为 3 份:
块 0: tensor([0, 1, 2])
Chunk 1: tensor([3, 4, 5])
Chunk 2: tensor([6, 7, 8])
二维张量:
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]])
Andere Erweiterungen