megengine.functional.svd¶
- svd(inp, full_matrices=False, compute_uv=True)[源代码]¶
计算矩阵(或一组矩阵)
inp
的奇异值分解。令 \(X\) 为输入矩阵(或者一组矩阵),奇异值分解的输出满足:
\[X = U * diag(S) * Vh\]其中,
U
是一个列向量正交的矩阵(或一组矩阵),S
是一个元素均为非负值的向量的向量(或一组向量),而Vh
是一个行向量正交的矩阵(或一组矩阵)。- 参数
inp (Tensor) – 输入可以是 shape 形如
(..., M, N)
的矩阵,需满足inp.ndim >= 2
.full_matrices (bool, optional) – 如果取
False
,则U
和Vh
的 shape 分别为(..., M, K)
、(..., K, N)
,其中K = min(M, N)
. 如果取True
,则 shape 分别为(..., M, M)
、(..., N, N)
. 默认值:False
.compute_uv (bool, optional) – 除了
S
,是否要计算``U`` 和Vh
。 默认:True
.
注解
不支持
full_matrices``和 ``compute_uv
为True
.
- 返回类型
- 返回
返回值是一个tuple (
U
,S
,Vh
),U
,S
,Vh
为输入矩阵``inp`` 的奇异值分解的结果。( 当compute_uv
为 True 的时,只会返回U
和Vh
)。U
包含输入矩阵的正交列向量(列向量为输入矩阵的左奇异向量)。如果full_matrices
为True
,则矩阵的 shape 必须为(..., M, M)
。如果full_matrices
为False
,则矩阵的 shape 必须为(..., M, K)
,其中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)