Unit 2, lesson 13

Derivatives, Partial Derivatives, and the Chain Rule

How derivatives, partial derivatives and the gradient measure how a loss changes with each parameter, how the chain rule tracks every factor of a prediction error, and how directional derivatives and finite differences check it.

  • Video 13
  • Calculus for Optimization

A one-feature model predicts 7 for a training example whose target is 8. If the weight is raised slightly, does the squared error go up or down, and how fast? Would raising the intercept instead have the same effect? Derivatives answer such questions for small changes. This lesson defines the derivative and the partial derivative, collects partial derivatives into the gradient, uses the chain rule to differentiate a squared prediction error through every intermediate step, and checks the result with a directional derivative and a finite difference.

Key terms

Derivative and differentiability

Definition Let ff be a real function defined on an open interval containing tt. The derivative of ff at tt is

f′(t)=lim⁡h→0f(t+h)−f(t)hf'(t) = \lim_{h \to 0} \frac{f(t + h) - f(t)}{h}

when this limit exists as a finite number, with hh approaching 00 through nonzero values from both sides. In that case ff is called differentiable at tt. Equivalently, there is a number f′(t)f'(t) with

f(t+h)=f(t)+f′(t) h+e(h),\begin{aligned} f(t + h) &= f(t) + f'(t)\,h \\[0.4em] &\quad + e(h), \end{aligned}

where e(h)/h→0e(h)/h \to 0 as h→0h \to 0. So near tt the function is approximated by a line with slope f′(t)f'(t). The derivative has the unit of ff divided by the unit of tt. If the difference quotient has different one-sided limits at tt, then ff is not differentiable there, and a method that still needs a slope at tt must state a convention. For a convex function such as ∣r∣\lvert r \rvert at r=0r = 0, a standard choice is a subgradient, any number gg with ∣s∣≥g s\lvert s \rvert \ge g\,s for every real ss, which means any gg in [−1,1][-1, 1].

In plain words The difference quotient is the average rate of change over a step of size hh, and the derivative is the value it settles on as the step shrinks. Being differentiable means that close enough to tt the graph looks like a straight line, so a small step hh changes the output by about f′(t) hf'(t)\,h. It says nothing about large steps.

Example For f(t)=t2f(t) = t^2 and any h≠0h \ne 0,

(t+h)2−t2h=2th+h2h=2t+h,\begin{aligned} \frac{(t + h)^2 - t^2}{h} &= \frac{2th + h^2}{h} \\[0.4em] &= 2t + h , \end{aligned}

which tends to 2t2t as h→0h \to 0. So f′(3)=6f'(3) = 6, and the limit argument is a proof for every tt, not a pattern read from a few numbers. The absolute value ∣r∣\lvert r \rvert has the difference quotient ∣h∣/h\lvert h \rvert / h at r=0r = 0, which equals −1-1 for h<0h < 0 and 11 for h>0h > 0. The one-sided limits disagree, so ∣r∣\lvert r \rvert has no derivative at 00. Software often uses the subgradient 00 there, which is a convention and not the value of a derivative.

Partial derivative

Definition Let f(w,b)f(w, b) be defined near the point (w0,b0)(w_0, b_0). The partial derivative of ff with respect to ww at that point is the ordinary derivative at w0w_0 of the one-variable function g(w)=f(w,b0)g(w) = f(w, b_0), in which bb is held at b0b_0,

∂f∂w(w0,b0)=g′(w0),\frac{\partial f}{\partial w}(w_0, b_0) = g'(w_0) ,

so the steps hh in the limit defining g′(w0)g'(w_0) change ww alone. The partial derivative with respect to bb holds ww fixed in the same way. With more parameters, one coordinate moves and all the others stay fixed. In a training loss the data are held fixed as well.

In plain words Slice the loss surface along one coordinate axis and take the slope of the slice. Each partial derivative answers what happens when only one parameter moves while everything else stays put.

Example Fix one training example with feature x=3x = 3 and target y=8y = 8. The model predicts y^=wx+b\hat{y} = wx + b, the residual on this page is prediction minus target, r=y^−yr = \hat{y} - y, and the loss is L(w,b)=12r2L(w, b) = \tfrac12 r^2. The factor 12\tfrac12 only simplifies the derivatives. At w=2w = 2 and b=1b = 1 the prediction is 7, the residual is −1-1 and the loss is 0.50.5. Holding b=1b = 1 gives the slice L(w,1)=12(3w−7)2L(w, 1) = \tfrac12 (3w - 7)^2, whose derivative 3(3w−7)3(3w - 7) equals −3-3 at w=2w = 2. Holding w=2w = 2 gives the slice L(2,b)=12(b−2)2L(2, b) = \tfrac12 (b - 2)^2, whose derivative b−2b - 2 equals −1-1 at b=1b = 1. So at this point ∂L/∂w=−3\partial L / \partial w = -3 and ∂L/∂b=−1\partial L / \partial b = -1.

Gradient

Definition For a real function ff of a parameter vector θ=(θ1,…,θk)⊤\boldsymbol{\theta} = (\theta_1, \dots, \theta_k)^{\top}, the gradient ∇f(θ)\nabla f(\boldsymbol{\theta}) is the vector of partial derivatives ∂f/∂θj\partial f / \partial \theta_j in the same order as the parameters. On this page it is a column with the same shape as θ\boldsymbol{\theta}, so for θ=(w,b)⊤\boldsymbol{\theta} = (w, b)^{\top}

∇L=[∂L/∂w∂L/∂b].\nabla L = \begin{bmatrix} \partial L / \partial w \\ \partial L / \partial b \end{bmatrix} .

The function ff is differentiable at θ\boldsymbol{\theta} if its partial derivatives exist there and

f(θ+Δ)−f(θ)=∇f(θ)⊤Δ+e(Δ)\begin{aligned} & f(\boldsymbol{\theta} + \boldsymbol{\Delta}) - f(\boldsymbol{\theta}) \\[0.4em] &\quad = \nabla f(\boldsymbol{\theta})^{\top}\boldsymbol{\Delta} + e(\boldsymbol{\Delta}) \end{aligned}

with an error term satisfying e(Δ)/∥Δ∥2→0e(\boldsymbol{\Delta}) / \lVert \boldsymbol{\Delta} \rVert_2 \to 0 as Δ→0\boldsymbol{\Delta} \to \mathbf{0}. Having all partial derivatives is not enough. The function equal to wb/(w2+b2)wb / (w^2 + b^2) away from the origin, with f(0,0)=0f(0, 0) = 0, has both partial derivatives equal to 00 at the origin, yet it equals 12\tfrac12 at every point (t,t)(t, t) with t≠0t \ne 0, so it is not even continuous there. If the partial derivatives exist and are continuous on an open set containing θ\boldsymbol{\theta}, then ff is differentiable at θ\boldsymbol{\theta}. Polynomials, such as the loss on this page, are differentiable everywhere.

In plain words The gradient packs the one-at-a-time slopes into one vector. For a differentiable function, a small step that moves several parameters at once changes the value by about the dot product of the gradient with the step. The approximation is local, and curvature makes it less accurate for larger steps.

Example At w=2w = 2 and b=1b = 1 the gradient is ∇L=(−3,−1)⊤\nabla L = (-3, -1)^{\top}. For the step Δ=(0.01,0.01)⊤\boldsymbol{\Delta} = (0.01, 0.01)^{\top} it predicts a change of −3(0.01)−1(0.01)=−0.04-3(0.01) - 1(0.01) = -0.04. The actual new prediction is 2.01⋅3+1.01=7.042.01 \cdot 3 + 1.01 = 7.04, the residual is −0.96-0.96 and the loss is 0.46080.4608, a change of −0.0392-0.0392 from 0.50.5. The gap of 0.00080.0008 is the curvature term 12(0.04)2\tfrac12 (0.04)^2, which shrinks like the square of the step.

Chain rule

Definition If gg is differentiable at tt and ff is differentiable at g(t)g(t), then the composition f∘gf \circ g is differentiable at tt and

(f∘g)′(t)=f′(g(t)) g′(t).(f \circ g)'(t) = f'(g(t))\, g'(t) .

For several variables, suppose intermediate quantities u1,…,umu_1, \dots, u_m are differentiable functions of θ\boldsymbol{\theta} and LL is a differentiable function of u1,…,umu_1, \dots, u_m. Then

∂L∂θj=∑k=1m∂L∂uk ∂uk∂θj.\frac{\partial L}{\partial \theta_j} = \sum_{k=1}^{m} \frac{\partial L}{\partial u_k}\, \frac{\partial u_k}{\partial \theta_j} .

A computational graph draws each quantity as a node and each direct dependency as an edge labeled with its local derivative. The derivative of the output with respect to a parameter is the sum, over all paths from the parameter to the output, of the product of the local derivatives along each path.

In plain words Rates multiply along a chain of dependencies, and contributions from separate routes add. Dropping one factor, such as the derivative of the prediction with respect to the weight, gives a wrong gradient even when every other step is right.

Example The graph for one example multiplies ww by xx, adds bb to give y^\hat{y}, subtracts yy to give rr, and squares and halves rr to give LL. The local derivatives are ∂L/∂r=r\partial L / \partial r = r, ∂r/∂y^=1\partial r / \partial \hat{y} = 1, ∂y^/∂w=x\partial \hat{y} / \partial w = x and ∂y^/∂b=1\partial \hat{y} / \partial b = 1, so

∂L∂w=∂L∂r ∂r∂y^ ∂y^∂w=rx,∂L∂b=∂L∂r ∂r∂y^ ∂y^∂b=r.\begin{aligned} \frac{\partial L}{\partial w} &= \frac{\partial L}{\partial r}\,\frac{\partial r}{\partial \hat{y}}\,\frac{\partial \hat{y}}{\partial w} = r x, \\[0.4em] \frac{\partial L}{\partial b} &= \frac{\partial L}{\partial r}\,\frac{\partial r}{\partial \hat{y}}\,\frac{\partial \hat{y}}{\partial b} = r . \end{aligned}

With x=3x = 3 and r=−1r = -1 this gives (−3,−1)⊤(-3, -1)^{\top}, the gradient found from the slices. Expanding the loss as 12(3w+b−8)2\tfrac12 (3w + b - 8)^2 gives the same result, with the inner factor 3 for ww and 1 for bb. Forgetting the inner factor would make the two derivatives equal, which is wrong. With two training examples and total loss 12(r12+r22)\tfrac12 (r_1^2 + r_2^2), the weight reaches the loss through both predictions, and the two paths add to ∂L/∂w=r1x1+r2x2\partial L / \partial w = r_1 x_1 + r_2 x_2.

Directional derivative

Definition The directional derivative of ff at θ\boldsymbol{\theta} along a vector v\mathbf{v} is the rate of change of ff along the line θ+tv\boldsymbol{\theta} + t\mathbf{v} at t=0t = 0,

Dvf(θ)=lim⁡t→0φ(t)−φ(0)t,D_{\mathbf{v}} f(\boldsymbol{\theta}) = \lim_{t \to 0} \frac{\varphi(t) - \varphi(0)}{t},

where φ(t)=f(θ+tv)\varphi(t) = f(\boldsymbol{\theta} + t\mathbf{v}). If ff is differentiable at θ\boldsymbol{\theta}, the limit exists for every v\mathbf{v} and

Dvf(θ)=∇f(θ)⊤v.D_{\mathbf{v}} f(\boldsymbol{\theta}) = \nabla f(\boldsymbol{\theta})^{\top}\mathbf{v} .

For a unit vector, with ∥v∥2=1\lVert \mathbf{v} \rVert_2 = 1, this is the rate of change per unit of Euclidean distance in parameter space. For other vectors it is the rate per unit of tt. The partial derivative with respect to θj\theta_j is the directional derivative along the coordinate vector ej\mathbf{e}_j. When ∇f(θ)≠0\nabla f(\boldsymbol{\theta}) \ne \mathbf{0}, the Cauchy-Schwarz inequality shows that among unit vectors the rate is largest for v=∇f/∥∇f∥2\mathbf{v} = \nabla f / \lVert \nabla f \rVert_2 and smallest, equal to −∥∇f∥2-\lVert \nabla f \rVert_2, for the opposite vector. These steepest directions depend on the chosen coordinates and their scales.

In plain words Pick a straight path through parameter space and ask how fast the function changes as you set off along it. A step that moves several parameters at once mixes their partial derivatives, so it measures something different from any single one of them.

Example At w=2w = 2 and b=1b = 1, with gradient (−3,−1)⊤(-3, -1)^{\top}, the unit vector v=(3/5,4/5)⊤\mathbf{v} = (3/5, 4/5)^{\top} gives

DvL=−3⋅35−1⋅45=−135=−2.6.\begin{aligned} D_{\mathbf{v}} L &= -3 \cdot \tfrac35 - 1 \cdot \tfrac45 \\[0.4em] &= -\tfrac{13}{5} = -2.6 . \end{aligned}

The direction (1,1)⊤(1, 1)^{\top}, which raises both parameters together, has rate −4-4 per unit of tt, or −4/2≈−2.83-4/\sqrt{2} \approx -2.83 per unit distance. Neither number equals the partial derivative −3-3, which is the rate along (1,0)⊤(1, 0)^{\top}. No unit direction decreases the loss faster than at the rate −10≈−3.16-\sqrt{10} \approx -3.16, reached along (3,1)⊤/10(3, 1)^{\top}/\sqrt{10}, the direction of −∇L-\nabla L.

Finite-difference approximation

Definition For a step h>0h > 0, the forward difference and the central difference of ff at tt are

f(t+h)−f(t)h,f(t+h)−f(t−h)2h.\begin{gathered} \frac{f(t + h) - f(t)}{h}, \\[0.4em] \frac{f(t + h) - f(t - h)}{2h} . \end{gathered}

For a partial derivative, only the chosen coordinate is stepped and the others are held fixed. If ff has a continuous second derivative on [t,t+h][t, t + h], the forward difference differs from f′(t)f'(t) by 12f′′(ξ) h\tfrac12 f''(\xi)\,h for some ξ\xi in that interval. If ff has a continuous third derivative on [t−h,t+h][t - h, t + h], the central difference differs from f′(t)f'(t) by 16f′′′(ξ) h2\tfrac16 f'''(\xi)\,h^2 for some ξ\xi in that interval. In floating-point arithmetic a very small hh makes the two function values nearly equal, and their difference loses significant digits to rounding. Agreement between a finite difference and a formula is evidence that the formula or code is right at the tested points. It is not a proof, and it cannot show that a function is differentiable.

In plain words Nudge one parameter, measure how much the loss moves and divide by the nudge. It needs no derivative formula, which makes it a good independent test of a hand-derived or coded gradient, but it costs extra loss evaluations for every parameter. Too large a step picks up curvature and too small a step loses digits to rounding, so try a few step sizes and compare relative as well as absolute differences.

Example Hold b=1b = 1 and step ww around 22 with h=0.0001h = 0.0001. The loss values are L(2.0001,1)=0.499700045L(2.0001, 1) = 0.499700045 and L(1.9999,1)=0.500300045L(1.9999, 1) = 0.500300045, so the central difference is −0.0006/0.0002=−3-0.0006 / 0.0002 = -3, exact up to rounding because the loss is quadratic in ww. The forward difference is (0.499700045−0.5)/0.0001=−2.99955(0.499700045 - 0.5) / 0.0001 = -2.99955. Its error 0.000450.00045 is exactly 12⋅9⋅0.0001\tfrac12 \cdot 9 \cdot 0.0001, since the second derivative in ww is x2=9x^2 = 9. With h=10−12h = 10^{-12} rounding dominates instead, and in double-precision arithmetic both differences are off by about 0.00030.0003.

Check yourself

  1. Take a new example with x=2x = 2 and y=5y = 5, the same loss L=12r2L = \tfrac12 r^2 with r=y^−yr = \hat{y} - y, and the parameters w=1w = 1 and b=1b = 1. Find the prediction, the residual, the loss and the gradient, naming each chain-rule factor. Then check ∂L/∂w\partial L / \partial w with a forward difference with step h=0.001h = 0.001, holding bb fixed.
Show the answer

The prediction is y^=1⋅2+1=3\hat{y} = 1 \cdot 2 + 1 = 3, the residual is r=3−5=−2r = 3 - 5 = -2 and the loss is 12⋅4=2\tfrac12 \cdot 4 = 2. The factors are ∂L/∂r=r=−2\partial L / \partial r = r = -2, ∂r/∂y^=1\partial r / \partial \hat{y} = 1, ∂y^/∂w=x=2\partial \hat{y} / \partial w = x = 2 and ∂y^/∂b=1\partial \hat{y} / \partial b = 1. So ∂L/∂w=(−2)(1)(2)=−4\partial L / \partial w = (-2)(1)(2) = -4 and ∂L/∂b=(−2)(1)(1)=−2\partial L / \partial b = (-2)(1)(1) = -2, and the gradient in the order (w,b)(w, b) is (−4,−2)⊤(-4, -2)^{\top}.

At w=1.001w = 1.001 the loss is 12(−1.998)2=1.996002\tfrac12 (-1.998)^2 = 1.996002, so the forward difference is (1.996002−2)/0.001=−3.998(1.996002 - 2) / 0.001 = -3.998. It differs from −4-4 by 0.0020.002, which is the curvature term 12x2h\tfrac12 x^2 h, and a central difference with the same step gives −4-4 up to rounding. The agreement supports the derivation at this point but does not prove it.

  1. For the same example, find the rate of change of the loss along the path that raises ww by tt and lowers bb by tt. Is it the partial derivative with respect to ww?
Show the answer

The direction is v=(1,−1)⊤\mathbf{v} = (1, -1)^{\top}, and the directional derivative is ∇L⊤v=−4⋅1+(−2)(−1)=−2\nabla L^{\top}\mathbf{v} = -4 \cdot 1 + (-2)(-1) = -2 per unit of tt. A direct calculation agrees. Along the path the prediction is 2(1+t)+(1−t)=3+t2(1 + t) + (1 - t) = 3 + t, so the residual is t−2t - 2, the loss is 12(t−2)2\tfrac12 (t - 2)^2 and its derivative at t=0t = 0 is −2-2.

It is not the partial derivative with respect to ww, which is −4-4 and moves only ww. On this path the drop in bb cancels part of the effect of raising ww, so the prediction rises by tt instead of 2t2t. Because v\mathbf{v} has length 2\sqrt{2}, the rate per unit distance is −2/2=−2≈−1.41-2/\sqrt{2} = -\sqrt{2} \approx -1.41.

  1. Someone checks the derivative of f(r)=∣r∣f(r) = \lvert r \rvert at r=0r = 0 with a central difference and gets 00 for every step hh. They conclude that f′(0)=0f'(0) = 0. What is wrong?
Show the answer

The central difference is (∣h∣−∣−h∣)/(2h)=0(\lvert h \rvert - \lvert -h \rvert) / (2h) = 0 for every h>0h > 0, but the derivative does not exist at 00. The forward difference ∣h∣/h=1\lvert h \rvert / h = 1 and the backward difference (0−∣−h∣)/h=−1(0 - \lvert -h \rvert) / h = -1 show the two one-sided limits, and they disagree. The central difference is the average of these two, so it returns a tidy number at a corner where no derivative exists.

A finite difference cannot establish differentiability. If an algorithm needs a value at the corner, it has to state a convention, such as a subgradient in [−1,1][-1, 1]. The value 00 is one valid subgradient, not the derivative.

How this lesson connects