Learn R Programming

torch (version 0.8.1)

torch_svd: Svd

Description

Svd

Usage

torch_svd(self, some = TRUE, compute_uv = TRUE)

Arguments

self

(Tensor) the input tensor of size \((*, m, n)\) where * is zero or more batch dimensions consisting of \(m \times n\) matrices.

some

(bool, optional) controls the shape of returned U and V

compute_uv

(bool, optional) option whether to compute U and V or not

svd(input, some=TRUE, compute_uv=TRUE) -> (Tensor, Tensor, Tensor)

This function returns a namedtuple (U, S, V) which is the singular value decomposition of a input real matrix or batches of real matrices input such that \(input = U \times diag(S) \times V^T\).

If some is TRUE (default), the method returns the reduced singular value decomposition i.e., if the last two dimensions of input are m and n, then the returned U and V matrices will contain only \(min(n, m)\) orthonormal columns.

If compute_uv is FALSE, the returned U and V matrices will be zero matrices of shape \((m \times m)\) and \((n \times n)\) respectively. some will be ignored here.

Examples

Run this code
if (torch_is_installed()) {

a = torch_randn(c(5, 3))
a
out = torch_svd(a)
u = out[[1]]
s = out[[2]]
v = out[[3]]
torch_dist(a, torch_mm(torch_mm(u, torch_diag(s)), v$t()))
a_big = torch_randn(c(7, 5, 3))
out = torch_svd(a_big)
u = out[[1]]
s = out[[2]]
v = out[[3]]
torch_dist(a_big, torch_matmul(torch_matmul(u, torch_diag_embed(s)), v$transpose(-2, -1)))
}

Run the code above in your browser using DataLab