PyTorch中的雅可比矩陣


在本文中,我們將學習雅可比矩陣以及如何使用PyTorch中的不同方法計算該矩陣。我們在各種機器學習應用中使用雅可比矩陣。

雅可比矩陣

我們使用雅可比矩陣來計算輸入和輸出變數之間的關係。雅可比矩陣包含向量值函式的所有偏導數。我們可以在各種機器學習應用中使用此矩陣。以下是它的一些用途:

  • 用於分析多元微積分中函式的梯度和導數。

  • 求解系統的微分方程。

  • 計算向量值函式的逆。

  • 分析動態系統的穩定性。

在PyTorch中計算雅可比矩陣:

首先,我們必須使用以下命令安裝pytorch模組:

pip install torch

要計算雅可比矩陣值,我們使用PyTorch提供的函式`torch.autograd.functional.jacobian()`,它用於計算任何給定函式的雅可比值。此函式的引數如下:

  • func - 這是一個python函式,它以張量作為輸入,並在對輸入張量執行某些操作後返回張量或張量元組作為輸出。

  • inputs - 這是我們想要傳送到func函式的輸入,它可以是張量或張量元組。

讓我們看看計算雅可比矩陣的不同程式。

示例1:建立簡單矩陣並計算雅可比矩陣。

import torch

mat = torch.tensor([[1.0, 2.0],[3.0, 4.0]])
jacobian = torch.autograd.functional.jacobian(lambda val: val.sum(), mat)
print("Jacobian Matrix:")
print(jacobian)

輸出

Jacobian Matrix: tensor([[1., 1.], [1., 1.]])

解釋

在上面的程式中,我們使用PyTorch提供的張量函式建立了一個2*2矩陣。我們使用`torch.autograd.functional`中的雅可比函式來計算雅可比矩陣。`lambda` val: val.sum() 將矩陣作為輸入並計算矩陣的總和。

示例2:計算矩陣乘法函式的雅可比矩陣。

import torch

def mat_mul(A, B):
   return torch.mm(A, B)
mat1 = torch.tensor([[2.0, 3.0],[4.0, 5.0]])
mat2 = torch.tensor([[1.0, 2.0],[3.0, 4.0]])
jacobian = torch.autograd.functional.jacobian(lambda x: mat_mul(mat1, x), mat2)
print("Jacobian Matrix:")
print(jacobian)

輸出

Jacobian Matrix: tensor([[[[2., 0.], [3., 0.]], [[0., 2.], [0., 3.]]], [[[4., 0.], [5., 0.]], [[0., 4.], [0., 5.]]]])

解釋

在上面的程式中,我們使用PyTorch提供的`torch.mm`函式建立了兩個矩陣mat1、mat2。我們使用雅可比函式使用函式`lambda x: mat_mul(mat1, x), mat2`來計算雅可比矩陣。

示例3:透過建立隨機矩陣來計算雅可比矩陣。

import torch

rand_matrix = torch.randn((3, 2))
jacobian = torch.autograd.functional.jacobian(lambda val: val.sum(), rand_matrix)
print("Jacobian Matrix:")
print(jacobian)

輸出

Jacobian Matrix: tensor([[1., 1.], [1., 1.], [1., 1.]])

解釋

在上面的程式中,我們使用建立使用標準正態分佈的值的`torch.randn`函式建立了一個3*2隨機矩陣。我們使用雅可比函式`torch.autograd.functional`來計算雅可比矩陣元素的總和。

示例4:矩陣平方函式的雅可比矩陣。

import torch

def mat_sq(M):
   return torch.mm(M, M)
mat= torch.tensor([[2.0, 3.0],[4.0, 5.0]])
jacobian = torch.autograd.functional.jacobian(lambda ele: mat_sq(ele), mat)
print("Jacobian Matrix:")
print(jacobian)

輸出

Jacobian Matrix: 
tensor([[[[ 4., 4.], 
          [ 3., 0.]], 

         [[ 3., 7.], 
          [ 0., 3.]]], 

        [[[ 4., 0.], 
          [ 7., 4.]], 

         [[ 0., 4.], 
          [ 3., 10.]]]])

解釋

在上面的程式中,我們建立了一個名為`mat_sq`的函式,它用於使用PyTorch提供的`torch.mm`函式建立矩陣平方。我們使用張量函式建立了一個2*2矩陣。我們使用雅可比函式使用函式`lambda` ele: matrix_square(ele)來計算雅可比矩陣。

示例5:計算二維NumPy陣列矩陣的雅可比矩陣。

import torch
import numpy as np

array = np.array([[1.0, 2.0, 3.0],[4.0, 5.0, 6.0]])
mat = torch.tensor(array)
jacobian = torch.autograd.functional.jacobian(lambda val: val.sum(), mat)
print("Jacobian Matrix:")
print(jacobian)

輸出

Jacobian Matrix: tensor([[1., 1., 1.], [1., 1., 1.]], dtype=torch.float64)

解釋

在上面的程式中,我們使用`torch.tensor`將二維NumPy陣列轉換為PyTorch張量。我們使用`torch.autograd.functional`中的雅可比函式來計算雅可比矩陣。我們將求和函式和矩陣作為輸入引數傳遞。

因此,我們瞭解了PyTorch中的雅可比矩陣以及如何在不同情況下計算雅可比矩陣值。我們透過建立使用PyTorch的矩陣並使用各種程式來計算雅可比值。我們可以更改值和函式,並在機器學習任務的最佳化中使用雅可比矩陣。

更新於:2023年10月3日

瀏覽量:255

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.