PyTorch torch.svd_lowrank function
PyTorch torch reference manual
torch.svd_lowrank is a function in PyTorch used to compute a low-rank approximation of the SVD of a matrix. It uses randomized 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): The number of power iterations. Defaults to 6.niter(int, optional): The number of random iterations. Defaults to 2.mexp(int, optional): The matrix exponent. Defaults to 2.
Return Value:
tuple: Returns a tuple of (U, S, V).
Usage examples
Example
import
torch
Create a large matrix
A
=
torch.
randn
(
100
,
50
)
Low-rank SVD (compute the first 10 singular values)
U
,
S
,
V
=
torch.
svd_lowrank
(
A
,
q
=
10
)
(
"Shape of matrix A:"
,
A.
shape
)
(
"U shape:"
,
U.
shape
)
(
"Shape of singular values S:"
,
S.
shape
)
(
"V shape:"
,
V.
shape
)
(
"nSingular values:"
)
(
S
[
:
10
]
)
The output is:
矩阵 A 形状: torch.Size([100, 50])
U 形状: torch.Size([100, 10])
奇异值 S 形状: torch.Size([10])
V 形状: torch.Size([50, 10])
Singular values:
tensor([14.6526, 14.0891, 13.5830, 13.0966, 12.7677, 12.4337, 12.1510,
11.8361, 11.5309, 11.3064])