PyTorch torch.block_diag-Funktion
PyTorch torch-Referenzhandbuch
torch.block_diagist eine Funktion in PyTorch, die zum Erstellen einer Blockdiagonalmatrix verwendet wird. Sie ordnet mehrere Eingabetensoren als diagonale Blöcke entlang der Diagonalen an, um eine größere Blockdiagonalmatrix zu bilden.
Funktionsdefinition
torch.block_diag(*tensors)
Anwendungsbeispiel
Beispiel
import torch
# Blockdiagonalmatrix erstellen
a = torch.tensor([[1, 2], [3, 4]])
b = torch.tensor([[5, 6, 7]])
c = torch.tensor([[8], [9]])
result = torch.block_diag(a, b, c)
print("Blockdiagonalmatrix:")
print(result)
# tensor([[1, 2, 0, 0, 0],
# [3, 4, 0, 0, 0],
# [0, 0, 5, 6, 7],
# [0, 0, 8, 0, 0],
# [0, 0, 9, 0, 0]])
# Mehrere Matrixblöcke
m1 = torch.eye(2)
m2 = torch.eye(3)
result2 = torch.block_diag(m1, m2)
print("Blockdiagonale zweier Einheitsmatrizen:")
print(result2)
# Blockdiagonalmatrix erstellen
a = torch.tensor([[1, 2], [3, 4]])
b = torch.tensor([[5, 6, 7]])
c = torch.tensor([[8], [9]])
result = torch.block_diag(a, b, c)
print("Blockdiagonalmatrix:")
print(result)
# tensor([[1, 2, 0, 0, 0],
# [3, 4, 0, 0, 0],
# [0, 0, 5, 6, 7],
# [0, 0, 8, 0, 0],
# [0, 0, 9, 0, 0]])
# Mehrere Matrixblöcke
m1 = torch.eye(2)
m2 = torch.eye(3)
result2 = torch.block_diag(m1, m2)
print("Blockdiagonale zweier Einheitsmatrizen:")
print(result2)
Andere Erweiterungen