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,则 UVh 的 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_uvTrue .

返回类型:

Tensor

返回:

返回值是一个tuple ( U , S , Vh ), USVh 为输入矩阵``inp`` 的奇异值分解的结果。( 当 compute_uv 为 True 的时,只会返回 UVh )。 U 包含输入矩阵的正交列向量(列向量为输入矩阵的左奇异向量)。如果 full_matricesTrue ,则矩阵的 shape 必须为 (..., M, M) 。如果 full_matricesFalse ,则矩阵的 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)