计算 f ( x ) = [ f 1 = x 1 2 + 2 x 2 f 2 = 3 x 1 + 4 x 2 2 ] , J = [ ∂ f 1 ∂ x 1 ∂ f 1 ∂ x 2 ∂ f 2 ∂ x 1 ∂ f 2 ∂ x 2 ] = [ 2 x 1 2 3 8 x 2 ] \begin{equation} f(x)=\begin{bmatrix} f_1=x_1^2+2x_2\\\\f_2=3x_1+4x_2^2\end{bmatrix}, J=\begin{bmatrix} \frac{\partial f_1}{\partial x_1}&\frac{\partial f_1}{\partial x_2}\\\\ \frac{\partial f_2}{\partial x_1}&\frac{\partial f_2}{\partial x_2} \end{bmatrix}=\begin{bmatrix} 2x_1&2\\\\3&8x_2\end{bmatrix} \end{equation} f(x)=f1=x12+2x2f2=3x1+4x22,J=∂x1∂f1∂x1∂f2∂x2∂f1∂x2∂f2=2x1328x2
我们假设 x 1 = 1 , x 2 = 2 x_1=1,x_2=2 x1=1,x2=2,可得Jacobian matrix 表示如下: J = [ 2 x 1 2 3 8 x 2 ] = [ 2 2 3 16 ] \begin{equation} J=\begin{bmatrix} 2x_1&2\\\\3&8x_2\end{bmatrix}=\begin{bmatrix} 2&2\\\\3&16\end{bmatrix} \end{equation} J=2x1328x2=23216
因为fx为向量,所以在pytorch中一般是没有的,我们需要定义一个标量z z = [ 1 1 ] [ f 1 f 2 ] → ∂ z ∂ f = [ 1 1 ] \begin{equation} z=\begin{bmatrix}1&1\end{bmatrix}\begin{bmatrix}f_1\\\\f_2\end{bmatrix}\to \frac{\partial z}{\partial f}=\begin{bmatrix}1&1\end{bmatrix} \end{equation} z=[11]f1f2→∂f∂z=[11]
根据链式法则: ∂ z ∂ f ⋅ ∂ f ∂ x = ∂ z ∂ x \begin{equation} \frac{\partial z}{\partial f} \cdot\frac{\partial f}{\partial x}= \frac{\partial z}{\partial x} \end{equation} ∂f∂z⋅∂x∂f=∂x∂z
假设我们代入可得: ∂ z ∂ f ⋅ ∂ f ∂ x = [ 1 1 ] [ 2 2 3 16 ] = [ 5 18 ] \begin{equation} \frac{\partial z}{\partial f} \cdot\frac{\partial f}{\partial x}= \begin{bmatrix}1&1\end{bmatrix}\begin{bmatrix} 2&2\\\\3&16\end{bmatrix}=\begin{bmatrix}5&18\end{bmatrix} \end{equation} ∂f∂z⋅∂x∂f=[11]23216=[518]
根据pytorch中的自动微分,我们可以得到 ∂ z ∂ x = [ 5 18 ] \begin{equation} \frac{\partial z}{\partial x}=\begin{bmatrix}5&18\end{bmatrix} \end{equation} ∂x∂z=[518]