PyTorch torch.pca_lowrank function
PyTorch torch reference manual
torch.pca_lowrank is a function in PyTorch used to compute the principal component analysis (PCA) of a low-rank matrix. It uses a randomized algorithm to compute PCA efficiently and is suitable for large-scale datasets.
Function definition
torch.pca_lowrank(A, q, center, niter)
Parameter Description
A: Input matrixq: Number of power iterations (optional, default 6)center: Whether to center the data (optional, default True)niter: Number of random iterations (optional, default 2)
Usage examples
Example
import
torch
Create a data matrix
A
=
torch.
randn
(
100
,
10
)
Compute low-rank PCA
U
,
S
,
V
=
torch.
pca_lowrank
(
A
)
(
"Principal component shape:"
,
V.
shape
)
(
"Singular values:"
,
S
[
:
5
]
)
The output is:
主成分形状: torch.Size([10, 10])
奇异值: tensor([12.3456, 9.8765, 8.1234, 6.7890, 5.4321])