Differentiation Rules and the Chain Rule
Learning Objectives
- Apply the constant-multiple, sum, power, product and quotient rules, and state the domain on which each is valid.
- Derive the power, product and quotient rules from the limit definition of the derivative.
- State the chain rule with its hypotheses, and read it as the composition of two local linear approximations.
- Differentiate nested compositions layer by layer, and avoid the usual errors (forgetting the inner derivative, using ).
- Explain how the product and chain rules let a program compute exact derivatives automatically, and why the chain rule is what backpropagation applies.
Context & Motivation
The previous lesson, the-derivative-as-a-rate-of-change, defined as a limit and computed it for , and with a separate piece of algebra each time. That does not scale. A function such as would need a page of algebra under the limit sign, and a function defined by a program with thousands of operations is out of the question.
The way out is to differentiate a function by its structure. Almost every function in this course is built from a small supply of basic functions joined by arithmetic (sums, products, quotients) and by composition ( applied to the output of ). If the derivative of each joining operation is known, the derivative of the whole follows mechanically. The rules of this lesson are those operations' derivatives, the sum, power, product and quotient rules each proved from the definition once, and the chain rule justified by a linear-approximation argument, then used forever.
The chain rule, the rule for composition, is the one with the most reach. A deep neural network is a long composition of simple layers, and the derivative of its loss with respect to an early parameter is obtained by multiplying local derivatives layer by layer (in the multivariable form of the rule, partial-derivatives-and-the-multivariable-chain-rule). Organizing that product efficiently is what backpropagation does. This lesson only teaches the rule that makes it possible, not the algorithm.
Core Theory
Linearity and the power rule
If and are differentiable at and is a constant, then and . Both follow at once from the limit laws applied to the difference quotient. The derivative of a constant is .
Power rule. For a positive integer , . Expand with the binomial theorem:
Every term after the first still contains a factor of , so it vanishes in the limit. The rule extends to negative integers through the quotient rule below, valid for . For other real exponents it is proved in derivatives-of-exponentials-logarithms-and-trigonometric-functions, where it holds for . Until then, use it for integer exponents only.
The product rule
If and are differentiable at , then
Derivation: add and subtract in the numerator,
As the quotients become and , and because a differentiable function is continuous. That step is essential: the argument needs , and it holds because is differentiable at . The rule is not : with , the product is with derivative , while .
The quotient rule
If and are differentiable at and , then
First the reciprocal: , which uses and continuity of . Then and the product rule give the formula. The order in the numerator matters, because the difference is not symmetric. The condition is not enough on its own: with and at , the denominator is , yet has no derivative there, because does not.
The chain rule
Theorem. If is differentiable at and is differentiable at , then is differentiable at and
In Leibniz form, with and : .
Why it is true. Use the linear-approximation reading from the previous lesson. A small step in moves the inner output by about . A small step of size in moves the outer output by about . Chaining the two, with , gives a change of about in the final output. Composing two local linear maps multiplies their slopes. A line with slope followed by a line with slope is a line with slope .
The naive proof, multiplying and dividing by , divides by zero whenever takes the same value at nearby points. A complete proof rewrites the approximation as an equality with an error function that goes to . The statement above is the standard theorem.
The hypotheses matter, but neither is necessary for the conclusion. Let and , so . For the chain rule gives , which is as it should be. At the outer function is not differentiable at , so the theorem does not apply, and the conclusion fails: has a corner at . The inner hypothesis can fail the same way: with and , is not differentiable at and neither is . But failing a hypothesis only removes the guarantee. With and , is not differentiable at , yet is.
Layer by layer
For a composition of several functions, differentiate from the outside in, multiplying as you go. For :
A program evaluates a composition from the inside out and can multiply the local derivatives in the same pass. That observation is the basis of forward-mode automatic differentiation, shown in Example 3.
Worked Examples
Example 1: quotient rule, domain and tangent line
Differentiate .
The domain excludes , where the denominator vanishes. Applying the quotient rule:
At : and . A central difference with step gives , confirming the algebra. The tangent line is . It predicts , and the true value is . At it predicts while the true value is : the function is heading toward the vertical asymptote at , and the line cannot follow it.
Example 2: a three-layer chain
Differentiate .
Name the layers: , then , then . Multiply the local derivatives:
Here for every , so the outer square root is always evaluated at a point where it is differentiable, and exists on all of . At : , , so . A central difference gives .
Example 3: the chain rule inside a program
Forward-mode automatic differentiation carries a pair (value, derivative) through every operation. The product rule defines multiplication of pairs, and the chain rule defines how a function such as transforms them:
plaintextimport math class Dual: def __init__(self, v, d=0.0): self.v, self.d = v, d # value and derivative def __add__(self, o): o = o if isinstance(o, Dual) else Dual(o) return Dual(self.v + o.v, self.d + o.d) # sum rule __radd__ = __add__ def __sub__(self, o): o = o if isinstance(o, Dual) else Dual(o) return Dual(self.v - o.v, self.d - o.d) def __mul__(self, o): o = o if isinstance(o, Dual) else Dual(o) return Dual(self.v * o.v, self.d * o.v + self.v * o.d) # product rule __rmul__ = __mul__ def dsqrt(a): r = math.sqrt(a.v) return Dual(r, a.d / (2 * r)) # chain rule with outer derivative 1/(2 sqrt) def F(x): u = 3 * x * x - x return dsqrt(1 + u * u) out = F(Dual(1.0, 1.0)) # seed: dx/dx = 1 print(out.v, out.d) # 2.23606797749979 4.47213595499958
The program prints and , matching Example 2 without any symbolic algebra and without a step size, so the trade-off between truncation and cancellation error seen in the previous lesson's numerical differences does not arise. Only ordinary rounding remains.
Common Misconceptions & Pitfalls
- "The derivative of a product is the product of the derivatives." It is . The counter-example has derivative , not .
- "Differentiating gives ." That omits the inner derivative. The correct answer is , which at is , twice the wrong value .
- "The quotient rule numerator can be written in either order." It is . Swapping the two terms flips the sign of the result.
- "The power rule works for any exponent, including a variable one." holds when the exponent is a constant. For the variable is in the exponent and the rule gives the wrong answer, a case treated in the next lesson.
- "Rules apply anywhere the formula can be written down." They apply where the hypotheses hold. Take and at : the outer function is not differentiable at , and the chain-rule expression is undefined at , although it equals for . The theorem says nothing at here, and the composite happens to be differentiable there, so the value has to be found from the definition.
- "In a long chain, the local derivatives are harmless factors." Their product is the whole answer. Fifty factors of multiply to about , and fifty factors of to about . This is the arithmetic behind vanishing and exploding gradients in deep networks, and it is a consequence of the chain rule, not a flaw in an implementation.
Summary
Derivatives can be computed from structure: constants and sums by linearity, by for integer exponents, products by , and quotients by where and are differentiable and . The sum, power, product and quotient rules are proved once from the limit definition, and the chain rule is justified by a linear-approximation argument. The chain rule, , requires differentiable at and differentiable at , and says that slopes of composed local linear approximations multiply. A composition is differentiated from the outside in, multiplying a factor per layer. The same bookkeeping run by a program gives exact derivatives (up to rounding) without symbolic algebra or a step size, which is the principle behind automatic differentiation and, in deep learning, backpropagation. The next lesson, derivatives-of-exponentials-logarithms-and-trigonometric-functions, supplies the derivatives of the remaining basic functions, so that these rules reach almost every function in practice.