1 of 54

Conv. + Momentum + Adam +�Stochastic Gradient Descent

Lecture 12

The core algorithm of modern machine learning

Action Packed

Lecture

EECS 189/289, Fall 2025 @ UC Berkeley

Joseph E. Gonzalez and Narges Norouzi

EECS 189/289, Fall 2025 @ UC Berkeley

Joseph E. Gonzalez and Narges Norouzi

2 of 54

Join at slido.com�#3389863

The Slido app must be installed on every computer you’re presenting from

3389863

3 of 54

The (Batch) Gradient Descent Alg.

  •  

 

3389863

4 of 54

What are the conditions for convergence?

Learning rate seemed to affect convergence even on a simple parabola.

3389863

5 of 54

Convergence Assuming�Quadratic Approximation

6 of 54

Quadratic Approximations

  •  

3389863

7 of 54

Taylor Expansion of the Error Surface

  •  

Matrix Equivalent of the Second Derivative

3389863

8 of 54

Hessians (the 2nd derivative)

  •  

3389863

9 of 54

Hessian Exercise

  •  

Plots in demo notebook.

Hessian

 

 

 

3389863

3389863

10 of 54

Taylor Expansion of the Error Surface

  •  

Use a change of variables to better understand this part.

3389863

11 of 54

Eigen Decomposition of the Hessian

  •  

Show it!

3389863

12 of 54

Eigen Decomposition of the Hessian

  •  

Show it!

 

Derivation:

 

 

 

 

 

 

Eigenvector Defn.

Orthonormality

3389863

13 of 54

Taylor Expansion at the Stationary Pts.

  •  

3389863

14 of 54

If the eigen values at w* are all positive then

The Slido app must be installed on every computer you’re presenting from

3389863

15 of 54

Taylor Expansion at the Stationary Pts.

  •  

3389863

16 of 54

Stationary Points and the Hessian

Eigenvalues of the Hessian determine the curvature �at the critical points

Minimum

Maximum

Saddle Point

3389863

17 of 54

The eigenvector u1 corresponds to the:

The Slido app must be installed on every computer you’re presenting from

3389863

18 of 54

Eigenvectors of the Hessian

Elliptical contours of constant error align with the eigenvectors of the Hessian matrix.

Plots in demo notebook.

 

 

 

Direction with Smallest Eigenvalue

Direction with Largest Eigenvalue

3389863

19 of 54

Demo

Taylor Expansions and the Hessian

3389863

3389863

20 of 54

Recap: Quadratic Approx. Error

  •  

 

 

 

 

3389863

21 of 54

Grad. Descent on the Quadratic Approx.

  •  

Quadratic Approx. Error

 

 

3389863

22 of 54

Grad. Descent on the Quadratic Approx.

  •  

 

3389863

23 of 54

Learning Rate Impact on Convergence

  •  

 

 

3389863

24 of 54

Condition Number and Loss Surface

  •  

 

 

 

Direction with Smallest Eigenvalue

Direction with Largest Eigenvalue

 

 

 

 

3389863

25 of 54

Issues with Gradient Descent (GD)

GD converges slowly when the error surface is relatively flat:

GD oscillates when the error surface is poorly conditioned

 

 

3389863

26 of 54

Demo

Oscillating convergence

3389863

27 of 54

Momentum

  •  

3389863

28 of 54

How Momentum Helps with Flat Dir.

  •  

Geometric Series

3389863

29 of 54

How Momentum Helps with Oscillations

  •  

Alternating Geometric Series

3389863

30 of 54

Demo

Momentum

3389863

31 of 54

Learning Rate Schedules

  •  

3389863

32 of 54

Demo

Learning Rate Schedules

3389863

33 of 54

Adaptive Learning Rates – AdaGrad

  •  

*Developed at UC Berkeley

Tracking Magnitude

(terms squared)

Scaled Learning Rate

Issue:

Large initial gradients result in overly small learning rates near convergence.

3389863

34 of 54

Adaptive Learning Rates – RMSProp

  •  

AdaGrad

RMSProp

3389863

35 of 54

Adam = RMSProp + Momentum

  •  

RMSProp

Momentum

Gradient

Update

 

3389863

36 of 54

Demo

Adam

3389863

37 of 54

Recap: Batch Gradient Descent

  •  

 

3389863

38 of 54

Stochastic Gradient Descent

Approximating Gradient Descent

39 of 54

The Cost of Batch Gradient Descent

  •  

 

3389863

40 of 54

What is the cost for a single gradient update assuming N data points in D dimensions and the basic gradient update equation.

The Slido app must be installed on every computer you’re presenting from

3389863

41 of 54

The Cost of Batch Gradient Descent

  •  

 

 

 

3389863

42 of 54

The Training Error Estimates the Test Error

  •  

3389863

43 of 54

Approximating the Gradient

  •  

 

Best estimate (but expensive)

Cheap approx.

3389863

44 of 54

Stochastic Gradient Descent

  •  

 

 

3389863

45 of 54

Stochastic Gradient Descent (Shuffling)

In practice, we often shuffle and then iterate over the entire dataset

 

 

One epoch is a complete pass over all the data points.

Often only randomize data once.

3389863

46 of 54

Demo

Stochastic Gradient Descent

3389863

47 of 54

A Better Gradient Approximation

Estimating the gradient using a single data point results in noisy estimates.

?

Noisy but Cheap

Accurate but Expensive

 

 

 

 

Stochastic �Gradient Descent

Batch �Gradient Descent

 

Mini-Batch Stochastic�Gradient Descent

 

3389863

48 of 54

A Spectrum of Stochastic Gradients

We typically refer to any of the sampling based gradient estimation procedures as Stochastic Gradient Descent (SGD)

Noisy but Cheap

Accurate but Expensive

 

 

 

Stochastic �Gradient Descent

 

Batch �Gradient Descent

 

Mini-Batch Stochastic�Gradient Descent

Stochastic Gradient Methods

 

 

 

3389863

49 of 54

Mini-batch Stochastic Gradient Descent

  •  

 

 

3389863

50 of 54

Shuffling for Minibatch Gradient Descent

In practice, we often shuffle and then iterate over the entire dataset

 

 

One epoch is a complete pass over all the data points.

Often only randomize data once.

3389863

51 of 54

If I have N data points and a choose a batchsize B=2. In one epoch how many gradient steps will I take?

The Slido app must be installed on every computer you’re presenting from

3389863

52 of 54

How do I Pick the Batch Size?

Larger batch size:

  • Better gradient estimate
  • Enables hardware parallelism – can compute gradient for each point in parallel

Smaller batch size:

  • Cheap to compute 🡪 take more steps
  • Greater stochasticity can help with local minima

Typical design decision:

  • Increase batch size to saturate hardware parallelism
  • Increase learning rate proportional to batch size (take bigger steps)

3389863

53 of 54

Demo

Mini-batch Gradient Descent

3389863

54 of 54

Stochastic Gradient Descent

Lecture 13

Credit: Joseph E. Gonzalez and Narges Norouzi

Reference Book Chapters: Chapter 7