Skip to main content
Mathematical Foundations for Machine Learning

Gradient Descent Optimization

Published: 2026-07-11
Level: postgraduate
Audience: Postgraduate students in Machine Learning and related quantitative fields

Prerequisite Knowledge

This lecture builds on the following concepts from earlier lectures. If any feel unfamiliar, review the linked notes before proceeding.

Previously Covered in This Subject

  • Gradient Descent and the Update Rule — covered in Lecture 11 (section 11.1)
  • Learning Rate and Adaptive Methods Preview — covered in Lecture 11 (section 11.5)
  • The Gradient — Direction of Steepest Increase — covered in Lecture 9 (section 9.3)
  • The Hessian Matrix and Loss Surface Curvature — covered in Lecture 10 (section 10.5) and Lecture 12 (section 12.7)
  • Challenges of Gradient Descent and Lagrange's Method — covered in Lecture 12 (sections 12.2 and 12.4)

Gradient Descent Optimization

Gradient descent is the engine behind nearly every machine learning model. This lecture starts with the plain algorithm and shows exactly where it breaks. A single learning rate cannot serve a loss surface that is steep in one direction and flat in another. From there it builds the fixes in order . Momentum adds memory to smooth the path, Adagrad and RMSProp give each parameter its own learning rate, and Adam combines both ideas. The lecture closes with constrained optimization and the Lagrangian method. Which folds hard rules into the objective so the same machinery still applies. Worked examples with real numbers run through every optimizer, and the exam guidance distills what to memorize.

13.1 Problems with Gradient Descent

13.1.1 Definition and Explanation

Hook.You are a blindfolded hiker standing on a mountain. Your only tool is a sensor under your boots that tells you which direction the ground slopes steepest. You take a step downhill, check again, take another step. Can you guarantee reaching the valley floor? If the mountain is a smooth bowl — yes. But real mountains have flat plateaus where your sensor reads zero. And knife-edge ravines where one bad step sends you flying across to the other wall. This is gradient descent in the real world.

Gradient descent is an iterative optimization algorithm. You start at some point on the loss surface. You take steps proportional to the negative of the gradient. The goal is to reach a minimum. But gradient descent has fundamental failure modes when the loss surface has certain shapes.

Intuition + Analogy.Picture a long, narrow canyon. The walls are steep cliffs. The canyon floor slopes gently toward the exit, but it is nearly flat. Now imagine three hikers, each taking a different fixed step size.

  • Hiker Atakes tiny, cautious steps. On the cliffs, she makes slow but steady progress downward. But once she reaches the flat canyon floor, her steps become so small that she barely moves. She stalls out, thinking she has arrived. She hasn't — the exit is still ahead.
  • Hiker Btakes moderate strides. On the cliffs, each stride sends her crashing into the opposite wall. She bounces from wall to wall — zigzagging — making slow, inefficient progress along the canyon.
  • Hiker Ctakes huge leaps. On the cliffs, she bounds wildly from one wall to the other and back again. The bouncing cancels out. She is stuck in place, trapped. If she leaps even harder, she flies out of the canyon entirely.

This is exactly what happens with gradient descent when the loss surface has different steepness in different directions. The canyon walls represent the steep dimension of the loss surface. The canyon floor represents the flat dimension. Each hiker's stride length represents the learning rate . No single fixed stride works for both the steep walls and the flat floor.

The analogy breaks at one point: real loss surfaces have hundreds or millions of dimensions. You cannot visualize them. But the math works the same way — each dimension can have its own steepness. And a single learning rate must serve them all.

Aloss functionmaps your model parameters to a single number — how wrong your model is. The shape of this loss surface determines whether gradient descent will succeed or fail. A well-behaved loss surface is smooth and bowl-shaped. You can start at any point, slide down, and reach the bottom. But real loss surfaces are rarely that nice.

The two root problems come from thecurvatureof the loss surface. The curvature describes how steep the surface is in different directions. If the surface is highly curved — steep in one direction and flat in another — gradient descent struggles. The companion document calls these problematic featurescliffs(where slope changes dramatically from flat to steep) andvalleys/ravines(long narrow depressions with steep sides and a gently sloping floor). With a single fixed learning rate, you either crawl forever on the flat parts or bounce uncontrollably on the steep parts.

13.1.2 Symbol Registry

Symbol Meaning Type
Weight vector at step vector in
Learning rate — step size scalar, typical range
Gradient of the loss at vector in
Position coordinates on the loss surface scalar
Loss function value at scalar

13.1.3 Three Failure Modes

The gradient descent update rule.At each step , you compute the gradient . The vector of partial derivatives pointing in the direction of steepest ascent — and move in the opposite direction.

where.

  • is the weight vector (your current position on the loss surface),
  • is the learning rate (how big each step is),
  • is the gradient of the loss function at .

The negative sign is crucial: points uphill (steepest ascent), so points downhill. The learning rate scales the step. If is wrong for the terrain, the algorithm fails.

Problem 1: Vanishing updates on flat regions.When you have a small learning rate, each step is tiny. You descend slowly. Once you enter a flat region, the gradient becomes nearly zero. Since the update is , when , we get . You sit there, barely moving. The minimum is further ahead, but the algorithm thinks it has arrived because the gradient says "you are on flat ground." You never reach the true minimum — your steps have died out.

Problem 2: Zigzag oscillation.When you increase the learning rate to fix Problem 1, a new problem appears. In directions where the surface is steep. A large step in the direction shoots you across to the other side of the valley. Then the gradient points back the other way. You shoot across again. This creates a zigzag pattern — you oscillate back and forth across the valley floor. You are still making progress along the valley, but very inefficiently. Most of each step's movement cancels the previous step's side-to-side motion.

Problem 3: Getting trapped in the valley.In extreme cases, the oscillation becomes balanced. You jump from one wall of the valley to the other and back again. The updates exactly cancel each other out. You are stuck, bouncing in place. The loss does not decrease. This happens when the learning rate is high enough to overshoot the valley bottom. But not high enough to escape the valley entirely.

Problem 3b (bonus): Overshooting the trajectory.If the learning rate is pushed even higher, you can leave the relevant region of the loss surface entirely. The updates become so large that you fly off in some direction, never to return to the valley. The algorithm diverges completely — the loss grows without bound instead of shrinking.

The fundamental tension.The same learning rate must serve all dimensions of the parameter space. In a dimension with large curvature (steep), a large causes oscillation. In a dimension with small curvature (flat), a small causes stalling. There is no single that works for both. This is the core problem that momentum, Adagrad, RMSProp, and Adam were each invented to solve.

13.1.4 Worked Visualization

Consider a specific loss function designed to illustrate all three failure modes.

Look at the coefficients. The term has coefficient 0.01. The term has coefficient 1.5. The ratio is . The surface is 150 times steeper in the -direction than in the -direction. The surface looks like a long, narrow valley . Steep walls on the sides (the -direction), a gently sloping floor along the bottom (the -direction).

Step 1 — Compute the gradient.Take partial derivatives.

So the gradient vector is.

Notice the ratio: . The gradient in is 150 times larger than the gradient in for the same distance from the origin. This is the asymmetry.

Step 2 — Run gradient descent with different learning rates.Start at and apply .

  • (too small).Each step is tiny: , . After hundreds of steps, you get close to but slow to a crawl. The updates vanish before you truly reach the minimum.
  • (moderately high).The -direction oscillates. At , . Next step, . The gradient pulls back. The -direction moves at per step — painfully slow. You zigzag along the valley. After many iterations, the gradient shrinks near the center and you may or may not reach the true minimum.
  • (high).Severe oscillation. The -coordinate bounces between positive and negative values. The update magnitudes in are roughly , but the gradient direction flips each step. The position bounces symmetrically. You are trapped.
  • (too high).The step overshoots so far that the gradient at the new point is even larger than before. The magnitude grows with each step: , then . The algorithm diverges — you fly out of the valley entirely.

Sense-check.The true minimum is at . For , the -update magnitude exceeds the distance to the minimum, causing overshoot. For , the -update is so slow you stall. Only a narrow band of values works, and even then, convergence is slow.

Assumptions & Scope.

Assumptions that make gradient descent work.

  • The loss surface issmooth(differentiable everywhere). If the function has kinks or discontinuities, the gradient is not defined.
  • The loss surface iswell-conditioned— the ratio of largest to smallest curvature (the condition number) is close to 1. When the condition number is large (like 150 in our example), gradient descent struggles.
  • The learning rate is chosen appropriately for the problem. There is no universal good value.

When gradient descent fails.

  • Ravines / ill-conditioned surfaces— exactly the scenario in this section. A single cannot handle both steep and flat directions.
  • Saddle points— points where the gradient is zero but it is not a minimum (like the center of a Pringles chip). Gradient descent stops here even though lower ground exists.
  • Local minima— the gradient is zero, so the algorithm stops. But a lower minimum exists elsewhere. Gradient descent has no way to know.
  • Non-differentiable functions— if the loss function has sharp edges (like ReLU at zero, or constraint boundaries), the gradient is undefined at those points.

What this section does NOT cover.Momentum, adaptive learning rates, second-order methods (Newton's method), stochastic gradient descent, or constrained optimization. Each of these is designed to fix specific failure modes of plain gradient descent.

13.1.5 Visual Intuition

Imagine a 3D plot with and on the horizontal plane and on the vertical axis. The surface is shaped like a long canyon running diagonally. The -axis direction has steep, nearly vertical walls. The -axis direction is a gentle slope, almost a flat plain.

Draw contour lines (lines of constant ) on the - plane. Near the origin, the contours are tightly packed in the -direction (steep) and widely spaced in the -direction (flat). The tight spacing means the gradient is large; the wide spacing means the gradient is small.

Now trace the path of gradient descent with . The trajectory bounces from one side of the canyon to the other, making slow diagonal progress. Each bounce wastes movement crossing the canyon rather than moving along it. With , the trajectory barely moves once it hits the flat center — it looks like it stopped. With , the trajectory spirals outward and off the page.

Takeaway.The shape of the loss surface — not just the algorithm — determines whether gradient descent succeeds. A surface with a high condition number (like 150) makes plain gradient descent impractical.

13.1.6 Student Questions and Answers

Q.I understood the low learning rate case — you get stuck at a local minimum or valley. And the high learning rate case — you oscillate and sometimes overshoot. What is the third problem?

A.There are two distinct problems within the high-learning-rate regime. One is zigzag oscillation — you go from one side of the loss surface to the other, then back, making slow progress. The other is being trapped in that oscillation . You just keep bouncing between the two walls and never descend further because the updates cancel out. And there is a third scenario: if the rate is far too high, you overshoot the trajectory entirely and diverge. In terms of root cause, there are only two: too small (stalling) or too large (oscillation/trapping/divergence). The large- case splits into sub-problems depending on severity.

Pitfalls.
  1. Confusing "flat region" with "minimum."Both have . Gradient descent cannot tell them apart. If your loss stops decreasing, you need to check whether you are at a true minimum or just stuck on a plateau. Plot the loss curve and verify it has flattened near zero — not just stopped changing.
  2. Treating the learning rate as a one-time choice.The right depends on the shape of the loss surface, which you do not know in advance. You must experiment. Start with or , observe the loss curve, and adjust. Tools like grid search or Bayesian optimization can help, but they are computationally expensive.
  3. Assuming all dimensions behave the same.They do not. The condition number of the Hessian (the matrix of second derivatives) tells you the ratio of steepest to flattest direction. If it is large (e.g., > 100), plain gradient descent will be slow no matter what you pick. You need momentum or adaptive methods.
  4. Forgetting to normalize features.If your input features have different scales (e.g., one feature ranges 0–1 and another ranges 0–1000). The loss surface becomes a ravine just like our example. Normalizing features to zero mean and unit variance before training removes this artificial curvature and helps gradient descent enormously.
  5. Checking only the final loss.Watch thetrajectory— the loss at each step. If it oscillates (goes up-down-up-down), your is too high. If it drops monotonically but incredibly slowly, your is too low. If it shoots up and never comes down, you have diverged.
Recap + Bridge.Plain gradient descent fails when the loss surface varies in steepness across dimensions. Because one fixed learning rate cannot serve both steep and flat directions. Small stalls; large oscillates. The next section introduces momentum . A modification that adds "memory" of past steps to smooth out the oscillation and carry you through flat regions. Momentum is the first of several fixes to the fundamental problems exposed here.

13.1.7 Real-World and Domain Connection

The failure modes of gradient descent are not academic curiosities. In deep learning, loss surfaces of neural networks are highly non-convex and ill-conditioned. Researchers have found that the Hessian of a trained neural network often has a few very large eigenvalues. And many near-zero ones . Exactly the ravine geometry we studied. This is why plain gradient descent is almost never used in modern deep learning. Every major framework (PyTorch, TensorFlow, JAX) defaults to momentum, Adam, or another adaptive variant. Understanding why plain gradient descent fails is the prerequisite to understanding why every subsequent optimizer exists. In production machine learning systems at companies like Google and Meta. The choice of optimizer and learning rate schedule can be the difference between a model that trains in hours versus one that never converges.

13.2 Momentum-Based Gradient Descent

13.2.1 Physical Intuition — The Heavy Ball

Hook.Rolling a marble down a bowl is easy. It finds the bottom and settles. But rolling a marble down our canyon from Section 13.1 — with steep walls and a flat floor — is hard. A marble with no weight gets stuck on the flat floor. A heavy marble, though, has momentum. It remembers where it was going. When it hits the flat spot, it keeps rolling. When it crosses the canyon, it settles into a path along the floor instead of bouncing wall-to-wall. How do we give gradient descent the physics of a heavy ball?

Standard gradient descent has no memory. Each step depends only on the current gradient. It does not "remember" that the previous step was large and in a certain direction. Momentum adds this memory.

Intuition + Analogy.Imagine pushing a heavy shopping cart down a sidewalk. You push it forward (gravity — the gradient). As it rolls, it gains speed. When you reach a flat stretch, you stop pushing, but the cart does not stop instantly. Its momentum carries it forward. If the sidewalk has side-to-side bumps, the cart's weight keeps it from jerking left and right . The forward momentum is stronger than the sideways bumps.

Now map this to optimization.

  • Thecart's positionat time is your parameter vector .
  • Thepush you giveat each step is the gradient — it points downhill.
  • Thecart's velocityis — how fast and in which direction it is currently rolling.
  • Thecart's weight(inertia) is controlled by — a friction parameter. A heavy cart ( close to 1) keeps rolling for a long time. A light cart ( close to 0) stops quickly and behaves like plain gradient descent.

In the canyon, the gradient tries to push you across the valley walls (side-to-side). But the momentum from rolling along the valley floor (forward) resists. Over time, the side-to-side pushes cancel each other out (positive then negative then positive), while the forward momentum accumulates. The cart finds a smooth path along the canyon floor.

The analogy breaks at one point: in physics, momentum is mass times velocity (). Here, we do not have a mass parameter. Instead, acts like a friction/damping coefficient controlling how much past velocity survives into the next step. Think of as "how heavy the ball is" — higher means heavier, more momentum.

In physics, momentum is mass times velocity. A heavy ball rolling downhill gains velocity. When it reaches the bottom, that velocity carries it forward. It might overshoot slightly, then roll back, then overshoot less, then settle. The oscillations dampen out because friction is working against the velocity.

Standard gradient descent has no memory. Each step depends only on the current gradient. It does not "remember" that the previous step was large and in a certain direction. Momentum adds this memory. It says: "I was moving fast in this direction previously. Let me keep some of that speed, plus respond to the current gradient."

13.2.2 Symbol Registry

Symbol Meaning Type Typical value
Weight vector at step vector in .
Velocity at step vector in .
Gradient of the loss at vector in .
Learning rate — step size scalar 0.001–0.1
Friction parameter — scales old velocity scalar 0.8–0.9

13.2.3 Mathematical Formulation

Momentum-based gradient descent uses two equations. The first builds the velocity (a smoothed version of the gradient history). The second updates the weights using the velocity instead of the raw gradient.

Velocity update — the memory equation.

This says: the new velocity is a blend of the old velocity (weighted by ). And the current gradient (weighted by ).

  • If , you keep 90% of the old velocity and add only 10% of the current gradient. The velocity decays slowly — it has a long memory. Past gradients from many steps ago still influence the current velocity.
  • If , you keep only 50% of the old velocity. The memory is short — only very recent gradients matter.
  • If , . The velocity is just the raw gradient. This reduces to plain gradient descent (see below).

Weight update — using the velocity.

This is the key difference from standard gradient descent. Instead of stepping in the direction of , you step in the direction of — the smoothed gradient history. The velocity already incorporates past gradient information through the term.

What happens at the first step.At , there is no previous velocity. You set . The very first velocity is then . This makes the first momentum step identical indirectionto a gradient descent step, butscaledby . If , the first step is only 20% as large as a pure gradient descent step with the same . This is why momentum often needs a slightly higher learning rate than plain gradient descent — the early steps are diminished.

Why the velocity dampens oscillation.Consider the canyon from Section 13.1. When you are on the left wall, the gradient points right (toward the right wall). The velocity, however, was built up from sliding down the left wall . It also points right, but its magnitude is driven by the steep wall slope. When you cross the center and land on the right wall, the gradient now points left. But the velocity — which is 90% old velocity — still points right (from the previous step). The two oppose each other. The velocity wins because gives it 9× more weight than the new gradient. Instead of bouncing back immediately, you slide along the valley floor.

Think of two forces: the gradient pulls you across the valley (side-to-side). The velocity pulls you along the valley (forward). Over many steps, the side-to-side gradient pulls cancel out (positive, negative, positive...), while the forward velocity accumulates. The net result is smooth forward progress.

When .If you set .

This is exactly standard gradient descent. Momentum with reduces to plain gradient descent. So momentum is a generalization — gradient descent is the special case with no memory. As increases, you get more and more momentum.

Why you cannot use velocity alone (without gradient).The gradient provides the local terrain information — which way is downhill right here, right now. The velocity provides history — where you were going before. Without the gradient, the velocity would never change direction, even if you passed the minimum and started going uphill. The gradient acts as a steering wheel. The velocity acts as the engine. You need both. The equation blends them: the term is the engine (momentum), the term is the steering wheel (gradient).

Notation note — alternative formulations.The companion document for this lecture uses a different (but equivalent) form.

In this version, the momentum term is theactual previous displacement(the vector from the old position to the current position), not a separate velocity variable. This is sometimes called the "heavy ball" form after Polyak (1964). Both forms encode the same idea: the update includes a fraction of the previous step. The two-equation velocity form used in this lecture is the standard implementation in PyTorch, TensorFlow. And JAX because it requires storing only the velocity vector rather than both and . The equivalence: if you define (up to scaling by ), the two forms match.

13.2.4 Worked Example — Momentum Step Calculation

Consider the same loss function from Section 13.1.

with gradient.

Start at . Use momentum with and .

Step 1 — Compute the gradient at .

Notice the -component is about 63× larger than the -component. The surface is much steeper in .

Step 2 — Compute the first velocity.With and .

The first velocity is exactly 20% of the gradient. The -component (1.526) dominates — as expected, since the surface is much steeper in the -direction.

Step 3 — Weight update.

The -coordinate barely moves (drop of 0.0002). The -coordinate drops by 0.015. Both changes are small because and the first step is further reduced by the factor.

Step 4 — Second gradient.At .

Step 5 — Second velocity.

Notice what happened: the old velocity contributed and the new gradient contributed . They add constructively in this step because both point in the same direction (downhill). The velocity is growing — the ball is accelerating.

Step 6 — Second weight update.

What happens at the valley bottom.When the ball crosses the center and the gradient reverses, the new gradient and old velocity oppose each other. Say at some future step , (moving strongly in the negative direction). But the gradient is (pointing strongly positive ). Then.

The velocity in went from to — it is beingdampenedbut not reversed. The gradient wanted to push it positive, but the momentum (weighted at 0.8) overpowers it. This is how momentum avoids the zigzag. It keeps moving in the accumulated direction instead of jerking back with every gradient sign change.

Sense-check.The true minimum is at . After 6 steps, we are at approximately — still far away. But the velocity is building. In flat regions where the gradient becomes small, the velocity persists ( term dominates), so the ball keeps rolling. This is the key advantage over plain gradient descent, which would stall as soon as the gradient shrinks.

13.2.5 Student Questions and Answers

Q.If the velocity part is so important, why do we need the gradient at all? Why not just use only the velocity to update the weights?

A.You need the gradient to know the curvature. Velocity alone cannot tell you where the steepness is or in which direction. The gradient provides local surface information — is this spot steep or flat? Is the slope in this direction or that? Without the gradient, the velocity would not know how to adjust to the terrain. Look at the equation: . Both terms matter. The term gives you momentum — it pushes you in the direction you were already going. The term gives you steering — it nudges you toward the locally steepest descent. Without the gradient, you would be steering blind. If the velocity carried you past the minimum and onto an uphill slope, only the gradient can signal "turn around."

Q.Can we have independent learning rates for different directions? One learning rate for each parameter?

A.Yes. This is exactly what adaptive learning rate methods do. Algorithms like Adagrad, RMSProp, and Adam adapt the learning rate differently for different parameters. They reduce the learning rate for directions where the gradient has historically been large (steep dimensions — to prevent oscillation). They keep it higher for directions where the gradient has been small (flat dimensions — to make progress). The only way to find the right learning rates is trial and error. You cannot know in advance how many local minima exist or how many flat valleys lie in a high-dimensional loss surface. You try multiple combinations, observe which reduce the training error, and select the best. Tools like GridSearchCV and Bayesian optimization can automate this search.

Q.Doesn't gradient descent already have a direction and a scalar? The gradient shows the direction and the velocity gives a push in that direction. How do these interact?

A.The velocity term already includes the gradient — look at . The velocity itself is built from past gradients. So when you compute , you are already using gradient information through the velocity. Think of it this way: the term contains a long-running average of all past gradients. The term adds the current gradient to this average. The velocity is the smoothed gradient history. The update direction comes from that smoothed history rather than the raw instantaneous gradient. When the current gradient is large, multiplying it by (e.g., 0.1) reduces its immediate effect — it cannot jerk the trajectory. When the current gradient is small, the velocity term — carrying momentum from earlier, larger gradients — still carries weight. This is why momentum keeps moving through flat regions.

Q.There are two different formulas in different sources — one says the weight update adds the velocity, another uses a different form. Which is correct?

A.Both are correct — they are equivalent forms of the same idea. The canonical implementation form (used in PyTorch, TensorFlow, and JAX) uses two equations.

  1. — build velocity
  2. — update weights

The alternative form (Polyak's heavy ball, also shown in the companion document for this lecture) writes it as a single equation.

Here, is the actual displacement from the previous step . The momentum term is the previous update itself rather than a separate velocity variable. Under a change of variables, the two forms are equivalent. The professor's version (the two-equation form) is the one you should use on the exam and in code. Because it is the standard in all major deep learning frameworks. The learning rate is essential in both forms — it scales the step before subtracting from the weights.

Pitfalls.
  1. Setting too high ().The velocity has such a long memory that it barely responds to changes in the gradient. If the minimum is behind you, momentum keeps pushing you forward for hundreds of steps before turning around. This wastes computation and can overshoot badly.
  2. Setting too low ().The velocity decays too quickly. You get very little benefit from momentum — it behaves almost like plain gradient descent. The standard value is a good default.
  3. Forgetting the scaling on the first step.With , the first velocity is . If , the first step is only 10% of what plain gradient descent would take with the same . You may need to compensate with a slightly larger or use a warm-up schedule.
  4. Using momentum on a well-conditioned problem.If your loss surface is already a nice bowl (condition number close to 1), momentum adds little benefit and may cause overshooting. Plain gradient descent works fine. Momentum is a fix for ill-conditioned problems — do not apply fixes you do not need.
  5. Confusing with . controls the overall step size. controls memory length. They interact: with high , you can use a smaller because the velocity accumulates over time. With low , you need a larger to make progress. Tune them together, not independently.
Recap + Bridge.Momentum adds a velocity term that remembers past gradients, smoothing out the trajectory. The velocity equation blends old memory () with new gradient (). This dampens oscillation across steep valleys and carries you through flat plateaus. But momentum still uses one global learning rate for all parameters. The next three sections introduce adaptive methods that give each parameter its own learning rate . Fixing the remaining problem that different dimensions need different step sizes.
Notation note — alternative forms.The standard form of the momentum equations in deep learning frameworks matches the professor's version above with the scaling on the gradient term. The companion document shows the alternative Polyak heavy-ball form . Both are valid and encode the same physical intuition.

13.2.6 Real-World and Domain Connection

Momentum with is the default optimizer in PyTorch's SGD implementation. And appeared as the standard training algorithm for ImageNet-winning architectures like AlexNet and VGG. When training ResNet-50 on ImageNet. The standard recipe uses SGD with momentum . And a learning rate schedule that drops by a factor of 10 at specific epochs. This recipe has been so successful that "SGD + momentum" became the baseline against which all new optimizers are compared. Even today, when Adam is more popular for many tasks. SGD with momentum remains the preferred choice for state-of-the-art image classification models because it often generalizes better . Momentum finds flatter minima that transfer better to test data. In the 2018 paper "Three Mechanisms of Weight Decay," researchers showed that combining momentum with weight decay. And a learning rate schedule produced better vision models than Adam on several benchmarks.

13.2.7 Industry Applications

Most deep learning frameworks — PyTorch, TensorFlow, JAX — implement momentum-based gradient descent as the default or a primary option. The friction parameter is almost universally set to 0.9 in implementations across these tools. This is not arbitrary . 0.9 has been found empirically to give the best dampening behavior across a wide range of architectures and datasets.

In NLP, when you convert words to high-dimensional vectors (word embeddings), some words appear very frequently and others rarely. You do not want the frequent words to dominate gradient updates. Adaptive methods address this by effectively assigning different learning rates to different parameters based on how often they appear. For a very frequent word like "the", the learning rate might be small (0.003) because its gradient has been seen many times. For a rare word, the learning rate stays higher (0.03) because there is less accumulated gradient history.

13.3 Adagrad — Adaptive Gradient

13.3.1 Definition and Intuition

Hook.Momentum fixed the zigzag by smoothing the direction. But it left one problem untouched: every parameter still gets the same learning rate . In our canyon, the -direction is 150× steeper than the -direction. The step that is right for is wrong for . What if each parameter could have its own learning rate . A small one for steep directions and a large one for flat directions? That is Adagrad.

Adagrad stands foradaptive gradient. Unlike momentum, which tackles the oscillation problem by adding velocity. Adagrad tackles it by adaptively changing the learning rate itself — differently for each parameter.

Intuition + Analogy.Imagine each parameter is a road with a toll booth. Every time a gradient update passes through (a car drives by), the toll increases. If a road sees heavy traffic (large gradients), its toll becomes very expensive. And future cars slow down — they take smaller steps. If a road sees light traffic (small gradients), its toll stays cheap, and cars keep moving fast.

Now map this to the canyon.

  • The -direction is a busy highway. Gradients are large. Every step adds to the toll. After a few steps, the toll is high → the effective learning rate in becomes small → no more oscillation.
  • The -direction is a quiet back road. Gradients are tiny. The toll barely increases → the effective learning rate in stays close to the original . You keep making progress along the flat canyon floor.

This is adaptive cruise control for optimization. The system automatically slows you down where you are already going fast (steep directions). And speeds you up where you are crawling (flat directions).

The analogy breaks at one point: in a real toll booth, the toll resets periodically. In Adagrad, it never resets — it only increases. This is the algorithm's main weakness, which RMSProp was designed to fix.

The core idea: parameters that have received large gradients in the past should get smaller updates in the future. Parameters that have received small gradients should get larger updates. This is the opposite of what standard gradient descent does. With a fixed learning rate, large gradients produce large steps, which cause oscillation. Small gradients produce small steps, which cause stalling.

The mechanism: you accumulate a history of squared gradients. When the sum of past squared gradients is large. You divide the learning rate by a large number, making the effective step smaller. When the sum is small, the effective step stays large.

13.3.2 Symbol Registry

Symbol Meaning Type
Accumulated squared gradient at step scalar (per parameter) or vector
Gradient of the loss at step vector in
Global learning rate scalar
Weight at step vector in

13.3.3 Mathematical Formulation

Adagrad has two steps per iteration. First, accumulate the squared gradient. Second, use it to scale down the learning rate.

Step 1 — Accumulate the squared gradient.

This says: take the previous accumulated sum of squared gradients and add thesquareof the current gradient. The square eliminates the sign — both positive and negative gradients add to the sum. If gradients have historically been large, grows quickly. If gradients have been small, grows slowly.

In practice, implementations add a tiny constant (e.g.. ) to the denominator to avoid division by zero on the first step when . The full form is .

Step 2 — Update the weight with adaptive learning rate.

The global learning rate is divided by . This is the adaptive part.

  • When is large (this parameter has seen many large gradients) → is small → you take a cautious step. This prevents oscillation in steep directions.
  • When is small (this parameter has seen only small gradients) . stays close to → you take a normal-sized step. This helps you make progress through flat directions.

Walkthrough on the canyon example.In our loss function .

  • The -direction gradient is . Starting at , the gradient is 7.63. Its square is 58.2. After one step, . After two steps, . The denominator . The effective learning rate for is — already reduced by a factor of 10.
  • The -direction gradient is . Starting at , the gradient is 0.12. Its square is 0.014. After one step, . After two steps, . The denominator . The effective learning rate for is — essentially unchanged.

In the visualization, Adagrad reaches the minimum first among all methods tested. The steep -direction's effective learning rate drops quickly, dampening oscillation. The flat -direction's effective learning rate stays high, speeding up valley progress.

The critical weakness. never decreases. It only grows with each step — every new gradient, squared, gets added. Over a very long training run, can become enormous. The effective learning rate approaches zero. Training grinds to a halt even though you have not converged. This monotonic decay is why Adagrad is rarely used for long training runs. RMSProp and Adam were designed to fix this.

13.3.4 Student Questions and Answers

Q.What should the initial value of be? Is there a standard assumption?

A. is not a tunable hyperparameter like or . It is the accumulated history of squared gradients. You start with (or a small positive number like to avoid division by zero on the first update). Every step, you add the square of the current gradient. So . It grows automatically from the data — there is no hyperparameter to tune for itself. It is purely a running sum derived from the gradients you compute during training.

Q.Does Adagrad reduce the learning rate so much that the number of steps increases? Is that a disadvantage?

A.Yes and yes. The effective learning rate decreases over time because accumulates. Each step becomes progressively smaller. You may need more iterations to converge than with a constant learning rate. This monotonic decay is the known weakness of Adagrad. In very long training runs (e.g., training a large neural network for hundreds of epochs). The effective learning rate can effectively reach zero before convergence, and the model stops learning. RMSProp and Adam fix this by replacing the cumulative sum with a weighted moving average of squared gradients . An average that can godownwhen recent gradients are small, preventing the learning rate from vanishing.

Pitfalls.
  1. Forgetting that grows forever.Adagrad is great for convex problems or short training runs. For deep neural networks trained over many epochs, the learning rate decays to near zero and training stalls. Use RMSProp or Adam for long training runs.
  2. Using too small a global .Since each parameter's effective rate is already divided by . Starting with a tiny means even the flat directions get near-zero updates. A common starting value for Adagrad is (higher than you might use for plain gradient descent).
  3. Not adding in the denominator.On the first step, , so you would divide by zero. Always use with in code. The professor's slides omit for clarity, but every implementation includes it.
  4. Expecting Adagrad to work well on non-stationary problems.If the gradient distribution changes during training (e.g., different mini-batches have very different gradient magnitudes), the ever-growing cannot adapt. RMSProp handles this better because its moving average can forget old gradients.
Recap + Bridge.Adagrad gives each parameter its own effective learning rate by dividing the global by . Where is the sum of past squared gradients. Steep directions accumulate large and get small steps; flat directions keep small and get normal steps. The weakness: only grows, so the learning rate monotonically decays. RMSProp fixes this by replacing the cumulative sum with a weighted moving average that can forget old gradients. And adapt to changing conditions.

13.3.5 Real-World and Domain Connection

Adagrad was introduced by Duchi, Hazan. And Singer in 2011 and was one of the first adaptive learning rate methods to gain widespread adoption. It proved particularly effective for sparse data — problems where most features are zero most of the time. In natural language processing, word frequency follows a Zipfian distribution: "the" appears millions of times, while "hippopotamus" appears maybe once. With a fixed learning rate, common words dominate the gradient signal. Adagrad automatically reduces the effective learning rate for frequently occurring features. (which accumulate large ) and keeps it high for rare features (which accumulate little). This made Adagrad the go-to optimizer for word embedding models like word2vec and GloVe in the early 2010s. Google's original word2vec paper used Adagrad for training. Today, Adagrad has largely been superseded by Adam for most applications. But the idea of per-parameter adaptive learning rates — which Adagrad pioneered — is the foundation of every modern optimizer.

13.4 RMSProp

13.4.1 Definition and Intuition

Hook.Adagrad's toll booth has a fatal flaw: the toll only goes up, never down. After thousands of cars, every road becomes too expensive, and all traffic stops. What if the toll booth were smart . Raising the price when traffic is heavy, but lowering it when the road goes quiet? That is RMSProp.

RMSProp — Root Mean Square Propagation — fixes the main weakness of Adagrad. Instead of accumulating all past squared gradients in a sum that only grows, RMSProp uses an exponentially weighted moving average. This average can adapt — it rises when recent gradients are large and falls when they are small.

Intuition + Analogy.Expand the toll booth analogy from Adagrad. The smart toll booth cares more aboutrecenttraffic than traffic from hours ago. It blends the old toll price with the price of the most recent car, weighted by .

  • If (the typical value). Then 90% of today's toll price comes from the old price and 10% from the most recent car. Old traffic slowly fades from memory.
  • If a road was busy an hour ago but is quiet now, the toll pricedecreasesover time as old, expensive cars are forgotten. Traffic can speed up again.
  • If a road suddenly gets busy, the toll price rises quickly to slow things down.

Compare with Adagrad's toll booth: every car ever is remembered with equal weight. An hour of heavy traffic at 9 AM permanently raises the price. Even at 3 PM when no one is on the road. RMSProp is smarter — it adapts to changing conditions.

The analogy breaks at one point: is not truly a "memory length" in terms of absolute time steps. With , a gradient from 10 steps ago contributes of its original weight. It is still present, just diminished. The effective memory horizon is roughly steps — for , that is about 10 steps.

13.4.2 Mathematical Formulation

Symbol Registry — RMSProp

Symbol Meaning Type Typical value
Running average of squared gradients scalar/vector .
Decay rate for the running average scalar 0.9
Gradient of the loss at step vector .
Global learning rate scalar .

RMSProp changes only one thing from Adagrad: how is computed. The weight update formula is identical.

The running average update (the only difference from Adagrad).

This is a weighted moving average.

  • keeps a fraction of the old running average.
  • adds a fraction of the current squared gradient.
  • controls the memory length. → long memory (past gradients decay slowly). → short memory (only very recent gradients matter).

Contrast with Adagrad's , where old and new gradients are added with equal weight, and the sum only grows.

The weight update (same form as Adagrad).

The only difference from Adagrad is the definition of . Because RMSProp's is a weighted average (not a cumulative sum), it can go both up AND down.

  • If recent gradients are large → grows → effective learning rate shrinks → oscillation is dampened.
  • If recent gradients become small → shrinks (old large gradients decay out of memory) . Effective learning rate rises again → progress resumes in flat regions.

This prevents the vanishing learning rate problem. The learning rate adapts to thecurrentgradient landscape, not the entire history since step zero.

How the decay works numerically.With .

  • A squared gradient contributes of its value to at the current step.
  • After 1 step, it contributes .
  • After steps, it contributes .
  • After about 23 steps (), the contribution falls below 1% of the original. The effective memory window is roughly steps (the time constant of the exponential decay).

Why the name "Root Mean Square Propagation."The denominator is essentially theroot mean square(RMS) of recent gradients. The running average approximates the mean of squared gradients. Its square root gives the RMS. The algorithm propagates the RMS of past gradients to scale the current gradient — hence "Root Mean Square Propagation."

Worked Example — Comparing Adagrad vs RMSProp on one parameter.Consider a single parameter with the following gradient values over 5 steps.

Use . Start with . Assume for RMSProp.

Adagrad.

  • Step 1: . Effective LR = . Update: .
  • Step 2: . Effective LR = . Update: .
  • Step 3: . Effective LR ≈ 0.02. Update: .
  • Step 4: same tiny updates.
  • Step 5: . Effective LR = . Update: .

After step 5, the effective learning rate isstill smallbecause step 1's big gradient permanently inflated .

RMSProp.

  • Step 1: . Effective LR = . Update: .
  • Step 2: . Effective LR = . Update: .
  • Step 3: . Effective LR = .
  • Step 4: . Effective LR = .
  • Step 5: . Effective LR = . Update: .

Key observation.In steps 2–4 (small gradients), RMSProp's decreasedfrom 2.5 to 1.825, allowing the effective learning rate toincreasefrom 0.063 to 0.074. Adagrad's only grew, keeping the effective LR stuck. Then at step 5 (large gradient), RMSProp's rose again to 4.143, dampening the step.

Sense-check.After many small gradients, RMSProp recovers and takes larger steps again. Adagrad never recovers. This is why RMSProp is preferred for non-stationary or long-running optimization problems.

Assumptions & Scope.

Assumptions.

  • The gradient distribution may change over time (non-stationary). RMSProp handles this; Adagrad does not.
  • A single decay rate works for the entire training run. In practice, is almost always a good default.

When RMSProp is the right choice.

  • Long training runs where Adagrad's learning rate would decay to zero.
  • Problems where gradient magnitudes vary over time (e.g., different mini-batches have different statistics, or you are using a curriculum learning strategy).
  • Online learning settings where the data distribution shifts (concept drift).

When RMSProp might not help.

  • If the problem is well-conditioned, adaptive methods add unnecessary overhead. Plain SGD or SGD with momentum may work just as well and generalize better.
  • If is set too close to 1 (e.g., 0.999). The running average behaves like Adagrad's cumulative sum — it barely forgets anything. Setting it too close to 0 (e.g., 0.5) makes the average too jumpy — it reacts excessively to every gradient fluctuation.
Pitfalls.
  1. Confusing (RMSProp) with (momentum).Both are decay parameters around 0.9, but they serve different purposes. controls velocity memory (direction smoothing). controls the running average of squared gradients (learning rate adaptation). In Adam, both appear together, so keep them distinct.
  2. Setting too high ().The running average barely changes. Old large gradients dominate for hundreds of steps. RMSProp loses its adaptivity and behaves like Adagrad.
  3. Setting too low ().The running average is too volatile. It jumps around with every gradient, and the effective learning rate fluctuates wildly. Training becomes unstable.
  4. Forgetting the in the denominator.As with Adagrad, always use with to avoid division by zero on early steps. The professor's slides may omit it, but every implementation includes it.
Recap + Bridge.RMSProp replaces Adagrad's cumulative sum with a weighted moving average of squared gradients: . The running average can adapt up or down, preventing the learning rate from vanishing. RMSProp handles changing gradient distributions; Adagrad does not. The next section introduces Adam, which combines RMSProp's adaptive learning rates with momentum's velocity — getting the best of both worlds.

13.4.3 Real-World and Domain Connection

RMSProp was introduced by Geoffrey Hinton in a 2012 Coursera lecture (never formally published as a paper . It originated from a slide in his online course. Which is an unusual origin for one of the most-used optimization algorithms in deep learning). RMSProp became immediately popular because it solved a practical problem: training recurrent neural networks (RNNs) on long sequences. RNNs have very non-stationary gradient distributions — gradients explode early in training and vanish later. Adagrad's monotonically decaying learning rate was disastrous for RNNs because it killed learning in later epochs. RMSProp's adaptive running average allowed the learning rate to recover when gradients became small. Making it the default optimizer for sequence models in the mid-2010s. Today, RMSProp is less commonly used as a standalone optimizer but lives on as the component inside Adam . Every Adam update implicitly uses RMSProp's mechanism.

13.5 Adam — Adaptive Moment Estimation

13.5.1 Overview and Context

Hook.You have seen two separate ideas. Momentum smooths thedirectionof your steps by remembering past gradients. RMSProp scales thesizeof your steps by adapting the learning rate per parameter. Can you use both at once — smooth direction AND adaptive step size? That combination is Adam. It is the most widely used optimizer in deep learning today.

Adam combines the ideas of momentum and RMSProp into a single algorithm. It maintains two running averages: one for the gradient (like momentum's velocity) and one for the squared gradient (like RMSProp's ). It also includes bias correction terms for the early steps. When the running averages start from zero and need time to "warm up."

Intuition + Analogy.Think of Adam as a complete traffic management system for your city.

  • Momentumis thetraffic directorwho looks at the general flow of cars and guides them along the smoothest, most consistent routes. It prevents jerky side-to-side movements.
  • RMSPropis thesmart toll booththat adjusts prices per road based on recent traffic. It slows down busy highways and speeds up quiet back streets.
  • Adamis both at once. A traffic director who also controls toll prices. Cars flow smoothly (momentum) at the right speed for each road (adaptive learning rate).

The "Adaptive Moment Estimation" name comes from the two running averages.

  • Thefirst moment(mean) of the gradient — the momentum term .
  • Thesecond moment(uncentered variance) of the gradient — the RMSProp term .

In statistics, the first moment of a distribution is its mean; the second moment is related to its variance. Adam estimates both moments adaptively as training proceeds.

The full Adam algorithmuses four equations.

1. Gradient computation.

2. First moment estimate (momentum — direction smoothing).

This is the momentum term. (typically 0.9) controls how much old gradient direction is remembered. is a running average of the gradient — it smooths out the trajectory.

3. Second moment estimate (RMSProp — step size adaptation).

This is the RMSProp term. (typically 0.999) controls the memory of squared gradients. scales the learning rate per parameter.

4. Weight update.

The update uses thesmoothed direction (from momentum) and divides by theadaptive scale (from RMSProp). The result: smooth, well-paced steps in every dimension.

Bias correction (not covered in this lecture but standard in implementations).Because and , the early estimates are biased toward zero. Adam corrects this by dividing by and respectively. Full coverage in the next session.

In the visualization with the loss function , Adam's trajectory is similar to Adagrad's and RMSProp's. It adapts the effective learning rate per dimension and reaches the minimum efficiently. The momentum term helps smooth the path, reducing residual oscillation that might remain with pure RMSProp.

Comparison — Gradient Descent vs Momentum vs Adagrad vs RMSProp vs Adam
Feature GD Momentum Adagrad RMSProp Adam
Per-parameter LR No No Yes Yes Yes
Direction smoothing No Yes No No Yes
LR can increase N/A N/A No (only decreases) Yes Yes
Handles sparse gradients Poor Poor Good Good Good
Long-training stability Poor Medium Poor (LR vanishes) Good Good
Number of hyperparameters 1 () 2 () 1 () 2 () 3 ()
Default settings

When to pick which.

  • Start withAdamfor most deep learning problems. It is the most robust default.
  • If Adam's validation performance plateaus, trySGD + momentum— it sometimes generalizes better for vision tasks.
  • For very simple problems (linear regression, logistic regression on small data), plainGDwith a well-tuned is fine.
Pitfalls.
  1. Using Adam without understanding what and do. controls momentum memory. controls adaptive learning rate memory. The asymmetry ( much closer to 1) is deliberate. Squared gradients need a longer memory to produce stable estimates of the second moment.
  2. Treating Adam as always better.Adam converges faster in training, but SGD + momentum sometimes finds solutions thatgeneralizebetter (lower test error). Adam can converge to sharper minima that do not transfer well. For vision benchmarks like ImageNet, SGD + momentum often beats Adam on final test accuracy.
  3. Ignoring bias correction when implementing from scratch.If you implement Adam without the correction terms, the first few steps will be severely scaled down because . This is why frameworks like PyTorch and TensorFlow include bias correction by default.
  4. Tuning Adam's too high.Because Adam adapts per-parameter, the effective learning rate can be larger than you think. The recommended default is , which is an order of magnitude smaller than typical values for plain SGD ().
  5. Using Adam where second-order methods are better.For small, well-conditioned convex problems, Newton's method or L-BFGS may converge in far fewer iterations. Adam is designed for stochastic, high-dimensional, non-convex problems — it is overkill for a 5-parameter linear regression.
Recap + Bridge.Adam combines momentum (direction smoothing via ) and RMSProp (per-parameter adaptive learning rate via ). It uses two decay rates: for the first moment and for the second moment. Adam is the default optimizer for most deep learning applications. All methods so far have beenunconstrained— the parameters could go anywhere. The next section introducesconstrained optimization, where parameters must satisfy rules, and the Lagrangian method for handling them.

13.5.2 Real-World and Domain Connection

Adam was introduced by Kingma and Ba in their 2014 paper "Adam. A Method for Stochastic Optimization," which has been cited over 150,000 times . Making it one of the most cited papers in computer science history. Adam is the default optimizer in nearly every deep learning framework and library. The Transformer architecture (the foundation of GPT, BERT, and all modern LLMs) was originally trained with Adam using , , and . The GPT-3 paper used Adam with , , and a learning rate of with batch size 3.2M tokens. Vision Transformers (ViT), diffusion models (Stable Diffusion). And most generative models use Adam or AdamW (Adam with decoupled weight decay) as their optimizer. The specific hyperparameters vary by domain — vision models often use . While language models often reduce to 0.95 or 0.98 for better stability with very large batch sizes.

13.6 Constrained Optimization and Lagrangian

13.6.1 Definition and Motivation

Hook.Every optimizer we have studied so far lets the parameters wander anywhere — there are no fences, no rules, no forbidden zones. But real-world parameters always have limits. A probability must stay in . A portfolio cannot spend more than your budget. A factory cannot use negative hours of labor. What happens when gradient descent hits a constraint? It stops dead — the gradient is undefined at the boundary. How do you optimize when the parameter space has walls?

All optimization so far has beenunconstrained— you minimize a loss function with no restrictions on the parameter values. But many real problems have constraints. You might need to keep weights within a certain range, or ensure they satisfy an equation, or stay within a budget. This isconstrained optimization.

The problem: constraints create sharp edges on the feasible region. The loss surface might be smooth, but the boundaries — where constraints cut off the surface — are not. Gradient descent fails at edges. The gradient is undefined there. Even with momentum, the ball cannot roll past a sharp edge. It gets stuck.

Intuition + Analogy.You are walking in a park. The park is beautiful (that is the objective function — you want to find the best spot). But there is a fence around a restricted area (that is the constraint — you cannot cross it). If you cross the fence, a park ranger fines you. The amount of the fine is — the Lagrange multiplier.

Now consider three scenarios.

  • You are well inside the park, far from the fence. No fine. . The constraint does not matter.
  • You are standing right at the fence. The ranger says: "I will fine you rupees for every meter you cross." has a real meaning now — it is the price of the constraint.
  • You cross the fence. The ranger sets an infinite fine: . The penalty is so high you are forced back inside.

This is the Lagrangian method: you add the fine (constraint × multiplier) to your objective, and then solve theunconstrainedproblem of minimizing (objective + fines). The fines enforce the constraints automatically — you never actually cross the fence because crossing makes the total cost explode.

The fence analogy comes from the companion document for this lecture. Which uses a Park Ranger (setting fines) and a Climber (choosing the location) to explain the primal-dual relationship. The professor's version uses a simpler fence-and-fine narrative.

The solution: convert the constrained problem into an unconstrained one by folding the constraints into the objective function with penalty terms. This is the Lagrangian method.

13.6.2 Symbol Registry

Symbol Meaning Type
Objective function to minimize or maximize scalar function,
Constraint function scalar function
Lagrange multiplier (penalty parameter) for constraint scalar
Lagrangian — combined objective with constraints scalar function
Decision variables (e.g., model parameters) vector in

13.6.3 The Lagrangian Function

The Lagrangian construction.You start with an objective function to minimize. You have constraints (inequality constraints) or (equality constraints). The Lagrangian folds each constraint into the objective with a multiplier.

where.

  • is the original objective (what you want to minimize or maximize).
  • Each is a constraint function, written so that means "satisfying the constraint."
  • Each is a Lagrange multiplier — the penalty for violating constraint .
  • is the Lagrangian — a new, unconstrained function.

How to read the formula.The Lagrangian is the original objectivepluspenalty terms. If you satisfy constraint (so ), the penalty term is negative or zero . It reduces the Lagrangian or leaves it unchanged. If you violate the constraint (), the penalty is positive and increases the Lagrangian. The optimizer is punished for crossing constraints.

Why this works.The original constrained problem.

can be rewritten as the min-max problem.

Here is why.

  • If you pick an that violates , the inner max sets , making . Such are eliminated — they are infinitely expensive.
  • If you pick an that satisfies all constraints (). The inner max sets for inactive constraints (since making positive would add a negative term. Reducing the Lagrangian — which the "max" would avoid). The Lagrangian reduces to .

So the min-max over the Lagrangian is equivalent to the original constrained problem. This is the foundation of Lagrange duality, which will be explored in the next session.

Think of it with the fence analogy from earlier. You are walking in a park. There is a fence — you are not allowed to cross it. If you try to cross, you pay a fine. The fine is the term. The park experience is . The combined function — park experience plus any fines — is the Lagrangian.

When you solve the unconstrained problem of minimizing the Lagrangian, you are still trying to stay within the constraints. Crossing them incurs a penalty that increases the Lagrangian value. The penalty parameters control how harshly violations are punished.

13.6.4 Worked Example — Bounded 2D Surface

Consider the problem.

subject to.

Step 1 — Understand the geometry.The constraints define a diagonal strip in the - plane.

  • The line is the upper boundary (northeast).
  • The line is the lower boundary (southwest).
  • The feasible region is everything between these two parallel lines — an infinite diagonal band.

The objective is a paraboloid bowl centered at the origin . As you move away from the origin, the function value grows quadratically. To minimize it under the constraints, you want the pointinside the stripthat isclosest to the origin.

Step 2 — Find the unconstrained minimum.The unconstrained minimum of is at , where . But check the constraints: at , . Is between 1 and 2? No — . So the unconstrained minimum isnotin the feasible region. The constraint is violated.

Step 3 — Find the constrained minimum by reasoning.The origin is the global minimum. The constraint means the origin is excluded. The closest point in the strip to the origin must lie on thelowerboundary (since moving from the origin toward the strip, you first hit the lower boundary). Among all points on the line , the one closest to the origin is the perpendicular projection.

The line has normal vector . The closest point to the origin is where the position vector is parallel to the normal: for some . Substituting into : , so . The optimal point is .

Step 4 — Verify.At : . Check any other point on the boundary, e.g., : . Check : also on the boundary and . The point is indeed the minimum.

Step 5 — Why gradient descent fails here.The feasible region is a strip. At the optimal point on the boundary , the gradient of is.

This gradient points northeast (away from the origin). If you take a gradient descent step from the boundary, you would move southwest . Back toward the origin, which is outside the feasible region. You would immediately violate the constraint. If you stop at the boundary, you are stuck . The true minimum of inside the feasible region is on the boundary. But gradient descent cannot "slide along" the boundary because the gradient points across it.

Step 6 — The Lagrangian solution.Rewrite the constraints in standard form ().

Construct the Lagrangian.

with .

Now solve the unconstrained problem by taking partial derivatives.

From the first equation: . From the second: . So .

Only one constraint can be active at the optimum (you cannot be on both boundaries simultaneously since the lines are parallel). Since the origin is below the lower boundary, the lower constraint is active: .

With and : , so . Then , so .

Final answer: with and .The multiplier tells you themarginal costof the constraint: if you relax the constraint from to , the optimal objective value improves by approximately .

Sense-check.The Lagrangian gives the same answer as geometric reasoning. The multiplier means the constraint is binding and costly — loosening it would improve the objective.

Scope — when constraints matter and when they do not.

Active vs inactive constraints.If the unconstrained minimum already satisfies a constraint, that constraint isinactive— its multiplier . The constraint does not affect the solution. If the unconstrained minimum liesoutsidethe feasible region, the constraint isactive — and the optimum lies on the constraint boundary.

Equality vs inequality constraints.

  • (equality): The multiplier can be any real number (positive or negative). The optimum always lies on the boundary.
  • (inequality): . The optimum may be in the interior () or on the boundary ().

When the Lagrangian method fails.

  • Non-differentiable constraints: if is not smooth, the Lagrangian's derivatives are undefined.
  • Non-convex feasible regions: if the feasible set has holes or disconnected components. The Lagrangian may find a local minimum that is not global.
  • Integer constraints (): the Lagrangian method works for continuous variables. Discrete variables need integer programming, which is a different class of algorithms entirely.

13.6.5 Visual Intuition

Draw the - plane. Mark the origin . Draw two parallel diagonal lines: (southwest) and (northeast). Shade the region between them — this is the feasible strip.

Now draw concentric circles centered at the origin, each representing a level set of . The circles grow in radius as increases. The smallest circle thattouchesthe shaded strip is the one that grazes the lower boundary . The point of tangency is , where the circle is tangent to the constraint line.

At this point, the gradient of (pointing radially outward from the origin) is perpendicular to the level curve. The gradient of the constraint is , which points perpendicular to the constraint line. At the optimum, these two gradients areparallel: . This is the key geometric condition: at the optimal point under a binding constraint. The gradient of the objective is parallel to the gradient of the constraint. The multiplier is the scaling factor.

Takeaway.The constrained optimum occurs where the level sets of the objective just touch the constraint boundary — the gradients align.

13.6.6 Student Questions and Answers

Q.Does the Lagrangian always produce a smooth function? How do we know the penalty parameters are correct?

A.The Lagrangian itself is smooth . It is a sum of differentiable functions (the original objective plus the constraint terms, each multiplied by a constant). The penalty parameters are not known in advance. They emerge as part of the solution. Solving the Lagrangian involves finding both the optimal and the optimal simultaneously . You solve the system of equations together with thecomplementary slacknessconditions ( for each ). This leads to the min-max formulation and the Karush-Kuhn-Tucker (KKT) conditions, which will be explored in the next session. The KKT conditions are the necessary conditions for optimality in constrained optimization . They generalize the idea that "the gradient must be zero at the optimum" to problems with constraints.

Pitfalls.
  1. Forgetting to check constraint signs.When converting to form, the sign matters. becomes , NOT . Getting the sign wrong flips the multiplier's sign and leads to nonsensical solutions.
  2. Treating all constraints the same.Equality constraints () have unrestricted (can be positive or negative). Inequality constraints () require . If you get this wrong, the KKT conditions will not hold.
  3. Assuming the unconstrained optimum is always feasible.Always check: does the unconstrained solution satisfy all constraints? If yes, you are done — the constraints are inactive. If no, at least one constraint is binding.
  4. Confusing the Lagrangian with regularization.Both add penalty terms to the objective. But regularization penalizeslarge parameter valuesto prevent overfitting. The Lagrangian penalizesconstraint violations. The goals are different: regularization simplifies the model, Lagrangian enforces hard rules.
  5. Expecting a closed-form solution.Most real constrained optimization problems have no analytical solution. The Lagrangian formulation converts the problem into an unconstrained form that can be solvednumericallywith gradient-based methods. The fact that you wrote the Lagrangian does not mean you can solve it by hand . But you can now apply Adam or momentum to it.

13.6.7 Conceptual Connections

Recap + Bridge.Constrained optimization adds rules to the optimization problem: minimize subject to . The Lagrangian folds the constraints into the objective with penalty multipliers . The resulting min-max problem is equivalent to the original constrained problem. This connects to Lagrange duality, KKT conditions, and the min-max structure of game theory — topics covered in the next session.

The movieA Beautiful Minddramatizes the story of John Nash, whose Nobel-prize winning work on game theory connects to min-max theory. In constrained optimization, the Lagrangian formulation leads to a min-max problem. You minimize the Lagrangian with respect to . You maximize it with respect to . This duality — min in one variable, max in another . Mirrors the conceptual structure of Nash equilibrium (players simultaneously minimizing their own costs while opponents maximize them). The mathematical elegance of this connection makes constrained optimization one of the most rewarding topics in the course.

13.6.8 Real-World and Domain Connection

Constrained optimization powers mission-critical scheduling at major cloud providers. When Oracle or Microsoft schedules upgrades for thousands of enterprise customer systems, each customer has a unique set of constraints. Maintenance windows ("do not touch my servers between 9 AM and 5 PM"). Dependency chains ("upgrade the database before the application server"). Regulatory requirements ("data must stay in the EU"), and service-level agreements ("99.99% uptime during business hours"). The objective is to minimize total disruption — measured in downtime-minutes or revenue impact — subject to all these constraints. This is a massive constrained optimization problem with millions of variables. Cloud providers increasingly use AI agents for this: each agent represents a customer's requirements and negotiates with a central scheduler. The Lagrangian framework underlies the mathematical modeling of these systems. Even if the actual solving is done by specialized constraint solvers or reinforcement learning agents.

Beyond cloud scheduling, constrained optimization appears in portfolio optimization (maximize return subject to risk budget). Structural engineering (minimize weight subject to load-bearing constraints). Supply chain logistics (minimize shipping cost subject to delivery windows). And power grid management (minimize generation cost subject to demand and transmission limits). The Lagrangian method and KKT conditions are the mathematical language used across all these domains.

Exam Guidance Summary

Exam note — Mark distribution.Post-mid-semester content carries approximately 70% of the comprehensive exam weight. Pre-mid-semester content carries approximately 30%. If there are 8 problems on the exam, expect 5–6 from post-mid-sem and 2 from pre-mid-sem.

High-probability question types.

  • Expect one6-mark questionon any of the adaptive optimization algorithms — most likely Adagrad or RMSProp. You may be asked to write down two steps of the algorithm with numerical values (like the worked examples in sections 13.1 and 13.2). Momentum may also appear as a computational problem.
  • Constrained optimizationand the Lagrangian formulation are recurring exam topics. Be comfortable converting a constrained problem (with both equality and inequality constraints) into the Lagrangian form. Know how to handle the sign of each constraint when converting to form. The min-max interpretation is important.

Pre-mid-sem dependency.

Principal Component Analysis (PCA), covered in the companion document for this lecture block, relies on eigenvalues and eigenvectors. You must understand matrix structure, eigenvalues, and eigendecomposition from the pre-mid-sem material to grasp PCA. Do not skip the pre-mid-sem portion entirely — the 30% provides the mathematical foundations for the 70%. Specifically review: definitions of eigenvalues and eigenvectors, the characteristic equation , and the covariance matrix.

Exam note — Study strategy for adaptive methods.Focus on understandingwhyeach algorithm was invented — what problem of gradient descent it solves.
  • Adagrad.monotonic learning-rate decay per parameter using cumulative sum of squared gradients. Fixes the "one learning rate for all dimensions" problem.
  • RMSProp.replaces cumulative sum with weighted moving average of squared gradients. Fixes Adagrad's vanishing learning rate.
  • Adam.combines momentum (direction smoothing) with RMSProp (adaptive step size) plus bias correction. Fixes both oscillation and per-parameter scaling.
The formulas flow naturally from the motivation. If you understand the problem each algorithm solves, you can reconstruct the equations.

Exam note — Presentation matters.

Show your computations in a clear, step-by-step format.

  1. Write the formula first.
  2. Substitute the numbers.
  3. Show the result.
  4. Write your assumptions explicitly.

A well-organized answer with neatly labeled steps is easier to grade and earns more partial credit. For example, when asked to compute two steps of Adagrad.

          Step 1: Compute gradient at W_0. ∇L(W_0) = ... Step 2: Update A_1 = A_0 + (∇L(W_0))² = ... Step 3: Update W_1 = W_0 − (α/√A_1) ∇L(W_0) = ... Step 4: Compute gradient at W_1. ∇L(W_1) = ... Step 5: Update A_2 = A_1 + (∇L(W_1))² = ... Step 6: Update W_2 = W_1 − (α/√A_2) ∇L(W_1) = ...
          

Trial and error in practice.

When tuning gradient descent in real projects, people often start with one fixed learning rate (0.1 or 0.01). Observe the training error curve, and iteratively adjust. A loss curve that oscillates means is too high. A loss curve that drops slowly and plateaus means is too low. A loss curve that shoots upward means divergence — reduce significantly.

Tools like scikit-learn'sGridSearchCVautomate hyperparameter search by trying a predefined list of learning rates and selecting the best via cross-validation. You supply a grid like and the grid search trains a model for every combination. Returning the one with the lowest validation error. This is simple but computationally expensive — training models for candidate hyperparameters.

Bayesian optimization is a more sophisticated alternative. It builds a probabilistic model (usually a Gaussian process) of the validation loss as a function of hyperparameters. It chooses each new candidate by balancingexploration(trying regions with high uncertainty) againstexploitation(trying regions near known good values). Bayesian optimization typically finds good hyperparameters in far fewer trials than grid search. It is the preferred approach when each training run is expensive (e.g., training a large neural network on a GPU cluster).

Both methods are available in scikit-learn and specialized libraries like Optuna and Hyperopt. For exam purposes, you do not need to implement these — just know what they do and when to use which.

Exam note — Key formulas to memorize.
Algorithm Key Equation(s)
Gradient Descent
Momentum
Adagrad
RMSProp (same weight update as Adagrad)
Lagrangian ( for )

Key Industry Applications

Momentum ()

The combination of SGD with momentum and is nearly universal as the default optimizer in PyTorch, TensorFlow, and JAX. The friction parameter 0.9 is the empirically optimal value across most architectures, found through decades of experimentation. Notable models trained with SGD + momentum include.

  • AlexNet(2012): the breakthrough deep CNN for ImageNet, trained with momentum and learning rate .
  • ResNet(2015): the residual network architecture that won ImageNet, trained with SGD + momentum and a step-wise learning rate decay schedule.
  • VGG(2014): deep convolutional networks for visual recognition, all variants trained with momentum .

Interestingly, for state-of-the-art image classification, SGD + momentum still often beats Adam on final test accuracy . Momentum finds flatter minima that generalize better. The 2018 paper "Three Mechanisms of Weight Decay" (Loshchilov & Hutter) showed that decoupling weight decay from the optimizer (AdamW) can close this generalization gap.

Adam (Adaptive Moment Estimation)

Adam, introduced by Kingma & Ba (2014), is among the most widely used optimization algorithms in deep learning. It handles sparse gradients well, making it popular in NLP where word frequency distributions are highly skewed (Zipf's law). Key applications.

  • Transformers and LLMs.Every major language model from BERT to GPT-4 was trained with Adam or AdamW. The original Transformer paper used Adam with .
  • Generative models.Stable Diffusion, DALL-E, and other diffusion models use Adam/AdamW with and .
  • Reinforcement learning.Proximal Policy Optimization (PPO) and other RL algorithms often use Adam for policy gradient updates.

The standard hyperparameters () work well for most problems. Which is why Adam is the recommended "start here" optimizer in every deep learning framework's documentation.

Adaptive Learning Rates in NLP

Word embedding training (word2vec, GloVe, fastText) benefits from per-parameter learning rates because word frequency follows power-law distributions. The word "the" appears in roughly 7% of all English text, while "hippopotamus" may appear once per billion words. With a fixed learning rate.

  • Frequent words dominate the gradient signal, receiving abnormally large cumulative updates.
  • Rare words barely update at all, remaining near their random initialization.

Adaptive methods solve this: frequent words accumulate large gradient histories ( grows), reducing their effective learning rate to a trickle (). Rare words maintain their original learning rate (), allowing them to learn meaningful representations from their few occurrences. Google's original word2vec paper used Adagrad for precisely this reason. Modern embedding models use Adam for the same benefit with better long-training stability.

Constrained Optimization in Cloud Computing

Oracle, Microsoft, AWS, and Google Cloud schedule upgrades and maintenance for thousands of enterprise customers using constrained optimization. The problem structure.

  • Objective.Minimize total disruption (downtime-minutes × customer-importance-weight).
  • Constraints.Each customer's availability windows, dependency chains (service A must be upgraded before service B). Regulatory compliance (data sovereignty, GDPR), and SLA guarantees (e.g., 99.99% uptime).
  • Scale.Thousands of customers, millions of constraint variables.

This class of problem is increasingly delegated to AI agents — autonomous systems that negotiate schedules. Detect and resolve conflicts, and adapt when constraints change (e.g., a customer extends their maintenance window). The Lagrangian framework underlies the mathematical modeling, even when the actual solving uses mixed-integer programming solvers or reinforcement learning.

GridSearchCV (scikit-learn)

A brute-force hyperparameter tuning tool in scikit-learn. You supply a grid of values for each hyperparameter — e.g., and . The tool trains a model for every combination (4 × 5 = 20 combinations), evaluates each via cross-validation, and returns the best. Simple and guaranteed to find the best among the candidates, but computationally expensive — cost grows exponentially with the number of hyperparameters. For 5 hyperparameters with 5 values each, you need training runs. In practice, grid search is feasible for 1–3 hyperparameters with 3–5 values each. For larger searches, use random search (sample randomly from the grid) or Bayesian optimization.

Bayesian Optimization

A smarter alternative to grid search. Instead of trying every combination. Bayesian optimization builds a probabilistic surrogate model (typically a Gaussian Process) that predicts the validation loss for any hyperparameter setting. Along with an uncertainty estimate. It uses anacquisition functionto choose the next hyperparameter to try.

  • Exploration.Try points where the model is uncertain (high variance) — you might discover a great region you have not explored.
  • Exploitation.Try points near known good values (low predicted loss) — you refine the best solution you have found.

The acquisition function (commonly Expected Improvement or Upper Confidence Bound) balances these two. Each new training run updates the surrogate model, and the cycle repeats. Bayesian optimization typically finds good hyperparameters in 10–50 trials, versus hundreds or thousands for grid search. It is the preferred approach when each training run is expensive (hours on a GPU cluster). Libraries: scikit-optimize, Optuna, Hyperopt, BoTorch (Meta's Bayesian optimization library for PyTorch).

Agents and Autonomous Optimization

The next generation of ML engineering involves configuring and managing AI agents that perform optimization autonomously . Selecting architectures, tuning hyperparameters, scheduling training runs, and deploying models without human intervention. Understanding the mathematical foundations — gradient descent, momentum, adaptive methods. Constrained optimization — is essential for designing, debugging, and improving these agent systems. When an agent's training fails to converge, you need to know whether the problem is the optimizer (try Adam instead of SGD). The learning rate (too high → oscillation; too low → stalling), the constraints (check feasibility), or the architecture (gradient vanishing/exploding). The concepts in this lecture are the diagnostic toolkit for every ML practitioner.

MFML Lecture 13 notes · Gradient Descent Optimization

Mathematical Foundations for Machine Learning· postgraduate· 2026-07-11

Sections Breakdown

1Problems with Gradient Descent

The update rule and the three failure modes (stalling, zigzag oscillation, trapping/divergence) on ill-conditioned loss surfaces.

2Momentum-Based Gradient Descent

The velocity equation that remembers past gradients, smoothing the path and carrying through flat regions.

3Adagrad — Adaptive Gradient

Per-parameter learning rate via a cumulative sum of squared gradients, and its monotonic-decay weakness.

4RMSProp

Replaces the cumulative sum with a weighted moving average so the learning rate can adapt up or down.

5Adam — Adaptive Moment Estimation

Combines momentum (first moment) with RMSProp (second moment) into one optimizer.

6Constrained Optimization and Lagrangian

Folding constraints into the objective with Lagrange multipliers and the min-max formulation.

7Exam Guidance Summary

Mark distribution, high-probability question types, and key formulas to memorize.

8Key Industry Applications

Where these optimizers appear in production: vision, NLP, cloud scheduling, and hyperparameter search.

Postgraduate students in Machine Learning and related quantitative fields

Exam Revision Notes

Below is the distilled, exam-ready core of this lecture. Every entry is built from the full textbook notes above. Use this section for rapid review — but if something doesn't make sense, go back to the full explanation in the main content.

Problems with Plain Gradient Descent

Must-know: Plain gradient descent uses one learning rate for every direction. On an ill-conditioned surface (steep in one direction, flat in another) a single cannot work. Too small stalls on flat regions. Too large oscillates or diverges in steep directions. The condition number of the Hessian measures this steepness ratio.

\u26a0\ufe0f Top pitfall: Treating a flat region as the minimum. Both a flat plateau and a true minimum have . Watch the trajectory, not just the final loss. A stalled loss may mean you are stuck on a plateau, not converged.

Self-check: For the surface , why does stall while diverges?

Connects to: Momentum, Adagrad, RMSProp, Adam

Momentum-Based Gradient Descent

Must-know: Momentum adds a velocity term that remembers past gradients, smoothing the trajectory. The velocity is a blend of old velocity (weighted by ) and the current gradient (weighted by ). It dampens zigzag oscillation and carries you through flat plateaus. With it becomes plain gradient descent.

\u26a0\ufe0f Top pitfall: Forgetting the scaling on the first step. With , the first step is only of a plain gradient step. So momentum often needs a slightly larger or a warm-up schedule.

Self-check: Why does momentum keep moving through a flat region where plain gradient descent would stall?

Connects to: Problems with Plain Gradient Descent, Adam

Adagrad \u2014 Adaptive Gradient

Must-know: Adagrad gives each parameter its own effective learning rate by dividing by . Here is the running sum of past squared gradients. Steep directions accumulate large and get small steps. Flat directions keep small and get normal steps. Its weakness: only grows, so the learning rate decays to zero on long runs.

\u26a0\ufe0f Top pitfall: Using Adagrad for long training runs. Because accumulates forever, the effective learning rate vanishes and training stalls. Use RMSProp or Adam instead for deep networks.

Self-check: On the canyon example, why does the y-direction's effective learning rate drop far faster than the x-direction's?

Connects to: RMSProp, Adam, Problems with Plain Gradient Descent

RMSProp

Must-know: RMSProp fixes Adagrad's vanishing learning rate by replacing the cumulative sum with a weighted moving average of squared gradients. The update is . Because it is an average, can rise AND fall. So the learning rate adapts to the current gradient landscape instead of only decaying.

\u26a0\ufe0f Top pitfall: Confusing (RMSProp's squared-gradient decay) with (momentum's velocity decay). They are both near 0.9 but serve different purposes. Keep them distinct, especially inside Adam.

Self-check: In the 5-step worked example, why does RMSProp's decrease during small-gradient steps while Adagrad's does not?

Connects to: Adagrad, Adam

Adam \u2014 Adaptive Moment Estimation

Must-know: Adam combines momentum (first moment , direction smoothing) with RMSProp (second moment , adaptive step size). It is the default optimizer for most deep learning. The two decay rates differ on purpose. for the gradient average. for the squared-gradient average.

\u26a0\ufe0f Top pitfall: Assuming Adam is always best. It converges fast in training but can find sharper minima. Those generalize worse than SGD + momentum on vision tasks. Also, its default is an order of magnitude smaller than plain SGD's.

Self-check: What do the bias-correction terms and fix at the start of training?

Connects to: Momentum, RMSProp

Constrained Optimization and the Lagrangian

Must-know: Constrained optimization folds constraints into the objective with penalty multipliers . The Lagrangian turns a constrained problem into an unconstrained min-max. The form is . Inequality multipliers need . Equality multipliers can be any sign.

\u26a0\ufe0f Top pitfall: Getting the constraint sign wrong when converting to form. becomes , NOT . A flipped sign flips the multiplier's meaning and gives nonsense.

Self-check: In the bounded 2D example, why is and what does it tell you about relaxing the constraint ?

Connects to: Problems with Plain Gradient Descent, Momentum, Adam

Previous LectureLecture 12 · Challenges of Gradient Descent and Constrained Optimization
Next LectureLecture 14 · Gradient Descent Variants and Constrained Optimization

Was this lecture useful?

Loading comments…
🤖

BitsNotes AI Assistant

Subject Notes Assistant

Configure AI Chat

Choose how to access the chatbot
Have your own API key?

Switch to "Bring Your Own Key" tab above for unlimited access with any OpenAI-compatible provider.

🔑 Enter API key above to fetch live models from provider, or enter model name manually.
OpenAI-Compatible API Support

Choose any provider preset (Gemini, DeepSeek, Kimi, GLM, MiniMax, Qwen, OpenAI, Groq, Ollama, etc.) or enter a custom endpoint URL.

Security & Privacy First

Your API key is sent directly from your browser to your specified provider. BitsNotes servers never store or see your key.