Rate this Page

torch.svd#

torch.svd(input,some=True,compute_uv=True,*,out=None)#

Computes the singular value decomposition of either a matrix or batch ofmatricesinput. The singular value decomposition is represented as anamedtuple(U, S, V), such thatinput=Udiag(S)VH= U \text{diag}(S) V^{\text{H}}.whereVHV^{\text{H}} is the transpose ofV for real inputs,and the conjugate transpose ofV for complex inputs.Ifinput is a batch of matrices, thenU,S, andV are alsobatched with the same batch dimensions asinput.

Ifsome isTrue (default), the method returns the reduced singularvalue decomposition. In this case, if the last two dimensions ofinput arem andn, then the returnedU andV matrices will contain onlymin(n, m) orthonormal columns.

Ifcompute_uv isFalse, the returnedU andV will bezero-filled matrices of shape(m, m) and(n, n)respectively, and the same device asinput. The argumentsomehas no effect whencompute_uv isFalse.

Supportsinput of float, double, cfloat and cdouble data types.The dtypes ofU andV are the same asinput’s.S willalways be real-valued, even ifinput is complex.

Warning

torch.svd() is deprecated in favor oftorch.linalg.svd()and will be removed in a future PyTorch release.

U,S,V=torch.svd(A,some=some,compute_uv=True) (default) should be replaced with

U,S,Vh=torch.linalg.svd(A,full_matrices=notsome)V=Vh.mH

_,S,_=torch.svd(A,some=some,compute_uv=False) should be replaced with

S=torch.linalg.svdvals(A)

Note

Differences withtorch.linalg.svd():

Note

The singular values are returned in descending order. Ifinput is a batch of matrices,then the singular values of each matrix in the batch are returned in descending order.

Note

TheS tensor can only be used to compute gradients ifcompute_uv isTrue.

Note

Whensome isFalse, the gradients onU[…, :, min(m, n):]andV[…, :, min(m, n):] will be ignored in the backward pass, as those vectorscan be arbitrary bases of the corresponding subspaces.

Note

The implementation oftorch.linalg.svd() on CPU uses LAPACK’s routine?gesdd(a divide-and-conquer algorithm) instead of?gesvd for speed. Analogously,on GPU, it uses cuSOLVER’s routinesgesvdj andgesvdjBatched on CUDA 10.1.243and later, and MAGMA’s routinegesdd on earlier versions of CUDA.

Note

The returnedU will not be contiguous. The matrix (or batch of matrices) willbe represented as a column-major matrix (i.e. Fortran-contiguous).

Warning

The gradients with respect toU andV will only be finite when the input does nothave zero nor repeated singular values.

Warning

If the distance between any two singular values is close to zero, the gradients with respect toU andV will be numerically unstable, as they depends on1minijσi2σj2\frac{1}{\min_{i \neq j} \sigma_i^2 - \sigma_j^2}. The same happens when the matrixhas small singular values, as these gradients also depend onS^{-1}.

Warning

For complex-valuedinput the singular value decomposition is not unique,asU andV may be multiplied by an arbitrary phase factoreiϕe^{i \phi} on every column.The same happens wheninput has repeated singular values, where one may multiplythe columns of the spanning subspace inU andV by a rotation matrixandthe resulting vectors will span the same subspace.Different platforms, like NumPy, or inputs on different device types,may produce differentU andV tensors.

Parameters
  • input (Tensor) – the input tensor of size(*, m, n) where* is zero or morebatch dimensions consisting of(m, n) matrices.

  • some (bool,optional) – controls whether to compute the reduced or full decomposition, andconsequently, the shape of returnedU andV. Default:True.

  • compute_uv (bool,optional) – controls whether to computeU andV. Default:True.

Keyword Arguments

out (tuple,optional) – the output tuple of tensors

Example:

>>>a=torch.randn(5,3)>>>atensor([[ 0.2364, -0.7752,  0.6372],        [ 1.7201,  0.7394, -0.0504],        [-0.3371, -1.0584,  0.5296],        [ 0.3550, -0.4022,  1.5569],        [ 0.2445, -0.0158,  1.1414]])>>>u,s,v=torch.svd(a)>>>utensor([[ 0.4027,  0.0287,  0.5434],        [-0.1946,  0.8833,  0.3679],        [ 0.4296, -0.2890,  0.5261],        [ 0.6604,  0.2717, -0.2618],        [ 0.4234,  0.2481, -0.4733]])>>>stensor([2.3289, 2.0315, 0.7806])>>>vtensor([[-0.0199,  0.8766,  0.4809],        [-0.5080,  0.4054, -0.7600],        [ 0.8611,  0.2594, -0.4373]])>>>torch.dist(a,torch.mm(torch.mm(u,torch.diag(s)),v.t()))tensor(8.6531e-07)>>>a_big=torch.randn(7,5,3)>>>u,s,v=torch.svd(a_big)>>>torch.dist(a_big,torch.matmul(torch.matmul(u,torch.diag_embed(s)),v.mT))tensor(2.6503e-06)