PyTorch torch.lu 関数
torch.luPyTorch で行列の LU 分解を計算する関数です。LU 分解は行列 A を A = P * L * U と分解し、L は下三角行列、U は上三角行列、P は置換行列です。
関数定義
torch.lu(A, pivot=True, get_infos=False, out=None)
引数:
A(Tensor): 入力行列。pivot(bool, オプション): LU 列ピボット分解を行うかどうか。デフォルトは True。get_infos(bool, オプション): True の場合、情報を返します。デフォルトは False。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, pivot=True)
print("行列 A:")
print(A)
print("nLU 行列:")
print(LU)
print("nピボットインデックス:")
print(pivots)
# 行列を作成
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, pivot=True)
print("行列 A:")
print(A)
print("nLU 行列:")
print(LU)
print("nピボットインデックス:")
print(pivots)
出力結果:
矩阵 A:
tensor([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]])
LU 矩阵:
tensor([[7., 8., 9.],
[0.2, 0.4, 0.6],
[0.6, 0.8, 0.0]])
主元索引:
tensor([3, 3, 3], dtype=torch.int32)
例 - get_infos を使用
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, info = torch.lu(A, pivot=True, get_infos=True)
print("情報:", info)
A = torch.tensor([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0]])
# LU 分解、情報を取得
LU, pivots, info = torch.lu(A, pivot=True, get_infos=True)
print("情報:", info)
その他の拡張