PyTorch torch.solve 関数
torch.solvePyTorch において線形連立方程式を解くための関数です。AX = B を解きます。ここで A は係数行列、B は右辺の行列またはベクトルです。
関数定義
torch.solve(B, A, out=None)
パラメータ:
B(Tensor): 右辺の行列またはベクトル。A(Tensor): 係数行列。out(tuple, オプション): 出力タプル。
戻り値:
tuple: (X, LU) のタプルを返します。ここで X は解、LU は分解です。
使用例
例
import torch
# 係数行列と右辺ベクトルを作成
A = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
B = torch.tensor([5.0, 11.0])
# AX = B を解く
X, _ = torch.solve(B, A)
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]])
B = torch.tensor([5.0, 11.0])
# AX = B を解く
X, _ = torch.solve(B, A)
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.]])
右侧向量 B:
tensor([ 5., 11.])
解 X:
tensor([1., 2.])
验证: A @ X =
tensor([ 5., 11.])
その他の拡張