Skip to contents

Letting be or , the full SVD of a matrix , if k = min(m,n), is defined as

Usage

linalg_svd(A, full_matrices = TRUE)

Arguments

A

(Tensor): tensor of shape (*, m, n) where * is zero or more batch dimensions.

full_matrices

(bool, optional): controls whether to compute the full or reduced SVD, and consequently, the shape of the returned tensors U and V. Default: TRUE.

Value

A list (U, S, V) which corresponds to , , above. S will always be real-valued, even when A is complex. It will also be ordered in descending order. U and V will have the same dtype as A. The left / right singular vectors will be given by the columns of U and the rows of V respectively.

Details

A=Udiag(S)VHUKm×m,SRk,VKn×n A = U \operatorname{diag}(S) V^{H} \mathrlap{\qquad U \in \mathbb{K}^{m \times m}, S \in \mathbb{R}^k, V \in \mathbb{K}^{n \times n}}

where , is the conjugate transpose when is complex, and the transpose when is real-valued.

The matrices , (and thus ) are orthogonal in the real case, and unitary in the complex case. When m > n (resp. m < n) we can drop the last m - n (resp. n - m) columns of U (resp. V) to form the reduced SVD:

A=Udiag(S)VHUKm×k,SRk,VKk×n A = U \operatorname{diag}(S) V^{H} \mathrlap{\qquad U \in \mathbb{K}^{m \times k}, S \in \mathbb{R}^k, V \in \mathbb{K}^{k \times n}}

where .

In this case, and also have orthonormal columns. Supports input of float, double, cfloat and cdouble dtypes.

Also supports batches of matrices, and if A is a batch of matrices then the output has the same batch dimensions.

The returned decomposition is a named tuple (U, S, V) which corresponds to , , above.

The singular values are returned in descending order. The parameter full_matrices chooses between the full (default) and reduced SVD.

Note

When full_matrices=TRUE, the gradients with respect to U[..., :, min(m, n):] and Vh[..., min(m, n):, :] will be ignored, as those vectors can be arbitrary bases of the corresponding subspaces.

Warnings

The returned tensors U and V are not unique, nor are they continuous with respect to A. Due to this lack of uniqueness, different hardware and software may compute different singular vectors. This non-uniqueness is caused by the fact that multiplying any pair of singular vectors by -1 in the real case or by in the complex case produces another two valid singular vectors of the matrix. This non-uniqueness problem is even worse when the matrix has repeated singular values. In this case, one may multiply the associated singular vectors of U and V spanning the subspace by a rotation matrix and the resulting vectors will span the same subspace.

Gradients computed using U or V will only be finite when A does not have zero as a singular value or repeated singular values. Furthermore, if the distance between any two singular values is close to zero, the gradient will be numerically unstable, as it depends on the singular values through the computation of . The gradient will also be numerically unstable when A has small singular values, as it also depends on the computaiton of .

See also

  • linalg_svdvals() computes only the singular values. Unlike linalg_svd(), the gradients of linalg_svdvals() are always numerically stable.

  • linalg_eig() for a function that computes another type of spectral decomposition of a matrix. The eigendecomposition works just on on square matrices.

  • linalg_eigh() for a (faster) function that computes the eigenvalue decomposition for Hermitian and symmetric matrices.

  • linalg_qr() for another (much faster) decomposition that works on general matrices.

Other linalg: linalg_cholesky_ex(), linalg_cholesky(), linalg_det(), linalg_eigh(), linalg_eigvalsh(), linalg_eigvals(), linalg_eig(), linalg_householder_product(), linalg_inv_ex(), linalg_inv(), linalg_lstsq(), linalg_matrix_norm(), linalg_matrix_power(), linalg_matrix_rank(), linalg_multi_dot(), linalg_norm(), linalg_pinv(), linalg_qr(), linalg_slogdet(), linalg_solve(), linalg_svdvals(), linalg_tensorinv(), linalg_tensorsolve(), linalg_vector_norm()

Examples

if (torch_is_installed()) {

a <- torch_randn(5, 3)
linalg_svd(a, full_matrices = FALSE)
}
#> [[1]]
#> torch_tensor
#> -0.0804 -0.5354 -0.4235
#> -0.4432 -0.4393 -0.3962
#> -0.2495  0.0753  0.4123
#>  0.0594 -0.6866  0.6812
#>  0.8552 -0.2083 -0.1722
#> [ CPUFloatType{5,3} ]
#> 
#> [[2]]
#> torch_tensor
#>  3.4521
#>  2.0418
#>  1.5650
#> [ CPUFloatType{3} ]
#> 
#> [[3]]
#> torch_tensor
#>  0.7957 -0.4490  0.4066
#> -0.2418  0.3800  0.8928
#>  0.5553  0.8087 -0.1938
#> [ CPUFloatType{3,3} ]
#>