jax框架为例:求hession矩阵时前后向模式的自动求导的性能差别
注意:本文相关基础知识不介绍。
给出代码:
from jax import jacfwd, jacrev
import jax.numpy as jnp
def hessian_1(f):
return jacfwd(jacrev(f))
def hessian_2(f):
return jacfwd(jacfwd(f))
def hessian_3(f):
return jacrev(jacfwd(f))
def hessian_4(f):
return jacrev(jacrev(f))
def f(x):
return (x ** 2).sum()
print(hessian_1(f)(jnp.ones((100,))))
print(hessian_2(f)(jnp.ones((100,))))
print(hessian_3(f)(jnp.ones((100,))))
print(hessian_4(f)(jnp.ones((100,))))
import time
a=time.time()
hessian_1(f)(jnp.ones((100,)))
b=time.time()
print(b-a)
hessian_2(f)(jnp.ones((100,)))
c=time.time()
print(c-b)
hessian_3(f)(jnp.ones((100,)))
d=time.time()
print(d-b)
hessian_4(f)(jnp.ones((100,)))
e=time.time()
print(e-d)
运算结果:



结论(不一定正确):
两次求导均使用后向模式的要比两次求导均使用前向模式的要速度快,并且两次求导使用相同模式的要比两次求导分别使用不同模式的速度要快;
第一次求导使用后向模式,第二次求导使用前向模式,要比第一次求导使用前向模式,第二次求导使用反向模式的速度要快。
修改代码:
from jax import jacfwd, jacrev
import jax.numpy as jnp
from jax import jit
def hessian_1(f):
return jacfwd(jacrev(f))
def hessian_2(f):
return jacfwd(jacfwd(f))
def hessian_3(f):
return jacrev(jacfwd(f))
def hessian_4(f):
return jacrev(jacrev(f))
@jit
def f(x):
return (x ** 2).sum()
x = jnp.ones((100,))
print(hessian_1(f)(x))
print(hessian_2(f)(x))
print(hessian_3(f)(x))
print(hessian_4(f)(x))
import time
a=time.time()
hessian_1(f)(x)
b=time.time()
print(b-a)
hessian_2(f)(x)
c=time.time()
print(c-b)
hessian_3(f)(x)
d=time.time()
print(d-b)
hessian_4(f)(x)
e=time.time()
print(e-d)
运算结果:

得出另一种结论(之所以上下两次结论不同,个人估计是这个函数太过于简单造成的):
(不一定正确)
两次求导均使用后向模式的要比两次求导均使用前向模式的要速度慢;
第一次求导使用后向模式,第二次求导使用前向模式,要比第一次求导使用前向模式,第二次求导使用反向模式的速度要快。
jax框架为例:求hession矩阵时前后向模式的自动求导的性能差别的更多相关文章
- [Pytorch框架] 1.4 Autograd:自动求导
文章目录 Autograd: 自动求导机制 张量(Tensor) 梯度 Autograd: 自动求导机制 PyTorch 中所有神经网络的核心是 autograd 包. 我们先简单介绍一下这个包,然后 ...
- EOJ3536 求蛇形矩阵每一行的和---找规律
题目链接: https://acm.ecnu.edu.cn/problem/3536/ 题目大意: 求蛇形矩阵的每一行的和,数据范围n<=200000. 思路: 由于n数据较大,所以感觉应该是需 ...
- Robot Framework测试框架用例脚本设计方法
Robot Framework介绍 Robot Framework是一个通用的关键字驱动自动化测试框架.测试用例以HTML,纯文本或TSV(制表符分隔的一系列值)文件存储.通过测试库中实现的关键字驱动 ...
- Hession矩阵(整理)
二阶偏导数矩阵也就所谓的赫氏矩阵(Hessian matrix). 一元函数就是二阶导,多元函数就是二阶偏导组成的矩阵. 求向量函数最小值时用的,矩阵正定是最小值存在的充分条件. 经济学中常常遇到求最 ...
- 轻松应对并发问题,简易的火车票售票系统,Newbe.Claptrap 框架用例,第一步 —— 业务分析
Newbe.Claptrap 框架非常适合于解决具有并发问题的业务系统.火车票售票系统,就是一个非常典型的场景用例. 本系列我们将逐步从业务.代码.测试和部署多方面来介绍,如何使用 Newbe.Cla ...
- 二维KMP - 求字符矩阵的最小覆盖矩阵 - poj 2185
Milking Grid Problem's Link:http://poj.org/problem?id=2185 Mean: 给你一个n*m的字符矩阵,让你求这个字符矩阵的最小覆盖矩阵,输出这个最 ...
- 一本通1641【例 1】矩阵 A×B
1641: [例 1]矩阵 A×B sol:矩阵乘法模板.三个for循环 #include <bits/stdc++.h> using namespace std; typedef lon ...
- Task 4.2 求一个矩阵的最大子矩阵的和
任务:输入一个二维整形数组,数组里有正数也有负数.二维数组中连续的一个子矩阵组成一个子数组,每个子数组都有一个和.求所有子数组的和的最大值.要求时间复杂度为O(n). (1)设计思想:把二维矩阵分解成 ...
- 解决使用elementUI框架el-upload跨域上传时session丢失问题
解决方法一: 1.使用elementUI框架el-upload跨域上传时,后端获取不到cookie,后端接口显示未登录,在添加了 with-credentials="true"后依 ...
- [深度学习] pytorch学习笔记(1)(数据类型、基础使用、自动求导、矩阵操作、维度变换、广播、拼接拆分、基本运算、范数、argmax、矩阵比较、where、gather)
一.Pytorch安装 安装cuda和cudnn,例如cuda10,cudnn7.5 官网下载torch:https://pytorch.org/ 选择下载相应版本的torch 和torchvisio ...
随机推荐
- LeetCode 295. Find Median from Data Stream数据流的中位数 (C++/Java)
题目: Median is the middle value in an ordered integer list. If the size of the list is even, there is ...
- Vue2复习
Vue2 插值.指令.动态属性.表达式.v-html 插值:{{ data }} 指令 & 动态属性:例子(:id="xxx") 表达式:可以用于赋值,写在{{}}里面 v ...
- python3读csv文件,出现UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 0: invalid con
使用csv.reader(file)读csv文件时,出现如下错误:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in positio ...
- macOS Big Sur 11.0.1光盘镜像文件制作
https://blog.csdn.net/hymnal/article/details/110393501
- QT学习:10 IO类
--- title: framework-cpp-qt-10-IO类 EntryName: framework-cpp-qt-10-QIODevice date: 2020-04-17 10:24:0 ...
- 【换源】git命令行迁移仓库
直接git clone的话,查看本地分支,会只有默认主分支,可能是master,也可以能是设置的. 查看所有分支 git branch -a * master remotes/origin/HEAD ...
- 嵌入式HLS 案例开发手册——基于Zynq-7010/20工业开发板(2)
目 录 2 led_flash 案例 19 2.1 HLS 工程说明 19 2.2 编译与仿真 20 2.3 IP 核测试 23 3 key_led_demo 案例 23 3.1 HLS 工程说明 2 ...
- VS图片
- jQuery -- 手稿
- Spring5.X的bean的scope作用域
scope属性 singleton:单例,默认值,调用getBean方法返回是同一个对象,实例会被缓存起来,效率比较高,当一个bean被标识为singleton时候,spring的IOC容器中只会存在 ...