Runoob Tutorial ArchiveEnglish

Visualize the effect of different learning rates

Visualize the effect of different learning rates

Run gradient descent on the same function using too small, appropriate, and too large learning rates, and intuitively see the comparison of the three typical curves.

After completing this case study, you will understand: the learning rate is the most important hyperparameter in gradient descent—if you choose it too small, it's too slow; if you choose it too large, it diverges.


Real-life introduction

**Driving home—how deep to press the accelerator?

10 kilometers from home. Pressing the accelerator too lightly (learning rate too small)—it takes an hour to get there. Pressing the accelerator too heavily (learning rate too large)—you overshoot, turn around and overshoot again, oscillating back and forth. Just the right amount of force (appropriate learning rate)—you arrive steadily.

Choosing the learning rate for gradient descent and controlling the accelerator pedal in a car face the exact same problem.


Intuitive understanding

Objective function (f(x) = x^2), minimum at (x=0). Gradient is (f'(x) = 2x).

Update rule: (x_{t+1} = x_t - \eta \cdot 2x_t = x_t \cdot (1 - 2\eta)) . It converges if (\eta < 0.5) and diverges if (\eta > 1.0).

Too small lr=0.01

Convergence is very slow, only moved a short distance in 30 steps

Appropriate lr=0.2

Quickly and steadily converges to 0

Too large lr=1.05

Oscillating and diverging, getting farther and farther


Python hands-on practice

Example

import

numpy

as

np

def

f

(

x

)

:

return

x **

2

def

grad

(

x

)

:

return

2

def

run_gd

(

lr

,

steps

=

30

,

x0

=

8.0

)

:

x

=

x0

history

=

[

x

]

   

for

_

in

range

(

steps

)

:

x

=

x - lr * grad

(

x

)

history.

append

(

x

)

       

if

abs

(

x

)

1e6

:

break

   

return

history

settings

=

{

   

"Too small (lr=0.01)"

:

0.01

,

   

"Appropriate (lr=0.2)"

:

0.2

,

   

"Too large (lr=1.05)"

:

1.05

,

}

results

=

{

name: run_gd

(

lr

)

for

name

,

lr

in

settings.

items

(

)

}

print

(

"RUNOOB three learning rates comparison after 30 steps:\n"

)

print

(

f

"{'Learning Rate':<20} {'Final x':<12} {'f(x)':<14} {'Decision'}"

)

print

(

"-"

56

)

for

name

,

hist

in

results.

items

(

)

:

x_final

=

hist

[

1

]

judgement

=

(

"Converged"

if

abs

(

x_final

)

<

0.01

else

"Converging"

if

abs

(

x_final

)

<

3

else

"Divergent"

)

print

(

f

"{name:<20} {x_final:<12.4f} {f(x_final):<14.6f} {judgement}"

)

Analysis: How many steps are needed for |x| < 0.01?

print

(

"\nHow many steps does RUNOOB need to converge to |x| < 0.01?"

)

for

name

,

lr

in

settings.

items

(

)

:

x

=

8.0

   

for

step

in

range

(

1

,

1001

)

:

        x

=

x - lr * grad

(

x

)

       

if

abs

(

x

)

<

0.01

:

           

print

(

f

" {name}: Requires {step} steps"

)

           

break

   

else

:

       

print

(

f

" {name}: failed to converge within 1000 steps"

)

Original prompt (Chinese, left untranslated)



RUNOOB Comparison after 30 steps with three learning rates:



学习率设置             最终 x        f(x)           判断

--------------------------------------------------------

过小 (lr=0.01)        4.3219       18.678861      收敛中

合适 (lr=0.2)         0.0000       0.000000       已收敛

过大 (lr=1.05)        -34.5929     1196.668701    发散



RUNOOB 需要多少步收敛到 |x| < 0.01?

  过小 (lr=0.01): 需要 377 步

  合适 (lr=0.2):  需要 20 步

  过大 (lr=1.05): 1000 步内无法收敛


Application scenarios in AI

Scenario Description
Learning rate scheduling In actual training, "learning rate decay" is commonly used—large initially and decreasing later, fast convergence in the early stages, and fine-tuning in the later stages
Warmup When training Transformers, a very small learning rate is used for warmup first, and then gradually increased—to avoid initial oscillations
Adam adaptive Automatically adjusts the learning rate for each parameter—the "Optimizer Comparison" case will show this in detail