Fonction torch.lu_unpack de PyTorch


Pytorch torch 参考手册Manuel de référence PyTorch torch

torch.lu_unpackIl s'agit d'une fonction dans PyTorch utilisée pour extraire les matrices L, U et P du résultat de la décomposition LU.

Définition de fonction

torch.lu_unpack(LU_data, LU_pivots, unpack_data=True, out=None)

Parameters:

  • LU_data(Tensor) : La matrice obtenue à partir de la décomposition LU.
  • LU_pivots(Tensor) : Les indices pivots de la décomposition LU.
  • unpack_data(bool, optionnel) : Indique s'il faut extraire les données. La valeur par défaut est True.
  • out(tuple, facultatif) : Tuple de sortie.

Valeur de retour:

  • tuple: Renvoie le tuple (pivot, L, U).

Exemple d'utilisation

Exemple

import torch

# Créer une matrice
A = torch.tensor([[1.0, 2.0, 3.0],
                  [4.0, 5.0, 6.0],
                  [7.0, 8.0, 9.0]])

# Décomposition LU
LU, pivots = torch.lu(A)

# Extraire L, U, P
P, L, U = torch.lu_unpack(LU, pivots)

print("Matrice A :")
print(A)
print("nMatrice de permutation P :")
print(P)
print("nMatrice triangulaire inférieure L :")
print(L)
print("nMatrice triangulaire supérieure U :")
print(U)
print("nVérification : P @ L @ U =")
print(P @ L @ U)

Le résultat de sortie est:

矩阵 A:
tensor([[1., 2., 3.],
        [4., 5., 6.],
        [7., 8., 9.]])
置换矩阵 P:
tensor([[0., 0., 1.],
        [1., 0., 0.],
        [0., 1., 0.]])
下三角矩阵 L:
tensor([[1.0000, 0.0000, 0.0000],
        [0.1429, 1.0000, 0.0000],
        [0.5714, 0.5000, 1.0000]])
上三角矩阵 U:
tensor([[7.0000, 8.0000, 9.0000],
        [0.0000, 0.8571, 1.7143],
        [0.0000, 0.0000, 0.0000]])
验证: P @ L @ U =
tensor([[1., 2., 3.],
        [4., 5., 6.],
        [7., 8., 9.]])

Pytorch torch 参考手册Manuel de référence PyTorch torch

Autres extensions