PyTorch torch.svd_lowrank 関数
torch.svd_lowrankPyTorch において行列の低ランク近似 SVD を計算するための関数です。ランダム手法を使用して部分 SVD を計算するため、完全な SVD よりも高速です。
関数定義
torch.svd_lowrank(A, q=6, niter=2, mexp=2)
引数:
A(Tensor): 入力行列。q(int, オプション): べき乗反復回数。デフォルトは 6。niter(int, オプション): ランダム反復回数。デフォルトは 2。mexp(int, オプション): 行列指数。デフォルトは 2。
戻り値:
tuple: (U, S, V) のタプルを返します。
使用例
例
import torch
# 大行列を作成
A = torch.randn(100, 50)
# 低ランク SVD (上位 10 個の特異値を計算)
U, S, V = torch.svd_lowrank(A, q=10)
print("行列 A の形状:", A.shape)
print("U の形状:", U.shape)
print("特異値 S の形状:", S.shape)
print("V の形状:", V.shape)
print("n特異値:")
print(S[:10])
# 大行列を作成
A = torch.randn(100, 50)
# 低ランク SVD (上位 10 個の特異値を計算)
U, S, V = torch.svd_lowrank(A, q=10)
print("行列 A の形状:", A.shape)
print("U の形状:", U.shape)
print("特異値 S の形状:", S.shape)
print("V の形状:", V.shape)
print("n特異値:")
print(S[:10])
出力結果は次のとおりです:
矩阵 A 形状: torch.Size([100, 50])
U 形状: torch.Size([100, 10])
奇异值 S 形状: torch.Size([10])
V 形状: torch.Size([50, 10])
奇异值:
tensor([14.6526, 14.0891, 13.5830, 13.0966, 12.7677, 12.4337, 12.1510,
11.8361, 11.5309, 11.3064])
その他の拡張