当前位置:网站首页>解密NumPy求解梯度的一个关键难点
解密NumPy求解梯度的一个关键难点
2022-07-25 06:53:00 【算法channel】
关于numpy的gradient方法,在参数edge_order等于2时,如何得到左右两个边界值,是比较难理解的地方,而网上的博文几乎都千篇一律,都没有对此做出正确的解释。
而关注我的老粉「高新区运气王」经过深思熟虑,找出规律,总结出edge_order等于2时左右两个边界值的推导,难能可贵!在此以飨关注我的读者。
1 Numpy求梯度
import numpy as np
x = np.random.randint(10, size=(6, ))
f = x**2
print(f"f:{f}")
# >> 0, 1, 4, 9, 16
grad1 = np.gradient(f)
print(f"不设置edge_order时, f的默认梯度:{grad1}")
grad2 = np.gradient(f, edge_order=1)
print(f"edge_order=1时,f的梯度:{grad2}")
print(f"查看f的默认梯度是否和edge_order=1时的梯度值相等:{(grad1==grad2).all()}")
grad3 = np.gradient(f, edge_order=2)
print(f"edge_order=2时, f的梯度:{grad3}")输出结果如下:
f:[49 36 36 9 4 49]
不设置edge_order时, f的默认梯度:[-13. -6.5 -13.5 -16. 20. 45. ]
edge_order=1时,f的梯度:[-13. -6.5 -13.5 -16. 20. 45.]
查看f的默认梯度是否和edge_order=1时的梯度值相等:True
edge_order=2时, f的梯度:[-19.5 -6.5 -13.5 -16. 20. 70. ]2 一阶中心差分
这里我对整个过程进行解析:
f的计算这里应该不用说,就是对生成的一维随机矩阵x求平方得到的。
根据打印结果我们会发现,默认情况下和edge_order=1时,f的梯度是一样的。
具体怎么算的呢?
先说下边界,整体来讲就是,左边界:f[1] – f[0],右边界:f[-1] – f[-2]
比如本例中,左边界 = 36 – 49 = -13,右边界 = 49 - 4 = 45
再说下中间梯度:就是用的一阶中心差分,简而言之就是:
比如本例中,f的第二个梯度值 = (f的第三个数 – f的第一个数) / 2 = (36 - 49) / 2 = -6.5, 其他中间梯度值的计算也是同理。
3 理解难点(edge_order=2)
关键地方来了, edge_order=2时我们会发现,所得到的梯度值中间部分跟edge_order=1(也就是默认值)是一样的,区别就在于边界值。
按照官方文档来说edge_order=2就是利用边界处的二阶精确差计算梯度,具体啥意思呢?
网上博客也是千篇一律,都没有给出具体的计算过程。
这里我们对计算过程进行解析:
左边界 = 2*(edge_order=1时f的第一个梯度值) - (edge_order=1时f的第二个梯度值)
即:
402 Payment Required
(这里两撇表示edge_order=2时的梯度,表示f的第i个值)
大家可以自行跟二阶前向差分对比下,需要注意的是numpy里面求梯度这里中间部分的一阶差分是用的一阶中心差分。
右边界 = 2*(edge_order=1时f的最后一个梯度值) - (edge_order=1时f的倒数第二个梯度值)
即:
402 Payment Required
大家也可以自行推下这里,并跟二阶后向差分对比下。
本例中:
左边界 = 2*(-13) - (-6.5) = -19.5
左边界 = 2*(45) - (20) = 70
可以收藏下,日后你可能会用到。
边栏推荐
- EXCEL
- [Yugong series] July 2022 go teaching course 015 assignment operators and relational operators of operators
- GIS实战应用案例100篇(十七)-基于DEM制作三维地图
- 【transformer】DeiT
- 健康打卡每日提醒累了?那就让自动化帮你---HiFlow,应用连接自动化助手
- C#--MetroFramework框架调用metroModernUI库,并在工具栏使用
- JSON、
- A scene application of 2D animation
- C # read Beckhoff variable
- Rongyun launched a real-time community solution and launched "advanced players" for vertical interest social networking
猜你喜欢

JSON、

Health clock in daily reminder tired? Then let automation help you -- hiflow, application connection automation assistant

GIS实战应用案例100篇(十七)-基于DEM制作三维地图

100 GIS practical application cases (seventeen) - making 3D map based on DEM

Easy gene chip SEQ analysis method: practical workflow and advanced applications

你了解PowerBI中的去年同期吗

How to convert multi row data into multi column data in MySQL

百度希壤首场元宇宙拍卖落槌,陈丹青六幅版画作品全部成交!

JZ7 rebuild binary tree

Qt实战案例(53)——利用QDrag实现拖拽拼图功能
随机推荐
Two week learning results of machine learning
机器人工程-教学品质-如何判定
When the graduation season comes, are you ready? What are we going to do
JSON、
Mysql database
JS data type judgment - Case 6 delicate and elegant judgment of data type
The income of bank financial management is getting lower and lower. Now which financial products have high income?
常吃发酵馒头是否会伤害身体
labelme标注不同物体显示不同颜色以及批量转换
YOLOv7模型推理和训练自己的数据集
%d,%s,%c,%x
共模电感听过很多次,但是什么原理你们真的懂吗?
如何学习 C 语言?
代码中的软件工程:正则表达式十步通关
Can communication test based on STM32: turn the globe
Microorganisms are healthy. Don't exclude microorganisms in the human body
How can dbcontext support the migration of different databases in efcore advanced SaaS system
The code of Keil and Si compiler is not aligned??
Introduction to the usage of explain and the meaning of result field in MySQL
RecycleView实现item重叠水平滑动