Singular Value Decomposition(SVD)是一种矩阵分解方式,可以将一个实数矩阵分解为三个实数矩阵的乘积:$M=U\Sigma V^T$,其中$U$和$V$是正交矩阵,$\Sigma$是一个对角线元素非负且按照递减顺序排序的对角矩阵。Open3D中的open3d.core.Tensor
类提供了求解SVD的方法。
def svd(self, full_matrices=True, compute_uv=True) -> Tuple[Tensor, Union[Tensor, None], Union[Tensor, None]]:
"""
Computes the singular value decomposition (SVD).
Parameters
----------
full_matrices : bool
If True, `U` and `V` are of shape `(M, M)` and `(N, N)`, respectively. If False, `U` and `V` are of shape `(M, K)` and `(K, N)`, where `K = min(M, N)`.
compute_uv : bool
Whether to compute both `U` and `V`. If False, only the singular values `S` will be returned.
Returns
-------
U : Tensor
Unitary matrix. If `full_matrices=False`, `U` is of shape `(M, K)`, and the first `K` columns of `U` contains the left singular vectors.
S : Tensor or None
Singular values. If `compute_uv=False`, `S` is returned as a 1-D tensor of shape `(K,)`. Otherwise, it is returned as a diagonal matrix of shape `(K, K)`.
V : Tensor or None
Unitary matrix. If `full_matrices=False`, `V` is of shape `(K, N)`, and the first `K` rows of `V` contains the right singular vectors.
"""
full_matrices
:当full_matrices=True
时,U
和V
的形状分别为(M, M)
和(N, N)
;当full_matrices=False
时,U
和V
的形状分别为(M, K)
和(K, N)
,其中$K=min(M,N)$。默认值为True
。compute_uv
:是否计算U
和V
。如果compute_uv=False
,仅返回奇异值s
,形状为(K,)
;否则,同时返回U
和V
。默认值为True
。U
:左奇异向量,形状为(M, K)
。S
:奇异值,形状为(K,)
或(K, K)
。V
:右奇异向量,形状为(K, N)
。import open3d.core as o3c
# create a 3x5 matrix
x = o3c.Tensor([[1.0, 2.0, 3.0, 4.0, 5.0],
[6.0, 7.0, 8.0, 9.0, 10.0],
[11.0, 12.0, 13.0, 14.0, 15.0]])
# compute SVD by setting `full_matrices=False` and `compute_uv=True`
u, s, v = x.svd(full_matrices=False, compute_uv=True)
# check outputs
print(u)
print(s)
print(v)
输出:
70.5721 -0.4082 -0.0625 -0.5655 -0.4268
89.6025 -0.0793 0.1815 0.5017 0.0150
108.6329 0.2497 0.4254 -0.0554 0.5268
_tensor([34.9689, 1.4680, 0.0000], dtype=torch.float64)
90.0167 -0.4415 -0.1120 -0.1006 -0.8768
0.0000 0.7913 0.1063 0.5780 -0.1832
这个例子展示了如何使用open3d.core.Tensor
计算一个实数矩阵的奇异值分解。我们创建了一个3x5
的矩阵,然后使用svd
方法计算其SVD。在这个例子中,我们设置了full_matrices=False
和compute_uv=True
,代表我们希望输出奇异值、左奇异向量的前$K$列以及右奇异向量的前$K$行(其中$K$为$\min(M,N)$),并将它们存储在变量u
、s
和v
中。我们最后输出了这些变量的值,以检查计算结果是否正确。