PyTorch torch.svd_lowrank Function
PyTorch torch Reference Manual
torch.svd_lowrankIt is a function in PyTorch for computing a low-rank approximation SVD of a matrix. It uses random methods to compute a partial SVD, which is faster than a full SVD.
Function Definition
torch.svd_lowrank(A, q=6, niter=2, mexp=2)
Parameters:
A(Tensor): Input matrix.q(int, optional): Number of power iterations. Default is 6.niter(int, optional): Number of random iterations. Default is 2.mexp(int, optional): Matrix exponent. Default is 2.
Return Value:
tuple: Returns a tuple (U, S, V).
Usage Example
Example
import torch
# Create a large matrix
A = torch.randn(100, 50)
# Low-rank SVD (compute the top 10 singular values)
U, S, V = torch.svd_lowrank(A, q=10)
print("Matrix A shape:", A.shape)
print("U shape:", U.shape)
print("Singular values S shape:", S.shape)
print("V shape:", V.shape)
print("nSingular values:")
print(S[:10])
# Create a large matrix
A = torch.randn(100, 50)
# Low-rank SVD (compute the top 10 singular values)
U, S, V = torch.svd_lowrank(A, q=10)
print("Matrix A shape:", A.shape)
print("U shape:", U.shape)
print("Singular values S shape:", S.shape)
print("V shape:", V.shape)
print("nSingular values:")
print(S[:10])
The output result is:
矩阵 A 形状: torch.Size([100, 50])
U 形状: torch.Size([100, 10])
奇异值 S 形状: torch.Size([10])
V 形状: torch.Size([50, 10])
奇异值:
tensor([14.6526, 14.0891, 13.5830, 13.0966, 12.7677, 12.4337, 12.1510,
11.8361, 11.5309, 11.3064])
Other Extensions