PyTorch torch.lu_unpack 関数
torch.lu_unpackPyTorch において、LU 分解の結果から L、U、P 行列を展開するために使用される関数です。
関数定義
torch.lu_unpack(LU_data, LU_pivots, unpack_data=True, out=None)
パラメータ:
LU_data(Tensor): LU 分解で得られた行列。LU_pivots(Tensor): LU 分解のピボットインデックス。unpack_data(bool, オプション): データを展開するかどうか。デフォルトは True。out(tuple, オプション): 出力タプル。
戻り値:
tuple(pivot, L, U) のタプルを返します。
使用例
例
import torch
# 行列を作成
A = torch.tensor([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0]])
# LU 分解
LU, pivots = torch.lu(A)
# L、U、P を展開
P, L, U = torch.lu_unpack(LU, pivots)
print("行列 A:")
print(A)
print("n置換行列 P:")
print(P)
print("n下三角行列 L:")
print(L)
print("n上三角行列 U:")
print(U)
print("n検証: P @ L @ U =")
print(P @ L @ U)
# 行列を作成
A = torch.tensor([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0]])
# LU 分解
LU, pivots = torch.lu(A)
# L、U、P を展開
P, L, U = torch.lu_unpack(LU, pivots)
print("行列 A:")
print(A)
print("n置換行列 P:")
print(P)
print("n下三角行列 L:")
print(L)
print("n上三角行列 U:")
print(U)
print("n検証: P @ L @ U =")
print(P @ L @ U)
出力結果は次のとおりです:
矩阵 A:
tensor([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]])
置换矩阵 P:
tensor([[0., 0., 1.],
[1., 0., 0.],
[0., 1., 0.]])
下三角矩阵 L:
tensor([[1.0000, 0.0000, 0.0000],
[0.1429, 1.0000, 0.0000],
[0.5714, 0.5000, 1.0000]])
上三角矩阵 U:
tensor([[7.0000, 8.0000, 9.0000],
[0.0000, 0.8571, 1.7143],
[0.0000, 0.0000, 0.0000]])
验证: P @ L @ U =
tensor([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]])
その他の拡張
Pytorch torch リファレンスマニュアル