PyTorch torch.linalg.svd Funktion
PyTorch torch-Referenzhandbuch
torch.linalg.svdist eine Funktion im PyTorch-Modul für lineare Algebra, die zur Berechnung der Singulärwertzerlegung (SVD) einer Matrix verwendet wird. SVD zerlegt eine Matrix als A = U * diag(S) * V^T.
Funktionsdefinition
torch.linalg.svd(A, full_matrices=False, out=None)
Parameters:
A(Tensor): Die Eingabematrix.full_matrices(bool, optional): Wenn True, werden die vollständigen U- und V-Matrizen zurückgegeben. Standardwert ist False.out(tuple, optional): Ausgabetupel.
Rückgabewert:
tuple: Gibt ein Tupel (U, S, Vh) zurück, wobei Vh die Transponierte von V ist.
Anwendungsbeispiel
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],
[10.0, 11.0, 12.0]])
# SVD-Zerlegung
U, S, Vh = torch.linalg.svd(A)
print("Matrix A:")
print(A)
print("nU-Form:", U.shape)
print("Singulärwerte S:", S)
print("Vh-Form:", Vh.shape)
print("nVerifikation: U @ diag(S) @ Vh =")
print(U @ torch.diag(S) @ Vh)
# Matrix erstellen
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]])
# SVD-Zerlegung
U, S, Vh = torch.linalg.svd(A)
print("Matrix A:")
print(A)
print("nU-Form:", U.shape)
print("Singulärwerte S:", S)
print("Vh-Form:", Vh.shape)
print("nVerifikation: U @ diag(S) @ Vh =")
print(U @ torch.diag(S) @ Vh)
Das Ausgabeergebnis ist:
矩阵 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]])
Andere Erweiterungen