PyTorch中如何进行向量微分、矩阵微分、计算雅各比行列式

摘要

本文介绍了在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 ) =

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值