Runoob Tutorial ArchiveEnglish

Chain Rule — Deconstruction Method for Differentiating Composite Functions

Chain Rule -- Deconstruction Method for Differentiating Composite Functions

The Chain Rule is the mathematical soul of the backpropagation algorithm. Outer derivative × Inner derivative, propagated layer by layer.


Concept Analysis

Univariate Chain Rule

If y = g(x) and z = f(y), then:

[ \frac{dz}{dx} = \frac{dz}{dy} \cdot \frac{dy}{dx} ]

Backpropagation = Applying the Chain Rule in reverse along the computation graph

x

y=g(x)

∂y/∂x

z=f(y)

∂z/∂y

L

∂L/∂z

During backpropagation, the gradient is passed layer by layer from output to input: "local gradient" of each layer × "gradient passed from upstream" = the gradient of the layer's parameters with respect to the loss.


Life examples

**Throttle → Fuel consumption

Fuel consumption depends on vehicle speed, and vehicle speed depends on throttle depth.

"How much does fuel consumption increase by pressing the throttle a little more?" = (Rate of change of fuel consumption with respect to vehicle speed) × (Rate of change of vehicle speed with respect to throttle).

This is the chain rule: changes are propagated layer by layer and multiplied to get the total effect.


Python hands-on practice

Example

import

sympy

as

sp

x

=

sp.

Symbol

(

'x'

)

f(g(x)): sin(x^2), outer sin, inner x^2

d_direct

=

sp.

diff

(

sp.

sin

(

x**

2

)

,

x

)

d_chain

=

sp.

cos

(

x**

2

)

2

*x

print

(

f

"Directly differentiate: {d_direct}"

)

print

(

f

"Chain rule: cos(x^2)·2x = {d_chain}"

)

print

(

f

"Consistent: {sp.simplify(d_direct-d_chain)==0}"

)

Two-layer network backpropagation

w

,

xi

,

yi

=

sp.

symbols

(

'w x_i y_i'

)

a

=

sp.

Symbol

(

'a'

)

           

Define the intermediate variable as a symbol

a_expr

=

w * xi

Specific expression of a with respect to w

loss

=

(

a - yi

)

**

2

           

loss is defined as a function of symbol a

dL_da

=

sp.

diff

(

loss

,

a

)

                     

∂L/∂a

da_dw

=

sp.

diff

(

a_expr

,

w

)

                   

∂a/∂w

dL_dw_chain

=

dL_da.

subs

(

a

,

a_expr

)

Chain rule: substitute a=w*x_i and multiply

loss_direct

=

loss.

subs

(

a

,

a_expr

)

           

Expand loss completely into a function of w

dL_dw_direct

=

sp.

diff

(

loss_direct

,

w

)

print

(

f

"\nChain ∂L/∂w: {sp.simplify(dL_dw_chain)}"

)

print

(

f

"Direct ∂L/∂w: {sp.simplify(dL_dw_direct)}"

)

print

(

f

"Consistent: {sp.simplify(dL_dw_chain-dL_dw_direct)==0}"

)

Output:

Original prompt (Chinese, left untranslated)

直接求导: 2*x*cos(x**2)

链式法则: cos(x^2)·2x = 2*x*cos(x**2)

一致: True



链式 ∂L/∂w: 2*x_i*(w*x_i - y_i)

直接 ∂L/∂w: 2*x_i*(w*x_i - y_i)

一致: True


Application scenarios in AI

Backpropagation = systematic application of the chain rule on a computation graph

PyTorch's autograd engine automatically records the computation graph for every tensor operation. When calling loss.backward(), it traverses the computation graph backward starting from the loss node, applying the chain rule to each node: local gradient × upstream gradient = downstream gradient. Developers do not need to write any differentiation code manually.

Gradient Check

If you write a custom layer or loss function, how do you verify that the backpropagation gradient calculation is correct? Use numerical gradient ((f(x+h)-f(x-h))/2h) as the "ground truth" and compare it with the gradient calculated by autograd. The relative error should be less than 1e-5. This is the principle behind torch.autograd.gradcheck in PyTorch.

Computation graph optimization

Deep learning compilers (such as TensorRT, XLA) optimize the computation graph by merging adjacent operations (operator fusion) and reducing memory read/writes. However, it must be ensured that gradient calculation under the chain rule remains correct—this is an important constraint for graph optimization.