Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 30

The Gradient Vector and Steepest Ascent

Recommended resources

Introduction

In our previous lesson, we established how to measure the slope of a high-dimensional loss landscape, but only in the specific directions parallel to the parameter axes. We did this by computing partial derivatives, which involved "slicing" the landscape and finding the slope of the resulting 2D curve. This gave us the sensitivity of the loss function to each parameter individually.

However, the path of steepest descent—the most efficient way to get to a minimum—is rarely aligned perfectly with any single axis. To find this optimal path, we need a "compass" that points directly uphill at any location on our landscape. In this lesson, we will construct this compass. We will assemble the individual partial derivatives into a single, powerful object: the gradient vector. You will learn not just what the gradient is, but also the geometric reason why it always points in the direction of steepest ascent, a cornerstone concept that powers the most fundamental optimization algorithms in machine learning.


From Partial Derivatives to the Gradient Vector

In the last lesson, for a function , we calculated two separate quantities: and . These tell us the rate of change in the x and y directions, respectively. The gradient simply bundles these individual rates of change into a vector.

For a function with inputs , its gradient, denoted by (pronounced "del f"), is the vector of all its partial derivatives:

The gradient is a vector-valued function: you plug in a point (a set of specific parameter values), and it outputs a vector that describes the local slope at that point.

For instance, consider the function .

The gradient is . If we evaluate this at the point , we get the specific vector . This vector tells us that at this point, the slope is entirely in the x-direction.


The Gradient and the Direction of Steepest Ascent

The most important property of the gradient is that it points in the direction of the steepest ascent of the function. If you are standing on the loss landscape, the gradient vector is your compass needle pointing straight uphill. Consequently, the negative gradient points straight downhill, giving us the direction for gradient descent.

The following video from 3Blue1Brown provides a masterful visual intuition for this concept, connecting the abstract idea of a high-dimensional cost function to the simple analogy of a ball rolling down a hill.

Gradient descent, how neural networks learn Chapter 2, Deep learning

This video from the "Deep Learning" series by 3Blue1Brown explains gradient descent by first building the intuition for the gradient itself.

Watch the following segments to grasp the core idea: The 1D case: This section reviews the idea of using the slope (derivative) to find a minimum in a single-variable function. The 2D case: Here, the concept is extended to a 2D input space. Pay close attention to how the "slope" is replaced by the gradient vector, which points in the direction of steepest ascent, and how its negative points "downhill".

As the video explains, if our goal is to minimize a cost function (like finding the bottom of the valley), we repeatedly take small steps in the direction opposite to the gradient. This process is called gradient descent.

As shown here, the gradient at a point in the input space (bottom plane) points in the direction that corresponds to the steepest uphill path on the function's surface (top surface). To minimize the function, we would move in the opposite direction.

Why Does the Gradient Point Uphill? The Role of the Dot Product

We've established the geometric meaning of the gradient, but why is it true? The explanation lies in the connection between the gradient and the directional derivative, which measures the rate of change in any arbitrary direction.

The rate of change of a function at a point in the direction of a unit vector is given by the directional derivative, . The formula that connects all our concepts is:

This states that the rate of change in any direction is simply the dot product of the gradient vector at that point and the direction vector .

The following video explains this relationship and uses it to prove why the gradient must point in the direction of steepest ascent.

Why the gradient is the direction of steepest ascent

This Khan Academy video provides a clear and concise proof for why the gradient points in the direction of steepest ascent.

Watch the video starting from this section. Focus on how Sal Khan uses the geometric definition of the dot product to solve the problem. The key steps are: Framing the problem as maximizing the directional derivative, \nabla f \cdot \mathbf{u}. Recalling the geometric definition of the dot product: \mathbf{a} \cdot \mathbf{b} = \|\mathbf{a}\| \|\mathbf{b}\| \cos(\theta). Applying this to the directional derivative: \nabla f \cdot \mathbf{u} = \|\nabla f\| \|\mathbf{u}\| \cos(\theta). Realizing that since \mathbf{u} is a unit vector (\|\mathbf{u}\| = 1), the expression simplifies to \|\nabla f\| \cos(\theta). Concluding that to maximize this value, \cos(\theta) must be 1, which happens when the angle \theta between \nabla f and \mathbf{u} is 0. This means \mathbf{u} must point in the same direction as \nabla f.

This elegant proof reveals two profound facts:

  1. Direction of Steepest Ascent: The gradient vector points in the direction you should move to increase the function's value most rapidly.
  2. Magnitude of Steepest Ascent: The magnitude of the gradient vector, , is the actual slope (rate of change) in that steepest direction.

The image below shows the gradient vector on a tangent plane. You can see it's composed of its partial derivative components, and , which point along the axes. The resultant vector points in the direction that maximizes the "uphill" slope on the blue surface.

The gradient vector \(\nabla z\) is the vector sum of the partial derivative components. It lies on the tangent plane at a point and indicates the direction of steepest ascent on the function's surface.

For a more rigorous justification and a clever analogy, you can explore the following article.

Directional derivatives (going deeper) (article) - Khan Academy

This Khan Academy article formalizes the argument from the video and offers another way to think about it.

You can read the sections "Why does the gradient point in the direction of steepest ascent?" and the puzzle analogy. The puzzle about maximizing money with a constraint on the sum of squares is perfectly analogous to maximizing the directional derivative (a dot product) subject to the constraint that the direction vector has a length of 1.


The Gradient in Machine Learning: A Vector of "Relative Importance"

Let's bring this back to your goal of understanding how ML models work. Think of the cost function , where is the huge vector containing all 13,000+ weights and biases of a neural network.

The gradient is a vector with 13,000+ components. The i-th component, , tells you how sensitive the cost is to a small change in the i-th parameter. But the full vector does more: it tells you the single "direction" in the 13,000-dimensional parameter space that will increase the cost the fastest.

Therefore, the negative gradient, , is the direction that causes the most rapid decrease in cost. Each component of this vector tells you how to nudge the corresponding weight or bias. The relative magnitudes of the components tell you which parameters are most "important" to change in the current step to get the biggest reduction in error—they have the most "bang for your buck." This is the essence of what a network learns during training.

3Blue1Brown - Gradient descent, how neural networks learn

This section of the 3Blue1Brown article provides a non-spatial, highly intuitive way to think about the gradient in the context of a high-dimensional neural network.

Please read the section titled "Another Way to Think About The Gradient". Focus on the idea that the gradient encodes the relative importance of each weight and bias for reducing the cost.


Conclusion

In this lesson, we've unified the concept of partial derivatives into the single, powerful gradient vector. You now have the "compass" for navigating high-dimensional loss functions.

Key Takeaways:

  • The gradient vector, , is a vector containing all the partial derivatives of the function .
  • Geometrically, at a point points in the direction of the steepest ascent of the function.
  • The magnitude of the gradient, , represents the slope in that steepest direction.
  • This property arises from the relationship , which is maximized when the direction vector is aligned with the gradient .
  • In machine learning, the negative gradient points in the direction of steepest descent of the cost function, providing the optimal direction for each step in the gradient descent algorithm.

We've now defined the gradient and understood its geometric significance. In our next lesson, we will put this tool to work. We'll start by learning to compute the directional derivative in any arbitrary direction, and then we will derive and interpret the gradient for a cornerstone model in machine learning: linear regression with the mean squared error loss function.