# megengine.functional.svd¶

svd(inp, full_matrices=False, compute_uv=True)[源代码]

Computes the singular value decomposition of a matrix (or a stack of matrices) inp.

Let $$X$$ be the input matrix (or a stack of input matrices), the output should satisfies:

$X = U * diag(S) * Vh$

where U is a matrix (or stack of vectors) with orthonormal columns, S is a vector of non-negative numbers (or stack of vectors), and Vh is a matrix (or a stack of matrices) with orthonormal rows.

• inp (Tensor) – A input real tensor having the shape (..., M, N) with inp.ndim >= 2 .

• full_matrices (bool, optional) – If False , U and Vh have the shapes (..., M, K) and (..., K, N) , respectively, where K = min(M, N) . If True , the shapes are (..., M, M) and (..., N, N) , respectively. Default: False .

• compute_uv (bool, optional) – 除了 S，是否要计算U 和 Vh 。 默认: True .

• 不支持 full_matrices和 compute_uvTrue .

Tensor

Returns a tuple ( U , S , Vh ), which are SVD factors U , S, Vh of input matrix inp. ( U , Vh only returned when compute_uv is True). U contains matrices orthonormal columns (i.e., the columns are left singular vectors). If full_matrices is True , the array must have shape (..., M, M) . If full_matrices is False , the array must have shape (..., M, K) , where K = min(M, N) .

>>> import numpy as np
>>> x = Tensor(np.random.randn(9, 6))
>>> y = Tensor(np.random.randn(2, 7, 8, 3))
>>> U, S, Vh = F.svd(x, full_matrices=False)
>>> print(U._tuple_shape, S._tuple_shape, Vh._tuple_shape)
(9, 6) (6,) (6, 6)
>>> u, s, vh = F.svd(y, full_matrices=False)
>>> print(u._tuple_shape, s._tuple_shape, vh._tuple_shape)
(2, 7, 8, 3) (2, 7, 3) (2, 7, 3, 3)