PyTorch torch.dsplit Funktion
PyTorch torch Referenzhandbuch
torch.dsplitEs ist eine Funktion in PyTorch, mit der ein Tensor entlang der Tiefe (dritten Dimension) aufgeteilt wird.
Funktionsdefinition
torch.dsplit(input, indices_or_sections)
Anwendungsbeispiel
Beispiel
import torch
# Einen 3D-Tensor entlang der Tiefendimension aufteilen
x = torch.arange(24).reshape(2, 3, 4)
print("Ursprünglicher 3D-Tensor:")
print(x)
result = torch.dsplit(x, 2)
print("In 2 Teile entlang der Tiefe aufgeteilt:")
for i, t in enumerate(result):
print(f" Block {i}:n{t}")
# Nach Indizes aufteilen
result = torch.dsplit(x, [1, 3])
print("nAufteilung nach Indizes [1, 3]:")
for i, t in enumerate(result):
print(f" Block {i}:n{t}")
# 4D-Tensor entlang der Tiefendimension aufteilen
y = torch.arange(32).reshape(2, 2, 4, 2)
print("n4D Tensor-Form:", y.shape)
result = torch.dsplit(y, 2)
print("In 2 Teile entlang der dritten Dimension aufteilen:")
for i, t in enumerate(result):
print(f" Block {i} Form: {t.shape}")
# Einen 3D-Tensor entlang der Tiefendimension aufteilen
x = torch.arange(24).reshape(2, 3, 4)
print("Ursprünglicher 3D-Tensor:")
print(x)
result = torch.dsplit(x, 2)
print("In 2 Teile entlang der Tiefe aufgeteilt:")
for i, t in enumerate(result):
print(f" Block {i}:n{t}")
# Nach Indizes aufteilen
result = torch.dsplit(x, [1, 3])
print("nAufteilung nach Indizes [1, 3]:")
for i, t in enumerate(result):
print(f" Block {i}:n{t}")
# 4D-Tensor entlang der Tiefendimension aufteilen
y = torch.arange(32).reshape(2, 2, 4, 2)
print("n4D Tensor-Form:", y.shape)
result = torch.dsplit(y, 2)
print("In 2 Teile entlang der dritten Dimension aufteilen:")
for i, t in enumerate(result):
print(f" Block {i} Form: {t.shape}")
Das Ausgabeergebnis ist:
原始三维张量:
tensor([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
沿深度分为 2 份:
块 0:
tensor([[[ 0, 1],
[ 4, 5],
[ 8, 9]],
[[12, 13],
[16, 17],
[20, 21]]])
Chunk 1:
tensor([[[ 2, 3],
[ 6, 7],
[10, 11]],
[[14, 15],
[18, 19],
[22, 23]]])
按索引 [1, 3] 分割:
块 0:
tensor([[[ 0],
[ 4],
[ 8]],
[[12],
[16],
[20]]])
Chunk 1:
tensor([[[ 1, 2],
[ 5, 6],
[ 9, 10]],
[[13, 14],
[17, 18],
[21, 22]]])
Chunk 2:
tensor([[[ 3],
[ 7],
[11]],
[[15],
[19],
[23]]])
四维张量形状: torch.Size([2, 2, 4, 2])
沿第三维分为 2 份:
块 0 形状: torch.Size([2, 2, 2, 2])
Chunk 1 形状: torch.Size([2, 2, 2, 2])
Andere Erweiterungen