PyTorch torch.lobpcg Function
Pytorch torch Reference Manual
torch.lobpcgIt is a function in PyTorch used to solve eigenvalue problems using the LOBPCG (Locally Optimal Block Preconditioned Conjugate Gradient) method. It is especially suitable for eigenvalue computation of large-scale sparse matrices.
Function Definition
torch.lobpcg(A, B=None, X=None, k=None, n=None, tol=None, maxiter=None, largest=None, method=None, tracker=None, ortho_iparams=None, ortho_fparams=None, ortho_cparams=None)
Parameters:
A(Tensor): Input matrix (must be positive definite).B(Tensor, optional): Mass matrix, used for generalized eigenvalue problems.X(Tensor, optional): Initial approximate eigenvectors.k(int, optional): Number of eigenvalues to solve for.n(int, optional): Block size.
Return Value:
tuple: Returns a tuple of (eigenvalues, eigenvectors).
Usage Example
Example
import torch
# Create a symmetric positive definite matrix
n = 100
A = torch.randn(n, n)
A = A @ A.T # Make it symmetric positive definite
# Initial approximation
k = 3
X = torch.randn(n, k)
# LOBPCG solve
eigenvalues, eigenvectors = torch.lobpcg(A, X=X, k=k)
print("Matrix shape:", A.shape)
print("Eigenvalues:")
print(eigenvalues)
# Create a symmetric positive definite matrix
n = 100
A = torch.randn(n, n)
A = A @ A.T # Make it symmetric positive definite
# Initial approximation
k = 3
X = torch.randn(n, k)
# LOBPCG solve
eigenvalues, eigenvectors = torch.lobpcg(A, X=X, k=k)
print("Matrix shape:", A.shape)
print("Eigenvalues:")
print(eigenvalues)
The output result is:
矩阵形状: torch.Size([100, 100]) 特征值: tensor([105.0154, 101.8972, 98.6843])
Other Extensions