megengine.functional.svd¶
- svd(inp, full_matrices=False, compute_uv=True)[源代码]¶
Returns a singular value decomposition
A = USVh
of a matrix (or a stack of matrices)x
, whereU
is a matrix (or a stack of matrices) with orthonormal columns,S
is a vector of non-negative numbers (or stack of vectors), andVh
is a matrix (or a stack of matrices) with orthonormal rows.- 参数
x (Tensor) – A input real tensor having the shape
(..., M, N)
withx.ndim >= 2
.full_matrices (bool, optional) – If
False
,U
andVh
have the shapes(..., M, K)
and(..., K, N)
, respectively, whereK = min(M, N)
. IfTrue
, the shapes are(..., M, M)
and(..., N, N)
, respectively. Default:False
.compute_uv (bool, optional) – Whether or not to compute
U
andVh
in addition toS
. Default:True
.
注解
naive does not support
full_matrices
andcompute_uv
asTrue
.
- 返回类型
- 返回
- Returns a tuple (
U
,S
,Vh
), which are SVD factorsU
,S
,Vh
of input matrixx
. (U
,Vh
only returned whencompute_uv
is True). U
contains matrices orthonormal columns (i.e., the columns are left singular vectors). Iffull_matrices
isTrue
, the array must have shape(..., M, M)
. Iffull_matrices
isFalse
, the array must have shape(..., M, K)
, whereK = min(M, N)
.
- Returns a tuple (
实际案例
>>> import numpy as np >>> x = Tensor(np.random.randn(9, 6)) >>> y = Tensor(np.random.randn(2, 7, 8, 3))
Reconstruction based on reduced SVD, 2D case: >>> U, S, Vh = F.svd(x, full_matrices=False) >>> print(U._tuple_shape, S._tuple_shape, Vh._tuple_shape) (9, 6) (6,) (6, 6)
Reconsturction based on reduced SVD, 4D case: >>> 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)