Chain Rule (Multivariable)

1 min read

If y=f(g(x))y = f(g(x)), the chain rule gives: dydx=dfdgdgdx\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:RmRkf: \mathbb{R}^m \to \mathbb{R}^k and g:RnRmg: \mathbb{R}^n \to \mathbb{R}^m:

yx=fggx\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})))): Lx=Lf3f3f2f2f1f1x\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=zy2L = \|z - y\|^2

LW=Lzz(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