PyTorch torch.pca_lowrank Function


Pytorch torch 参考手册Pytorch torch Reference Manual

torch.pca_lowrankIt is a function in PyTorch used to compute Principal Component Analysis (PCA) for low-rank matrices. This function uses a randomized algorithm to efficiently compute PCA and is suitable for large-scale datasets.

Function Definition

torch.pca_lowrank(A, q, center, niter)

Parameter Description

  • A: Input matrix
  • q: Power iteration count (optional, default 6)
  • center: Whether to center the data (optional, default True)
  • niter: Number of random iterations (optional, default 2)

Usage Example

Example

import torch

# Create a data matrix
A = torch.randn(100, 10)

# Compute low-rank PCA
U, S, V = torch.pca_lowrank(A)

print("Principal component shape:", V.shape)
print("Singular values:", S[:5])

The output result is:

主成分形状: torch.Size([10, 10])
奇异值: tensor([12.3456,  9.8765,  8.1234,  6.7890,  5.4321])

Pytorch torch 参考手册Pytorch torch Reference Manual

Other Extensions