← Module 05 / Calculus & optimization

Lesson 05.6 · §4.5.1

How does a change travel through a composition?

Multiply Jacobians in the correct order and propagate a scalar gradient.

In this lesson 5 ideas
  1. Track one input through two functions
  2. Turn the Jacobian row into a gradient column
  3. Transfer
  4. Explore the relationship
  5. Check your reasoning

Source scope: §4.5.1, PDF pages 28. Numerical examples, experiments, added derivations, and practice are teaching supplements unless identified as source results.

Track one input through two functions

Let g(x)=(x,x2)Tg(x)=(x,x^2)^T and f(z1,z2)=z1+3z2f(z_1,z_2)=z_1+3z_2. Direct substitution gives (f∘g)(x)=x+3x2(f\circ g)(x)=x+3x^2 and derivative 1+6x1+6x. We can get the same answer by multiplying local derivatives rather than expanding the composition.

The single-variable chain rule is

(f∘g)′(x)=f′(g(x))g′(x).(f\circ g)'(x)=f'(g(x))g'(x).

The outer derivative is evaluated at the inner output, not at the original input.

Proposition 12. For differentiable g:Rn→Rmg:\mathbb R^n\to\mathbb R^m and f:Rm→Rkf:\mathbb R^m\to\mathbb R^k,

Jf∘g(x)=Jf(g(x))Jg(x).J_{f\circ g}(x)=J_f(g(x))J_g(x).

The dimensions are (k×m)(m×n)=k×n(k\times m)(m\times n)=k\times n. The rightmost matrix acts first on an input increment. Differentiability at the relevant points is an explicit prerequisite of the rule; the source leaves it implicit in the statement.

In our example Jg(x)=(1,2x)TJ_g(x)=(1,2x)^T is 2×12\times1 and Jf=(1,3)J_f=(1,3) is 1×21\times2. Their product is 1+6x1+6x. At x=2x=2, the two pathways contribute 11 and 1212, giving 1313 in total.

Turn the Jacobian row into a gradient column

Corollary 1. When k=1k=1, f:Rm→Rf:\mathbb R^m\to\mathbb R and g:Rn→Rmg:\mathbb R^n\to\mathbb R^m give

∇(f∘g)(x)=Jg(x)T∇f(g(x)).\nabla(f\circ g)(x)=J_g(x)^T\nabla f(g(x)).

The shape is (n×m)(m×1)=n×1(n\times m)(m\times1)=n\times1. Transposing the Jacobian product reverses factor order: (JfJg)T=JgTJfT(J_fJ_g)^T=J_g^TJ_f^T. This explains both the transpose and the backward direction of scalar derivative propagation. The source states the Jacobian rule without proof and obtains the corollary from ∇f=JfT\nabla f=J_f^T.

Added explanation of the rule. A small input change hh produces JghJ_gh in the intermediate space and then Jf(Jgh)J_f(J_gh) in the output space. Differentiability makes omitted errors small relative to ∥h∥\|h\|. This composition is the product JfJgJ_fJ_g, with derivatives evaluated at their respective base points.

Transfer

Let g(x)=Bx−bg(x)=Bx-b for B∈Rm×nB\in\mathbb R^{m\times n}, and f(z)=∥z∥22f(z)=\|z\|_2^2. Then Jg=BJ_g=B and ∇f(z)=2z\nabla f(z)=2z, so ∇(f∘g)(x)=2BT(Bx−b)\nabla(f\circ g)(x)=2B^T(Bx-b). Its nn entries describe how the loss changes with each input parameter, not with each of the mm residuals.

Explore the relationship

Explore it yourself

Follow both derivative paths

How do the two paths from x to the scalar output combine?

Preparing interactive controls…

x ∈ R2
→
g(x) = (x, x²)(2, 4)
→
f(z) = z₁ + 3z₂14

Jg = (1, 4)ᵀ (2 × 1). Jf = (1, 3) (1 × 2).

Preview: Jf Jg = 1 × 1 + 3 × 4 = 13 (1 × 1).

Gradient form: Jgᵀ ∇f has shape (1 × 2)(2 × 1), also 13.

One path passes through z₁ = x with derivative 1. The other passes through z₂ = x² with derivative 2x, then multiplies by 3. Add the paths because f adds their outputs. Stage controls highlight the calculation without timed motion.

Check your reasoning

Make a prediction

If g maps R\u00b3 to R\u00b2 and f maps R\u00b2 to R, what is the shape of the composite gradient?