PyTorch torch.lu Funktion
PyTorch torch Referenzhandbuch
torch.luEs ist eine Funktion in PyTorch, die zur Berechnung der LU-Zerlegung einer Matrix verwendet wird. Die LU-Zerlegung zerlegt eine Matrix A in A = P * L * U, wobei L eine untere Dreiecksmatrix, U eine obere Dreiecksmatrix und P eine Permutationsmatrix ist.
Funktionsdefinition
torch.lu(A, pivot=True, get_infos=False, out=None)
Parameters:
A(Tensor): Eingabematrix.pivot(bool, optional): Gibt an, ob die LU-Zerlegung mit Spaltenpivotisierung durchgeführt werden soll. Standardwert ist True.get_infos(bool, optional): Wenn True, Informationen zurückgeben. Standardwert ist False.out(tuple, optional): Ausgabetupel.
Rückgabewert:
tuple: Gibt das Tupel (pivot, L, U) zurück.
Anwendungsbeispiele
Beispiel
import torch
# Matrix erstellen
A = torch.tensor([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0]])
# LU-Zerlegung
LU, pivots = torch.lu(A, pivot=True)
print("Matrix A:")
print(A)
print("\nLU-Matrix:")
print(LU)
print("\nPivot-Indizes:")
print(pivots)
# Matrix erstellen
A = torch.tensor([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0]])
# LU-Zerlegung
LU, pivots = torch.lu(A, pivot=True)
print("Matrix A:")
print(A)
print("\nLU-Matrix:")
print(LU)
print("\nPivot-Indizes:")
print(pivots)
Das Ausgabeergebnis ist:
矩阵 A:
tensor([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]])
LU 矩阵:
tensor([[7., 8., 9.],
[0.2, 0.4, 0.6],
[0.6, 0.8, 0.0]])
主元索引:
tensor([3, 3, 3], dtype=torch.int32)
Beispiel - Verwendung von get_infos
import torch
A = torch.tensor([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0]])
# LU-Zerlegung, einschließlich Informationen
LU, pivots, info = torch.lu(A, pivot=True, get_infos=True)
print("Information:", info)
A = torch.tensor([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0]])
# LU-Zerlegung, einschließlich Informationen
LU, pivots, info = torch.lu(A, pivot=True, get_infos=True)
print("Information:", info)
Andere Erweiterungen