Chain Rule (Multivariable)

1 min read

If y=f(g(x))y = f(g(x)), the chain rule gives: dydx=dfdg⋅dgdx\frac{dy}{dx} = \frac{df}{dg} \cdot \frac{dg}{dx}

Multivariable generalization: if y=f(g(x))\mathbf{y} = f(\mathbf{g}(\mathbf{x})) where f:Rm→Rkf: \mathbb{R}^m \to \mathbb{R}^k and g:Rn→Rmg: \mathbb{R}^n \to \mathbb{R}^m:

∂y∂x=∂f∂g⋅∂g∂x\frac{\partial \mathbf{y}}{\partial \mathbf{x}} = \frac{\partial f}{\partial \mathbf{g}} \cdot \frac{\partial \mathbf{g}}{\partial \mathbf{x}}

This is a product of Jacobians: (k×m)⋅(m×n)=(k×n)(k \times m) \cdot (m \times n) = (k \times n).

Why this is everything in deep learning:

  • Backpropagation is the chain rule applied to a computation graph in reverse
  • For L=loss(f3(f2(f1(x))))L = \text{loss}(f_3(f_2(f_1(\mathbf{x})))): ∂L∂x=∂L∂f3⋅∂f3∂f2⋅∂f2∂f1⋅∂f1∂x\frac{\partial L}{\partial \mathbf{x}} = \frac{\partial L}{\partial f_3} \cdot \frac{\partial f_3}{\partial f_2} \cdot \frac{\partial f_2}{\partial f_1} \cdot \frac{\partial f_1}{\partial \mathbf{x}}
  • Each layer contributes one Jacobian factor; the backward pass accumulates them right-to-left

Concrete example: z=σ(Wx+b)z = \sigma(W\mathbf{x} + \mathbf{b}), L=∥z−y∥2L = \|z - y\|^2

∂L∂W=∂L∂z⋅∂z∂(Wx+b)⋅∂(Wx+b)∂W\frac{\partial L}{\partial W} = \frac{\partial L}{\partial z} \cdot \frac{\partial z}{\partial (W\mathbf{x}+\mathbf{b})} \cdot \frac{\partial (W\mathbf{x}+\mathbf{b})}{\partial W}

See also: Gradient, Backpropagation

Linked from