Fonction torch.qr de PyTorch
Manuel de référence PyTorch torch
torch.qrest une fonction de PyTorch utilisée pour calculer la décomposition QR d'une matrice. La décomposition QR décompose la matrice A en A = Q * R, où Q est une matrice orthogonale et R est une matrice triangulaire supérieure.
Définition de fonction
torch.qr(input, out=None)
Parameters:
input(Tensor) : La matrice d'entrée.out(tuple, facultatif) : Le tuple de sortie.
Valeur de retour:
tuple: Renvoie un tuple (Q, R).
Exemple d'utilisation
Exemple
import torch
# Créer une matrice
A = torch.tensor([[12.0, -51.0, 4.0],
[6.0, 167.0, -68.0],
[-4.0, 24.0, -41.0]])
# Décomposition QR
Q, R = torch.qr(A)
print("Matrice A :")
print(A)
print("nMatrice orthogonale Q:")
print(Q)
print("nMatrice triangulaire supérieure R:")
print(R)
print("nVérification: Q @ R =")
print(Q @ R)
# Créer une matrice
A = torch.tensor([[12.0, -51.0, 4.0],
[6.0, 167.0, -68.0],
[-4.0, 24.0, -41.0]])
# Décomposition QR
Q, R = torch.qr(A)
print("Matrice A :")
print(A)
print("nMatrice orthogonale Q:")
print(Q)
print("nMatrice triangulaire supérieure R:")
print(R)
print("nVérification: Q @ R =")
print(Q @ R)
Le résultat de sortie est:
矩阵 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.]])
Autres extensions