PyTorch torch.lobpcg function
PyTorch torch reference manual
torch.lobpcg is a function in PyTorch used to solve eigenvalue problems with 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 examples
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 solution
eigenvalues
,
eigenvectors
=
torch.
lobpcg
(
A
,
X
=
X
,
k
=
k
)
(
"Matrix shape:"
,
A.
shape
)
(
"Eigenvalues:"
)
(
eigenvalues
)
The output is:
矩阵形状: torch.Size([100, 100])
Eigenvalues:
tensor([105.0154, 101.8972, 98.6843])