摘要
本文介绍了在PyTorch中进行向量微分、矩阵微分以及计算雅各比行列式的方法。通过对自动微分(Autograd)功能的讲解,展示了如何轻松实现复杂的数学运算,如向量和矩阵的导数计算,以及通过雅各比矩阵和雅各比行列式对函数的线性变换特性进行分析。
Abstract
This article introduces methods for performing vector differentiation, matrix differentiation, and computing Jacobian determinants in PyTorch. Through an explanation of the automatic differentiation (Autograd) functionality, it demonstrates how to easily implement complex mathematical operations, such as calculating derivatives of vectors and matrices, and analyzing the properties of functions’ linear transformations via Jacobian matrices and Jacobian determinants.
一、计算雅各比行列式
需要传入函数和函数的输入
import torch
from torch.autograd.functional import jacobian
def func(x):
return x.exp().sum(dim=1)
x = torch.randn(2, 3)
y = func(x)
print(x)
'''
tensor([[-0.2497, -0.8842, 0.6314],
[-0.0687, -1.5360, 1.4695]])
'''
print(y) # tensor([3.0724, 5.4959])
# exp(-0.2497)+exp(-0.8842)+exp(0.6314)=3.0724
# exp(-0.0687)+exp(-1.5360)+exp(1.4695)=5.4959
print(jacobian(func, x))
输出结果是:
tensor([[-0.2497, -0.8842, 0.6314],
[-0.0687, -1.5360, 1.4695]])
tensor([3.0724, 5.4959])
tensor([[[0.7791, 0.4130, 1.8803],
[0.0000, 0.0000, 0.0000]],
[[0.0000, 0.0000, 0.0000],
[0.9336, 0.2152, 4.3470]]])
暂记: y 1 = e x p ( − 0.2497 ) + e x p ( − 0.8842 ) + e x p ( 0.6314 ) = 3.0724 y1=exp(-0.2497)+exp(-0.8842)+exp(0.6314)=3.0724 y1=exp(−0.2497)+exp(−0.8842)+exp(0.6314)=3.0724
y 2 = e x p ( − 0.0687 ) + e x p ( − 1.5360 ) + e x p ( 1.4695 ) =