megengine.functional.svd

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

返回一个矩阵 x``(或一个矩阵堆) 的奇异值分解 ``A = USVh , 其中 U 是一个具有正交列的矩阵 (或一个矩阵堆) , S 是一个非负数的向量 (或一个向量堆), Vh 是一个具有正交行的矩阵 (或一个矩阵堆)。

参数
  • x (Tensor) – 输入tensor的shape为 (..., M, N), 并且 x.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

返回

返回一个元组 ( U , S , Vh ), U , S, Vh 是输入 x``的SVD因子 ( ``U , Vh 只有当``compute_uv`` 是 True时返回).。 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))

基于 reduced SVD, 2D 情形的重构: >>> U, S, Vh = F.svd(x, full_matrices=False) >>> print(U._tuple_shape, S._tuple_shape, Vh._tuple_shape) (9, 6) (6,) (6, 6)

基于 reduced SVD, 4D 情形的重构: >>> 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)