Runoob Tutorial ArchiveEnglish

Manually implement the chain rule

Manually implement the chain rule

Construct composite functions, manually calculate local gradients layer by layer and multiply them, then verify with sympy.

After learning this case study, you will understand: the core of backpropagation is the chain rule—each layer only calculates its local gradient, and the final gradient is the product of the gradients of each layer.


Real-life introduction

**The production line of dominoes

A factory has three processes: raw materials -> semi-finished product A -> semi-finished product B -> finished product. You want to know "if 1 kg more raw material is used, how many more finished products will there be?"

You do not need to derive the formula for the entire process from scratch. Each step only calculates its own "local proportion": If semi-finished product A increases by 1 → how many more semi-finished product B? If semi-finished product B increases by 1 → how many more finished products? Multiplying these three proportions gives the answer.

This is the chain rule—each layer only cares about itself, and the final impact = the product of the impacts of each layer.


Intuitive understanding

Construct a three-layer composite function:

x

u = g(x)

x^2+1

v = f(u)

3*sin(u)

y = h(v)

v^2

[ \frac{dy}{dx} = \underbrace{\frac{dy}{dv}}{h'(v)} \cdot \underbrace{\frac{dv}{du}}{f'(u)} \cdot \underbrace{\frac{du}{dx}}_{g'(x)} ]


Python Hands-on Practice

Example

import

sympy

as

sp

import

numpy

as

np

def

g

(

x

)

:

return

x **

2

1

def

f

(

u

)

:

return

3

sin

(

u

)

def

g_prime

(

x

)

:

return

2

def

f_prime

(

u

)

:

return

3

cos

(

u

)

x0

=

1.5

---- Manual Chain Rule ----

u0

=

g

(

x0

)

dy_du

=

f_prime

(

u0

)

   

Second layer local gradient

du_dx

=

g_prime

(

x0

)

   

First layer local gradient

manual_grad

=

dy_du * du_dx

print

(

"="

55

)

print

(

"RUNOOB Manual Chain Rule"

)

print

(

f

"x0 = {x0}, u = g(x0) = {u0:.4f}"

)

print

(

f

"dy/du = 3*cos(u) = {dy_du:.4f}"

)

print

(

f

"du/dx = 2*x0 = {du_dx:.4f}"

)

print

(

f

"Chain multiplication: dy/dx = {manual_grad:.4f}"

)

---- sympy automatic differentiation (standard answer) ----

x

=

sp.

Symbol

(

'x'

)

y_expr

=

3

sin

(

x**

2

1

)

auto_grad

=

float

(

sp.

diff

(

y_expr

,

x

)

.

subs

(

x

,

x0

)

)

print

(

f

"\nRUNOOB sympy automatic differentiation = {auto_grad:.4f}"

)

print

(

f

"Both methods are consistent: {np.isclose(manual_grad, auto_grad)}"

)

---- Extension: Three-layer composite ----

def

h

(

v

)

:

return

v **

2

def

h_prime

(

v

)

:

return

2

v0

=

f

(

u0

)

dy_dv

=

h_prime

(

v0

)

grad_3layer

=

dy_dv * dy_du * du_dx

print

(

f

"\nRUNOOB three-layer gradient propagation:"

)

print

(

f

" 3rd layer: {dy_dv:.4f} x 2nd layer: {dy_du:.4f} x 1st layer: {du_dx:.4f} = {grad_3layer:.4f}"

)

Original prompt (Chinese, left untranslated)



=======================================================

RUNOOB 手动链式法则

x0 = 1.5, u = g(x0) = 3.2500

dy/du = 3*cos(u) = -2.9838

du/dx = 2*x0     = 3.0000

链式相乘: dy/dx = -8.9514



RUNOOB sympy 自动求导 = -8.9514

两种方法一致: True



RUNOOB Three-layer gradient propagation:

  第3层: 6.5000 x 第2层: -2.9838 x 第1层: 3.0000 = -58.1841


Application scenarios in AI

Scenario Relationship with the chain rule
Backpropagation Each layer in a neural network only calculates its local gradient and then passes it to the previous layer
PyTorch autograd Automatically tracks each operation to build a computational graph, and automatically applies the chain rule during backpropagation
Gradient checking After manually implementing backpropagation, verify with numerical gradients—this is Method One vs Method Two in this case