评价此页

torch.lu_unpack#

torch.lu_unpack(LU_data, LU_pivots, unpack_data=True, unpack_pivots=True, *, out=None)#

lu_factor() 返回的 LU 分解解包到 P, L, U 矩阵。

另请参阅

lu() 返回 LU 分解的矩阵。其梯度公式比 lu_factor() 后跟 lu_unpack() 更高效。

参数
  • LU_data (Tensor) – 压缩的 LU 分解数据

  • LU_pivots (Tensor) – 压缩的 LU 分解透视点

  • unpack_data (bool) – 指示是否应解压数据的标志。如果为 False,则返回的 LU 为空张量。默认为 True

  • unpack_pivots (bool) – 指示是否应将透视点解压到排列矩阵 P 中。如果为 False,则返回的 P 为空张量。默认为 True

关键字参数

out (tuple, optional) – 三个张量的输出元组。如果为 None 则忽略。

返回

一个命名元组 (P, L, U)

示例

>>> A = torch.randn(2, 3, 3)
>>> LU, pivots = torch.linalg.lu_factor(A)
>>> P, L, U = torch.lu_unpack(LU, pivots)
>>> # We can recover A from the factorization
>>> A_ = P @ L @ U
>>> torch.allclose(A, A_)
True

>>> # LU factorization of a rectangular matrix:
>>> A = torch.randn(2, 3, 2)
>>> LU, pivots = torch.linalg.lu_factor(A)
>>> P, L, U = torch.lu_unpack(LU, pivots)
>>> # P, L, U are the same as returned by linalg.lu
>>> P_, L_, U_ = torch.linalg.lu(A)
>>> torch.allclose(P, P_) and torch.allclose(L, L_) and torch.allclose(U, U_)
True