Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Calculating Gradients of Multivariable Functions

Hello! Welcome back to our course on the mathematical foundations of AI.

Introduction

In our previous lesson, we established how to compute partial derivatives, which measure the rate of change of a multivariable function along the directions of the coordinate axes. This was like finding the slope of a hill, but only when walking strictly east or strictly north.

This is a good start, but in machine learning, we're often interested in a more powerful question: from our current position, what is the single best direction to move in to achieve the fastest increase (or decrease) in our function's value? Answering this is the key to optimizing complex models.

Today, we will address this by learning to compute the gradient of a scalar-valued multivariable function. We will see that the gradient is a vector that elegantly packages all the partial derivatives of a function into a single object. This vector not only tells us the rate of change in all axial directions at once but, more importantly, points in the direction of the steepest ascent of the function.

Defining the Gradient

The concept of the gradient builds directly on what we learned about partial derivatives. For a scalar-valued function of variables, , the gradient is simply the vector whose components are the partial derivatives of with respect to each variable.

Given your linear algebra background from Module 1, you'll recognize this as a column vector. The gradient of is denoted by (pronounced "nabla f" or "del f").

Formal Definition:
Let be a differentiable scalar-valued function with variables . The gradient of is the vector-valued function:

Note that the gradient takes a point in (like a specific set of model parameters) and returns a vector in (the direction of steepest ascent at that point).

For a function of two variables, , the gradient is:

Computing the Gradient

The process of computing the gradient is straightforward:

  1. Identify all the independent variables of the function.
  2. Compute the partial derivative of the function with respect to each variable, as you learned in the previous lesson.
  3. Arrange these partial derivatives into a vector.

Let's watch a video that illustrates this computational process.

Mathematics for Machine Learning: What is Gradient?

This video from Dr. Data Science explains what the gradient is and walks through the process of computing it for a simple multivariable function. It's a great practical demonstration of the definition.

Watch from 02:22 to 09:01. Focus on how the gradient is constructed by first calculating the partial derivatives with respect to x1 and x2, and then assembling them into a single vector.

As the video showed, for :

So, the gradient is:

At a specific point, say , the gradient vector would be .

Test your understanding!

Let . Compute the gradient of , .

Show answer

First, we compute the three partial derivatives:

Now, we assemble them into the gradient vector:

The Geometric Meaning of the Gradient

Computing the gradient is just mechanics. The reason it's a cornerstone of machine learning lies in its geometric interpretation. To understand this, we first need to introduce the concept of a directional derivative.

The partial derivatives and tell us the rate of change in the directions of the unit vectors and . The directional derivative, , generalizes this to find the rate of change in the direction of any unit vector .

A remarkable result from calculus is that the directional derivative can be computed elegantly using the gradient. For a differentiable function and a unit vector , the directional derivative of in the direction of is given by the dot product of the gradient and :

This single equation is packed with meaning. To fully appreciate its derivation and implications, we'll consult a rigorous textual explanation.

2.7: Directional Derivatives and the Gradient

The following text, 'Directional Derivatives and the Gradient' from the CLP-3 Multivariable Calculus textbook, provides a formal derivation of the relationship between the directional derivative and the gradient. Your request for mathematical rigor will be well-served here.

Please read the following parts: The introduction and derivation from the start down to Definition 2.7.1. Focus on how the chain rule is used to derive the formula rate of change = ∇f ⋅ v. Definition 2.7.1 itself, which formally defines the gradient. Example 2.7.6, which provides a concrete computation of a gradient and a directional derivative. This reading will solidify the formal connection between the concepts.

Now, let's unpack the implications of the formula . Recall from linear algebra that the dot product can also be written as , where is the angle between the vectors and .

Applying this to the directional derivative:

(since ).

This tells us everything we need to know:

  • Maximum Increase: The rate of change is maximized when , which happens when . This means the direction points in the same direction as the gradient . The value of this maximum rate of change is .
  • Maximum Decrease: The rate of change is minimized (most negative) when , which happens when . This means points in the opposite direction of the gradient, . The value of this maximum rate of decrease is .
  • Zero Change: The rate of change is zero when , which happens when . This means is orthogonal (perpendicular) to the gradient . This direction is tangent to the level curve of the function at that point.

In summary:

The gradient vector at a point points in the direction of the steepest ascent of the function at , and its magnitude is the rate of that ascent.

Gradient Vector on a 3D Surface
This image visualizes the gradient. For the given surface, the red dot marks the point (1,1) in the xy-plane. The red arrow represents the gradient vector \(\nabla f(1,1)\), pointing in the direction of the steepest uphill climb on the surface from that point.

Connection to Machine Learning: Gradient Descent

This brings us to the core application in machine learning. Most ML training involves minimizing a cost function (or loss function), often denoted , where is the vector of model parameters. We want to find the parameters that make the cost as low as possible.

The gradient, , tells us the direction to change to increase the cost the fastest. Therefore, the negative gradient, , points in the direction of the steepest decrease in cost.

The gradient descent algorithm is built on this exact principle:

  1. Start with some random initial parameters .
  2. Calculate the negative gradient of the cost function, .
  3. Update the parameters by taking a small step in that direction: , where is a small positive number called the learning rate.
  4. Repeat steps 2 and 3 until the cost converges to a minimum.

Every time you train a neural network using libraries like PyTorch or TensorFlow, this is fundamentally what's happening under the hood during the optimization step.

Conclusion

In this lesson, we have defined the gradient and established its critical role in multivariate calculus and machine learning.

Key Takeaways:

  • The gradient, , is a vector containing all the partial derivatives of a scalar-valued function .
  • To compute the gradient, you calculate each partial derivative individually and assemble them into a vector.
  • The directional derivative gives the rate of change of in any direction .
  • The gradient's direction is the direction of the function's steepest ascent, and its magnitude is the rate of that ascent.
  • The negative gradient, , points in the direction of steepest descent, forming the basis for the gradient descent optimization algorithm.

Preview of the Next Lesson:
So far, we've dealt with relatively simple functions. In machine learning, especially in deep learning, functions are often complex compositions of other functions (e.g., an activation function applied to a linear transformation). To find the gradient of such a composite function, we need a multivariable version of a familiar tool: the chain rule. In the next lesson, we will derive the multivariable chain rule and see how it allows us to compute gradients for these more complex, nested functions.

Can't find a good explanation? Sign up and we'll make it for you

Sign up