pytorch 深度学习之微积分

导数和微分

如果的 f 导数存在,这个极限被定义为:

f(x)=limh0f(x+h)f(x)h

如果 f(a) 存在,则称 fa 处是可微(differentiable)的。 如果 f 在一个区间内的每个数上都是可微的,则此函数在此区间中是可微的。导数 f(x) 解释为 f(x) 相对于 x 的瞬时(instantaneous)变化率。 所谓的瞬时变化率是基于 x 中的变化 h,且 h 接近 0。

举例:u=f(x)=3x24x

def f(x): return 3 * x ** 2 -4 * x def numerical_lim(f,x,h): return (f(x+h)-f(x)) / h h = 0.1 for i in range(5): print(f'h={h:.5f}, numerical limit={numerical_lim(f, 1, h):.5f}') h *= 0.1
h=0.10000, numerical limit=2.30000 h=0.01000, numerical limit=2.03000 h=0.00100, numerical limit=2.00300 h=0.00010, numerical limit=2.00030 h=0.00001, numerical limit=2.00003

导数的几个等价符号:

f(x)=y=dydx=dfdx=ddxf(x)=Df(x)=Dxf(x)

常见函数求微分:

  • DC=0C 是一个常数 )
  • Dxn=nxn1 (幂律 (power rule) , n 是任意实数)
  • Dex=ex
  • Dln(x)=1/x

常用法则:

  • 常数相乘法则:

    ddx[Cf(x)]=Cddxf(x)

  • 加法法则:

    ddx[f(x)+g(x)]=ddxf(x)+ddxg(x)

  • 乘法法则:

    ddx[f(x)g(x)]=f(x)ddx[g(x)]+g(x)ddx[f(x)]

  • 除法法则:

    ddx[f(x)g(x)]=g(x)ddx[f(x)]f(x)ddx[g(x)][g(x)]2

%matplotlib inline import numpy as np from IPython import display from matplotlib import pyplot as plt def use_svg_display(): display.set_matplotlib_formats('svg') def set_figsize(figsize=(3.5, 2.5)): use_svg_display() plt.rcParams['figure.figsize'] = figsize def set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend): """设置matplotlib的轴""" axes.set_xlabel(xlabel) axes.set_ylabel(ylabel) axes.set_xscale(xscale) axes.set_yscale(yscale) axes.set_xlim(xlim) axes.set_ylim(ylim) if legend: axes.legend(legend) axes.grid() def plot(X, Y=None, xlabel=None, ylabel=None, legend=None, xlim=None, ylim=None, xscale='linear', yscale='linear', fmts=('-', 'm--', 'g-.', 'r:'), figsize=(3.5, 2.5), axes=None): """绘制数据点""" if legend is None: legend = [] set_figsize(figsize) axes = axes if axes else plt.gca() # 如果X有一个轴,输出True def has_one_axis(X): return (hasattr(X, "ndim") and X.ndim == 1 or isinstance(X, list) and not hasattr(X[0], "__len__")) if has_one_axis(X): X = [X] if Y is None: X, Y = [[]] * len(X), X elif has_one_axis(Y): Y = [Y] if len(X) != len(Y): X = X * len(Y) axes.cla() for x, y, fmt in zip(X, Y, fmts): if len(x): axes.plot(x, y, fmt) else: axes.plot(y, fmt) set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend) x = np.arange(0, 3, 0.1) plot(x, [f(x), 2 * x - 3], 'x', 'f(x)', legend=['f(x)', 'Tangent line (x=1)'])
C:\Users\Administrator\AppData\Local\Temp\ipykernel_9800\3988637146.py:7: DeprecationWarning: `set_matplotlib_formats` is deprecated since IPython 7.23, directly use `matplotlib_inline.backend_inline.set_matplotlib_formats()` display.set_matplotlib_formats('svg')

偏导数

y=f(x1,x2,...xn) 是一个具有 n 个变量的函数,y 关于第 i 个参数 xn 的偏导数为:

yxi=limh0f(x1,...xi1,xi+h,xi+1,...xn)f(x1,...xi,...,xn)h

为了计算 fracyxi,可以简单地将 x1,...xi1,xi+1,...xn 看作常数,并计算 y 关于 xi 的导数,对于偏导数的表示,以下形式等价:

yxi=fxi=fxi=fi=Dif=Dxif

梯度

可以连结一个多元函数对其所有变量的偏导数,以得到该函数的梯度(gradient)向量。设函数 f:RnR 的输入是一个 n 维向量 x=[x1,x2,...xn]T ,并且输出是一个标量。函数 f(x) 相对于 x 的梯度是一个包含 n 个偏导数的向量:

xf(x)=[f(x)x1,f(x)x2,...,f(x)xn]T

其中 xf(x) 通常在没有歧义时被 f(x) 取代。

假设 xn 维向量,在微分多元函数时经常使用如下规则:

  • 对于所有 ARm×n,都有 xAx=AT
  • 对于所有 ARn×m,都有 xxTA=A
  • 对于所有 ARn×n,都有 xxTAx=(A+AT)x
  • xx2=xxTx=2x

链式法则

假设函数 y=f(u)u=g(x),则根据链式求导法则:

dydx=dydududx

假设可微分函数 y 有变量 u1,u2,...um 其中,每个可微函数 ui 都有变量 x1,x2,...,xn,则根据链式求导法则:

dydxi=dydu1du1dxi+dydu2du2dxi++dydumdumdxi


__EOF__

本文作者刘皇叔
本文链接https://www.cnblogs.com/xiaojianliu/p/16159345.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   刘-皇叔  阅读(395)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
历史上的今天:
2020-04-18 Sphinx + GitHub + ReadtheDocs 创建电子书
点击右上角即可分享
微信分享提示