求偏导的一般形式

分析中的一些知识

映射,对,存在,

当为线性函数时,其中。可拆解为:
\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}
特别的,当时,一阶梯度为:

在pytorch的autograd.grad函数或backward方法中,grad_outputs/grad_tensors 是一个与outputs的形状一致的向量,即:

在给定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形状一致的向量,相当于是将中每个维度的梯度进行加权求和。

参考pytorch官网的关于求导教程,我将其重新总结一下,意思是在得到后用于计算损失:,所以对的梯度就根据链式法则可以写为:(这里雅可比算子记为,对的导数记为)
\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}
或者根据维度对齐,反向推出:
\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。

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

两种计算图.png

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

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

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

你可能感兴趣的:(求偏导的一般形式)