Fonction torch.linalg.svd de PyTorch
Manuel de référence PyTorch torch
torch.linalg.svdest une fonction du module d'algèbre linéaire de PyTorch utilisée pour calculer la décomposition en valeurs singulières (SVD) d'une matrice. La SVD décompose une matrice comme A = U * diag(S) * V^T.
Définition de fonction
torch.linalg.svd(A, full_matrices=False, out=None)
Parameters:
A(Tensor) : La matrice d'entrée.full_matrices(bool, optionnel) : Si True, renvoie les matrices U et V complètes. La valeur par défaut est False.out(tuple, facultatif) : Tuple de sortie.
Valeur de retour:
tuple: Renvoie un tuple (U, S, Vh), où Vh est la transposée de V.
Exemple d'utilisation
Exemple
import torch
# Créer une matrice
A = torch.tensor([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0],
[10.0, 11.0, 12.0]])
# Décomposition SVD
U, S, Vh = torch.linalg.svd(A)
print("Matrice A :")
print(A)
print("forme de nU :", U.shape)
print("Valeurs singulières S :", S)
print("forme de Vh :", Vh.shape)
print("nVérification : U @ diag(S) @ Vh =")
print(U @ torch.diag(S) @ Vh)
# Créer une matrice
A = torch.tensor([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0],
[10.0, 11.0, 12.0]])
# Décomposition SVD
U, S, Vh = torch.linalg.svd(A)
print("Matrice A :")
print(A)
print("forme de nU :", U.shape)
print("Valeurs singulières S :", S)
print("forme de Vh :", Vh.shape)
print("nVérification : U @ diag(S) @ Vh =")
print(U @ torch.diag(S) @ Vh)
Le résultat de sortie est:
矩阵 A:
tensor([[ 1., 2., 3.],
[ 4., 5., 6.],
[ 7., 8., 9.],
[10., 11., 12.]])
U 形状: torch.Size([4, 4])
奇异值 S: tensor([25.4627, 1.2907, 0.0000])
Vh 形状: torch.Size([3, 3])
验证: U @ diag(S) @ Vh =
tensor([[ 1.0000, 2.0000, 3.0000],
[ 4.0000, 5.0000, 6.0000],
[ 7.0000, 8.0000, 9.0000],
[10.0000, 11.0000, 12.0000]])
Autres extensions