PyTorch torch.cholesky_solve 関数
torch.cholesky_solvePyTorch で Cholesky 分解を使用して連立一次方程式を解く関数です。Cholesky 分解を利用して AX = B を効率的に解きます。
関数定義
torch.cholesky_solve(B, L, upper=False, out=None)
パラメータ:
B(Tensor): 連立一次方程式の右辺の行列またはベクトル。L(Tensor): Cholesky 分解で得られた上三角行列または下三角行列。upper(bool, オプション): True の場合、L は上三角行列です。そうでない場合は下三角行列です。デフォルトは False です。out(Tensor, オプション): 出力テンソル。
戻り値:
torch.Tensor: 連立一次方程式の解 X を返します。
使用例
例
import torch
# 対称正定値行列 A を作成します
A = torch.tensor([[4.0, 2.0, 2.0],
[2.0, 5.0, 3.0],
[2.0, 3.0, 6.0]], dtype=torch.float64)
# 右辺ベクトル B を作成します
B = torch.tensor([3, 4, 5], dtype=torch.float64)
# Cholesky 分解
L = torch.cholesky(A)
# Cholesky を使用して AX = B を解きます
X = torch.cholesky_solve(B, L)
print("行列 A:")
print(A)
print("n右辺ベクトル B:")
print(B)
print("n解 X:")
print(X)
print("n検証: A @ X =")
print(A @ X)
# 対称正定値行列 A を作成します
A = torch.tensor([[4.0, 2.0, 2.0],
[2.0, 5.0, 3.0],
[2.0, 3.0, 6.0]], dtype=torch.float64)
# 右辺ベクトル B を作成します
B = torch.tensor([3, 4, 5], dtype=torch.float64)
# Cholesky 分解
L = torch.cholesky(A)
# Cholesky を使用して AX = B を解きます
X = torch.cholesky_solve(B, L)
print("行列 A:")
print(A)
print("n右辺ベクトル B:")
print(B)
print("n解 X:")
print(X)
print("n検証: A @ X =")
print(A @ X)
出力結果は次のとおりです:
矩阵 A:
tensor([[4., 2., 2.],
[2., 5., 3.],
[2., 3., 6.]], dtype=torch.float64)
右侧向量 B:
tensor([3., 4., 5.], dtype=torch.float64)
解 X:
tensor([-2.5000, 5.0000, -2.0000], dtype=torch.float64)
验证: A @ X =
tensor([3., 4., 5.], dtype=torch.float64)
その他の拡張
Pytorch torch リファレンスマニュアル