Skip to content
Glacius
CalculusConcept reference

Multivariable chain rule

For differentiable vector maps in output-row convention, the composition Jacobian is the outer Jacobian at the inner output times the inner Jacobian at the original input.

On this page 9 sections
  1. Overview
  2. Understand the idea
  3. Let G(x,y)=(2x,y) and F(u,v)=(u^2,v)
  4. A closer look
  5. A parameter may influence several intermediate model values before they meet in the loss
  6. Key takeaway
  7. Further questions
  8. Sources & further reading
  9. Concept connections

01Understand the idea#

When several intermediate variables all depend on an input, that input can affect the final output along several paths. The multivariable chain rule adds the contributions from those paths. Multiplication carries a rate along one path; addition combines different paths.

For FGF\circ G, the inner map GG acts first. The outer map FF receives G(p)G(p), so its derivative must be evaluated there.

A schematic composition sends input p through G to G(p), then through F to F(G(p)). The corresponding derivative factors are J_G(p) for the first arrow and J_F(G(p)) for the second. Acting on an input change applies the inner factor first, so the matrix product is outer times inner.A schematic composition sends input p through G to G(p), then through F to F(G(p)). The corresponding derivative factors are J_G(p) for the first arrow and J_F(G(p)) for the second. Acting on an input change applies the inner factor first, so the matrix product is outer times inner.
Figure 1A schematic composition sends input p through G to G(p), then through F to F(G(p)). The corresponding derivative factors are J_G(p) for the first arrow and J_F(G(p)) for the second. Acting on an input change applies the inner factor first, so the matrix product is outer times inner.
Link to this figure ↗Download SVGDownload PNG

For differentiable maps, multiply the outer Jacobian by the inner Jacobian. We use output rows and input columns.

JFG(p)=JF(G(p))JG(p)\begin{gathered}J_{F\circ G}(p)\\=J_F(G(p))J_G(p)\end{gathered}

Let u=x2u=x^2, v=3xv=3x, and y=uvy=uv. At x=2x=2, the forward values are u=4,v=6u=4,v=6. Through uu, the contribution is (y/u)(du/dx)=6(4)=24(\partial y/\partial u)(du/dx)=6(4)=24. Through vv, it is (y/v)(dv/dx)=4(3)=12(\partial y/\partial v)(dv/dx)=4(3)=12. Adding gives dy/dx=36dy/dx=36.

Check your reasoning

JG(p)J_G(p) rows: (1,0);(2,1)(1,0);(2,1). JF(G(p))J_F(G(p)) rows: (3,0);(0,2)(3,0);(0,2). Return the ordered rows of JFG(p)J_{F\circ G}(p).

  1. A(3,0);(4,2)(3,0);(4,2)
  2. B(3,0);(6,2)(3,0);(6,2)
  3. C(3,0);(0,2)(3,0);(0,2)
Show answer and explanation
(3,0);(4,2)(3,0);(4,2)

(2,1)(2,1): 0+4=40+4=4.

02Let G(x,y)=(2x,y) and F(u,v)=(u^2,v)#

Let G(x,y)=(2x,y)G(x,y)=(2x,y) and F(u,v)=(u2,v)F(u,v)=(u^2,v). At p=(1,2)p=(1,2), first find the intermediate input.

G(1,2)=(2,2)G(1,2)=(2,2)

For G(x,y)=(2x,y)G(x,y)=(2x,y) and F(u,v)=(u2,v)F(u,v)=(u^2,v) at p=(1,2)p=(1,2), inner diagonal entries are 2,1. At G(p)=(2,2)G(p)=(2,2), outer entries are 4,1. Multiply to get 8,1.

JFG(1,2)=(8001)J_{F\circ G}(1,2)=\begin{pmatrix}8&0\\0&1\end{pmatrix}
Check your reasoning

G(x,y)=(x+y,y)G(x,y)=(x+y,y), F(u,v)=(u2,v)F(u,v)=(u^2,v). At p=(1,2)p=(1,2), give JFG(p)J_{F\circ G}(p) as ordered rows.

  1. A(2,2);(0,1)(2,2);(0,1)
  2. B(6,6);(0,1)(6,6);(0,1)
  3. C(6,1);(0,1)(6,1);(0,1)
Show answer and explanation
(6,6);(0,1)(6,6);(0,1)

Outer entry: 2(3)=62(3)=6.

03A closer look#

If GG has 2 inputs and 3 outputs, and FF has 3 inputs and 2 outputs, the Jacobians have shapes 3×23\times2 and 2×32\times3. Outer times inner gives the required 2×22\times2 result. Square factors still need the same order.

Check your reasoning

JG(p)J_G(p) rows: (1,2);(0,1)(1,2);(0,1). JF(G(p))J_F(G(p)) rows: (2,0);(1,1)(2,0);(1,1). Repair reversed factors: give JFG(p)J_{F\circ G}(p) as ordered rows.

  1. A(4,2);(1,1)(4,2);(1,1)
  2. B(2,0);(0,1)(2,0);(0,1)
  3. C(2,4);(1,3)(2,4);(1,3)
Show answer and explanation
(2,4);(1,3)(2,4);(1,3)

(2,2)(2,2): 2+1=32+1=3.

04A parameter may influence several intermediate model values before they meet in the loss#

A parameter may influence several intermediate model values before they meet in the loss. Omitting a path gives an incomplete gradient. When the same dependence is written using Jacobians, matrix multiplication performs these multiply-and-add operations with the dimensions kept in order.

Key takeaway

Evaluate the intermediate input, then multiply outer by inner.

  • Compute the derivative of a composed vector map using Jacobians.

Further questions

Why does the inner factor sit on the right?
Matrix products act on a vector from right to left. An input change first passes through the inner derivative, then through the outer derivative.

Sources & further reading

  1. [1]

Reference this concept

Link to this page, a section, or an individual figure.

Glacius. “Multivariable chain rule.” Math behind ML. /learn/c-multi-chain