PyTorch torch.linalg.qr Funktion
PyTorch torch Referenzhandbuch
torch.linalg.qrEs ist eine Funktion im PyTorch-Modul für lineare Algebra, die verwendet wird, um die QR-Zerlegung einer Matrix zu berechnen. Die QR-Zerlegung zerlegt die Matrix A als A = Q * R, wobei Q eine orthogonale Matrix und R eine obere Dreiecksmatrix ist.
Funktionsdefinition
torch.linalg.qr(A, mode='reduced', out=None)
Parameters:
A(Tensor): Eingabematrix.mode(str, optional): 'reduced' gibt die reduzierte Zerlegung zurück, 'complete' gibt die vollständige Zerlegung zurück. Voreinstellung ist 'reduced'.out(tuple, optional): Ausgabetupel.
Rückgabewert:
tuple: Gibt das Tupel (Q, R) zurück.
Anwendungsbeispiel
Beispiel
import torch
# Matrix erstellen
A = torch.tensor([[12.0, -51.0, 4.0],
[6.0, 167.0, -68.0],
[-4.0, 24.0, -41.0]])
# QR-Zerlegung
Q, R = torch.linalg.qr(A)
print("Matrix A:")
print(A)
print("nOrthogonale Matrix Q:")
print(Q)
print("nObere Dreiecksmatrix R:")
print(R)
print("nÜberprüfung: Q @ R =")
print(Q @ R)
# Matrix erstellen
A = torch.tensor([[12.0, -51.0, 4.0],
[6.0, 167.0, -68.0],
[-4.0, 24.0, -41.0]])
# QR-Zerlegung
Q, R = torch.linalg.qr(A)
print("Matrix A:")
print(A)
print("nOrthogonale Matrix Q:")
print(Q)
print("nObere Dreiecksmatrix R:")
print(R)
print("nÜberprüfung: Q @ R =")
print(Q @ R)
Das Ausgabeergebnis ist:
矩阵 A:
tensor([[ 12., -51., 4.],
[ 6., 167., -68.],
[ -4., 24., -41.]])
正交矩阵 Q:
tensor([[-0.8571, 0.3943, 0.3314],
[-0.4286, -0.9029, -0.0343],
[ 0.2857, -0.1714, 0.9428]])
上三角矩阵 R:
tensor([[ -14., -21., 14.],
[ 0., -175., 70.],
[ 0., 0., -35.]])
验证: Q @ R =
tensor([[ 12., -51., 4.],
[ 6., 167., -68.],
[ -4., 24., -41.]])
Andere Erweiterungen