美文网首页
求偏导的一般形式

求偏导的一般形式

作者: chenymcan | 来源:发表于2022-01-26 18:06 被阅读0次

    分析中的一些知识

    映射f: R^n \rightarrow R^m, x \mapsto f(x)=y,对x=(x_1, x_2, \cdots, x_n)^T \in R^n,存在y=(y_1, y_2, \cdots, y_m)^T=f(x) \in R^m

    f为线性函数时y=Wx,其中W \in R^{m\times n}。可拆解为:
    \large y = \begin{pmatrix} y_1 \\ y_2 \\ \vdots \\ y_m \\ \end{pmatrix} = \begin{pmatrix} r_1x \\ r_2x \\ \vdots \\ r_mx \\ \end{pmatrix} = \begin{pmatrix} w_{1,1}x_1+w_{1,2}x_2+\cdots+w_{1,n}x_n \\ w_{2,1}x_1+w_{2,2}x_2+\cdots+w_{2,n}x_n \\ \vdots \\ w_{m,1}x_1+w_{m,2}x_2+\cdots+w_{m,n}x_n \\ \end{pmatrix}

    一阶梯度为jaccob矩阵
    \large J = \begin{pmatrix} \frac{\partial y_1}{\partial x_1} & \frac{\partial y_1}{\partial x_2} &\cdots& \frac{\partial y_1}{\partial x_n}\\ \frac{\partial y_2}{\partial x_1} & \frac{\partial y_2}{\partial x_2} &\cdots& \frac{\partial y_2}{\partial x_n} \\ \vdots & \vdots & \ddots & \vdots \\ \frac{\partial y_m}{\partial x_1} & \frac{\partial y_m}{\partial x_2} &\cdots& \frac{\partial y_m}{\partial x_n} \end{pmatrix}_{(m \times n)} \in \mathbb{R}^{m,n}
    特别的,当m=1时,一阶梯度为:
    \frac{\partial{y}}{\partial{x}} = \nabla_x (y) = (\frac{\partial{y}}{\partial{x_1}},\frac{\partial{y}}{\partial{x_2}}, \cdots, \frac{\partial{y}}{\partial{x_n}})^T \in \mathbb{R}^n

    在pytorch的autograd.grad函数或backward方法中,grad_outputs/grad_tensors 是一个与outputs的形状一致的向量,即:
    \large \mathrm{grad\_outputs}=(a_1, a_2, \cdots,a_m)^T
    在给定grad_outputs 之后,真正返回的梯度为:
    \large grad = J^T \times \mathrm{grad\_outputs} = \begin{pmatrix} a_1\frac{\partial y_1}{\partial x_1}+a_2\frac{\partial y_2}{\partial x_2}+\cdots+a_m\frac{\partial y_m}{\partial x_1}\\ a_1\frac{\partial y_2}{\partial x_2}+a_2\frac{\partial y_1}{\partial x_2}+\cdots+a_m\frac{\partial y_m}{\partial x_2}\\ \cdots\cdots\cdots\cdots \\ a_1\frac{\partial y_1}{\partial x_n}+a_2\frac{\partial y_1}{\partial x_n}+\cdots+a_m\frac{\partial y_m}{\partial x_n}\\ \end{pmatrix}_{n\times 1} \in \mathbb{R}^n.
    输出的梯度与inputs形状一致的向量,相当于是将y中每个维度的梯度进行加权求和。

    参考pytorch官网的关于求导教程,我将其重新总结一下,意思是在得到y\in R^m后用于计算损失:z=g(y) \in R^1,所以zx的梯度就根据<u>链式法则</u>可以写为:(这里雅可比算子记为Dyx的导数记为D_x (y)
    \frac{\partial z}{\partial x} = \sum_{i=1}^m \frac{\partial z}{\partial y_i} \frac{\partial y_i}{\partial x} = \sum_{i=1}^m \frac{\partial z}{\partial y_i} \begin{pmatrix} \frac{\partial y_i}{\partial x_1} \\ \frac{\partial y_i}{\partial x_2} \\ \vdots \\ \frac{\partial y_i}{\partial x_n} \\ \end{pmatrix}_{n \times 1} = \begin{pmatrix} \sum_{i=1}^m \frac{\partial z}{\partial y_i} \frac{\partial y_i}{\partial x_1} \\ \sum_{i=1}^m \frac{\partial z}{\partial y_i} \frac{\partial y_i}{\partial x_2} \\ \vdots \\ \sum_{i=1}^m \frac{\partial z}{\partial y_i} \frac{\partial y_i}{\partial x_n} \\ \end{pmatrix}_{n \times 1} \\ = [ \begin{pmatrix} \frac{\partial z}{\partial y_1} & \frac{\partial z}{\partial y_2} & \cdots & \frac{\partial z}{\partial y_m} \\ \end{pmatrix}_{1 \times m} \times \begin{pmatrix} \frac{\partial y_1}{\partial x_1} & \frac{\partial y_1}{\partial x_2} &\cdots& \frac{\partial y_1}{\partial x_n}\\ \frac{\partial y_2}{\partial x_1} & \frac{\partial y_2}{\partial x_2} &\cdots& \frac{\partial y_2}{\partial x_n} \\ \vdots & \vdots & \ddots & \vdots \\ \frac{\partial y_m}{\partial x_1} & \frac{\partial y_m}{\partial x_2} &\cdots& \frac{\partial y_m}{\partial x_n} \end{pmatrix}_{n\times m} ]^T \\ = [ \begin{pmatrix} \nabla_y (z)^T \end{pmatrix}_{1 \times m} \times \begin{pmatrix} D_x (y) \end{pmatrix}_{n \times m} ]^T = \begin{pmatrix} D_x (y)^T \end{pmatrix}_{n \times m} \times \begin{pmatrix} \nabla_y (z) \end{pmatrix}_{m \times 1}
    或者根据<u>维度对齐</u>,反向推出:
    \frac{\partial z}{\partial x} = \begin{pmatrix} \frac{\partial z}{\partial x_1} \\ \frac{\partial z}{\partial x_2} \\ \vdots \\ \frac{\partial z}{\partial x_n} \\ \end{pmatrix}_{n \times 1} = \begin{pmatrix} D_x (y)^T \end{pmatrix}_{n \times m} \times \begin{pmatrix} \nabla_y (z) \end{pmatrix}_{m \times 1} \\ = \begin{pmatrix} \frac{\partial y_1}{\partial x_1} & \frac{\partial y_2}{\partial x_1} &\cdots& \frac{\partial y_m}{\partial x_1} \\ \frac{\partial y_1}{\partial x_2} & \frac{\partial y_2}{\partial x_2} &\cdots& \frac{\partial y_m}{\partial x_2} \\ \vdots & \vdots & \ddots & \vdots \\ \frac{\partial y_1}{\partial x_n} & \frac{\partial y_2}{\partial x_n} &\cdots& \frac{\partial y_m}{\partial x_n} \end{pmatrix}_{n \times m} \times \begin{pmatrix} \frac{\partial z}{\partial y_1} \\ \frac{\partial z}{\partial y_2} \\ \vdots \\ \frac{\partial z}{\partial y_m} \\ \end{pmatrix}_{m \times 1} = \begin{pmatrix} \sum_{i=1}^n \frac{\partial y_i}{\partial x_1} \frac{\partial z}{\partial y_i} \\ \sum_{i=1}^n \frac{\partial y_i}{\partial x_1} \frac{\partial z}{\partial y_i} \\ \vdots \\ \sum_{i=1}^n \frac{\partial y_i}{\partial x_1} \frac{\partial z}{\partial y_i} \\ \end{pmatrix}_{n \times 1} \\ = \sum_{i=1}^m \frac{\partial z}{\partial y_i} \begin{pmatrix} \frac{\partial y_i}{\partial x_1} \\ \frac{\partial y_i}{\partial x_2} \\ \vdots \\ \frac{\partial y_i}{\partial x_n} \\ \end{pmatrix}_{n \times 1} = \sum_{i=1}^m \frac{\partial z}{\partial y_i} \frac{\partial y_i}{\partial x}
    以上可以作为一般的复合多元函数的求导公式。

    pytorch官网的教程

    pytorch中求导函数还有两个参数:

    • retain_graph如果为True,则每次backward后,梯度会累加,如线性层中参数b.grad开始时为0,第一次backward后b.grad=1,再一次backward候b.grad变为2。

    分两种情况考虑:一个节点衍生出多个节点:比如x=2z, y=z^2这种z生成了x和y。还有就是多个节点衍生出一个节点比如:要计算z=x^2 y(这里x,y,z均为标量)的导数,有两个变量x,y\frac{\partial z}{\partial y} =x^2\frac{\partial z}{\partial x} =2xy,计算图如下:

    两种计算图.png

    grad与backward的最大区别就是前者需要指定输入输出,并将计算的梯度结果以return形式返回;而后者不用指定输入输出,计算的梯度结果直接存入叶子节点的grad属性中。

    • create_graph如果要计算高阶导数,则必须选为True。

    另外,更高维度的pytorch求导,可以参考:https://blog.csdn.net/waitingwinter/article/details/105774720
    因为本人暂时用不到多元符合函数高阶求导,就没有去验证是否正确。

    相关文章

      网友评论

          本文标题:求偏导的一般形式

          本文链接:https://www.haomeiwen.com/subject/ncwnhrtx.html