Loading content...
One of the most powerful tools in differential calculus is the chain rule, which enables us to compute derivatives of composite functions—functions built by nesting one function inside another. This principle is absolutely fundamental in machine learning, particularly in the backpropagation algorithm that trains neural networks.
When we have a composite function h(x) = f(g(x)), where an outer function f is applied to the result of an inner function g, the chain rule tells us how the rate of change propagates through this composition:
$$h'(x) = f'(g(x)) \cdot g'(x)$$
In words: the derivative of the composite function equals the derivative of the outer function evaluated at the inner function, multiplied by the derivative of the inner function.
Extending to Multiple Compositions: For a chain of three or more functions, such as h(x) = f(g(k(x))), the chain rule extends naturally by multiplying the derivatives at each stage:
$$h'(x) = f'(g(k(x))) \cdot g'(k(x)) \cdot k'(x)$$
Each derivative in the product is evaluated at the accumulated value of all functions to its right.
Available Elementary Functions: Your implementation should support the following base functions and their derivatives:
| Function | f(x) | f'(x) |
|---|---|---|
| square | x² | 2x |
| sin | sin(x) | cos(x) |
| exp | eˣ | eˣ |
| log | ln(x) | 1/x |
Your Task: Write a Python function that computes the derivative of a composite function at a specified point. The function receives a list of function names (applied from right to left, i.e., the rightmost function is the innermost) and a value x, and returns the derivative evaluated at that point.
functions = ['sin', 'square'], x = 1.01.080605The composite function is h(x) = sin(x²), where 'square' is applied first (innermost), then 'sin'.
Step 1: Identify the layers • Inner function: g(x) = x² • Outer function: f(u) = sin(u)
Step 2: Compute values at x = 1 • g(1) = 1² = 1 • g'(x) = 2x, so g'(1) = 2 • f'(u) = cos(u), so f'(g(1)) = cos(1) ≈ 0.540302
Step 3: Apply chain rule • h'(1) = f'(g(1)) · g'(1) = cos(1) · 2 ≈ 1.080605
functions = ['square'], x = 2.04.0With a single function, no chain rule application is needed—we simply compute the derivative directly.
Function: h(x) = x² Derivative: h'(x) = 2x At x = 2: h'(2) = 2 · 2 = 4.0
functions = ['exp', 'sin', 'square'], x = 0.51.240883The composite function is h(x) = exp(sin(x²)), with three nested layers.
Step 1: Identify the layers (inside out) • Innermost: k(x) = x² → k'(x) = 2x • Middle: g(u) = sin(u) → g'(u) = cos(u) • Outermost: f(v) = exp(v) → f'(v) = exp(v)
Step 2: Compute forward pass values at x = 0.5 • k(0.5) = 0.25 • g(k(0.5)) = sin(0.25) ≈ 0.247404 • f(g(k(0.5))) = exp(0.247404) ≈ 1.280671
Step 3: Compute derivatives and apply chain rule • k'(0.5) = 2 · 0.5 = 1.0 • g'(k(0.5)) = cos(0.25) ≈ 0.968912 • f'(g(k(0.5))) = exp(0.247404) ≈ 1.280671
Step 4: Multiply all derivatives • h'(0.5) = 1.280671 · 0.968912 · 1.0 ≈ 1.240883
Constraints