PyTorch torch.lu_solve 関数
torch.lu_solveこれは、PyTorch において LU 分解を用いて線形方程式系を解く関数です。計算済みの LU 分解を利用して AX = B を効率的に解きます。
関数定義
torch.lu_solve(B, LU, pivots, out=None)
パラメータ:
B(Tensor): 右側の行列またはベクトル。LU(Tensor): LU 分解によって得られた行列。pivots(Tensor): LU 分解のピボットインデックス。out(Tensor, オプション): 出力テンソル。
戻り値:
torch.Tensor: 線形方程式系の解を返します。
使用例
実例
import torch
# 行列と右辺ベクトルを作成
A = torch.tensor([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0]], dtype=torch.float64)
B = torch.tensor([14.0, 32.0, 50.0], dtype=torch.float64)
# LU 分解
LU, pivots = torch.lu(A)
# LU 分解を使用して解く
X = torch.lu_solve(B, LU, pivots)
print("行列 A:")
print(A)
print("n右辺ベクトル B:")
print(B)
print("n解 X:")
print(X)
print("n検証: A @ X =")
print(A @ X)
# 行列と右辺ベクトルを作成
A = torch.tensor([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0]], dtype=torch.float64)
B = torch.tensor([14.0, 32.0, 50.0], dtype=torch.float64)
# LU 分解
LU, pivots = torch.lu(A)
# LU 分解を使用して解く
X = torch.lu_solve(B, LU, pivots)
print("行列 A:")
print(A)
print("n右辺ベクトル B:")
print(B)
print("n解 X:")
print(X)
print("n検証: A @ X =")
print(A @ X)
出力結果は以下のとおりです:
矩阵 A:
tensor([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]], dtype=torch.float64)
右侧向量 B:
tensor([14., 32., 50.], dtype=torch.float64)
解 X:
tensor([1., 2., 3.], dtype=torch.float64)
验证: A @ X =
tensor([14., 32., 50.], dtype=torch.float64)
その他の拡張
Pytorch torch リファレンスマニュアル