B3 Calculus Essentials

The Gradient Vector

Arrow pointing uphill on 2D surface in 3D view. Show why negative gradient = steepest descent.

1. From Individual Partials to a Vector

In the last section, we learned how to measure slope in individual directions. The partial derivative ∂f/∂x tells you how steep the surface is in the x-direction; ∂f/∂y tells you the steepness in the y-direction. Each gives you valuable information, but separately they're like having half a map.

Here's the natural next question: what if we want to know the steepest direction overall—not just along the x-axis or y-axis, but in any direction?

That object is the gradient vector, and it's one of the most important concepts in all of machine learning.

The idea is simple: take all your partial derivatives and package them as the components of a vector. For a function f(x, y), the gradient is:

∇f = (∂f/∂x, ∂f/∂y)

That's it. The gradient is just a vector whose components are the partial derivatives. Together, they encode everything about how the function changes in any direction from the current point.

2. Gradient Notation

You'll see the gradient written several ways:

  • ∇f — The most common notation. The symbol ∇ is called "nabla" or "del." Read ∇f as "del f" or "the gradient of f."
  • grad(f) — Spelled out explicitly. Some textbooks prefer this for clarity.
  • ∇f(x, y) — Emphasizes that the gradient depends on where you are.

For a function of two variables: ∇f = (∂f/∂x, ∂f/∂y)

For a function of n variables: ∇f = (∂f/∂x₁, ∂f/∂x₂, ..., ∂f/∂xₙ)

The pattern is always the same: one component for each input variable, each component being the partial derivative with respect to that variable.

3. The Key Property: Direction of Steepest Increase

Here's the remarkable fact that makes the gradient so powerful:

The gradient vector points in the direction of steepest increase of the function.

Imagine you're hiking on a hilly terrain and you want to climb as quickly as possible. You could walk north, south, east, west, or any diagonal. Each direction has a different slope. The gradient tells you: "If you want to gain elevation as fast as possible, walk this way."

Why does this work? The component ∂f/∂x measures how much the function increases when you step in the x-direction. The component ∂f/∂y measures the increase when you step in the y-direction. When you combine these into a vector, you get the optimal combination for maximum increase.

In your visualization, you'll see this as an arrow lying on the xy-plane, pointing in the direction where the surface rises most steeply. Drag the point around and watch the arrow rotate—it always swings to point "uphill."

4. Gradient Magnitude: How Steep Is Steep?

The gradient isn't just a direction—it's a vector with both direction and magnitude. The magnitude tells you how steep the steepest direction is.

||∇f|| = √[(∂f/∂x)² + (∂f/∂y)²]

A larger magnitude means a steeper slope; a smaller magnitude means a gentler slope.

Consider what happens at different points:

  • At a hilltop or valley bottom: Both partial derivatives are zero, so the gradient is the zero vector (0, 0). The magnitude is zero.
  • On a gentle slope: The partial derivatives are small, so the gradient vector is short.
  • On a steep cliff: The partial derivatives are large, so the gradient vector is long.

In your visualization, the length of the gradient arrow changes as you move across the surface. Near the peak of a hill, the arrow shrinks to nothing. On steep flanks, the arrow grows long.

5. Negative Gradient: Flip the Arrow to Go Downhill

Here's where things get practical for machine learning.

If the gradient points in the direction of steepest increase, then the negative gradient points in the direction of steepest decrease:

-∇f = (-∂f/∂x, -∂f/∂y)

Just flip the arrow. Where the gradient says "go this way to climb," the negative gradient says "go this way to descend."

Why do we care about going downhill? Because in machine learning, we want to minimize a loss function. We're not trying to climb to the peak—we're trying to descend into the valley where the loss is lowest. The negative gradient is our guide down.

Think of it like water flowing downhill. Water doesn't think; it just follows the steepest descent from wherever it is. Gradient descent algorithms mimic this natural behavior.

6. Gradient in the xy-Plane

Here's a subtle but important point that often confuses beginners: the gradient is a vector in the input space, not a vector pointing "up" the surface.

For f(x, y), the gradient ∇f = (∂f/∂x, ∂f/∂y) is a 2D vector with x and y components. It lies flat in the xy-plane. It doesn't have a z-component.

The gradient tells you which direction to move in the input space to achieve the steepest increase in the output. It answers: "Should I increase x? Decrease y? Some combination?"

In your 3D visualization, you'll see the gradient arrow lying on the horizontal plane beneath the surface, not tilting up along the surface itself. This is correct!

7. Why This Matters for Machine Learning

Now let's connect everything to neural network training.

When you train a neural network, you have a loss function L that measures how wrong the network's predictions are. This loss depends on all the network's weights: L(w₁, w₂, ..., wₙ). You can think of this as a surface—an incredibly high-dimensional surface that you can't visualize, but which has the same mathematical properties as our 3D surfaces.

Your goal: Find the weights that minimize the loss.

The problem: You can't see the whole surface. You're standing at one point (your current weights) and can only measure local information—specifically, the gradient.

The solution: Use the gradient as a compass. The gradient ∇L points uphill (toward higher loss). The negative gradient -∇L points downhill (toward lower loss). Take a step in the negative gradient direction, and you'll reduce the loss.

This is gradient descent in its purest form:

weights_new = weights_old - learning_rate × ∇L

Each iteration: compute the gradient, subtract a small multiple of it from the current weights, repeat until the loss stops decreasing.

8. Gradient for N Variables

For a neural network with millions of weights:

∇L = (∂L/∂w₁, ∂L/∂w₂, ∂L/∂w₃, ..., ∂L/∂wₙ)

The gradient is now a vector with millions of components—one for each weight. You can't visualize a million-dimensional arrow, but mathematically it behaves exactly like our 2D gradient:

  • It points in the direction of steepest increase of L
  • Its magnitude indicates how steep that direction is
  • The negative gradient points toward lower loss
  • Each component tells you how to adjust one specific weight

The update rule looks exactly the same: wᵢ_new = wᵢ_old - learning_rate × ∂L/∂wᵢ

9. Computing Gradients: Enter Backpropagation

Computing each partial derivative from scratch would require evaluating the loss function multiple times per weight—astronomically expensive for millions of weights.

Backpropagation solves this problem brilliantly. It computes all partial derivatives in essentially two passes through the network:

  1. Forward pass: Compute the loss normally, but save intermediate values
  2. Backward pass: Starting from the loss, propagate gradient information backward through each layer using the chain rule

The result: instead of millions of forward passes, we need one forward pass and one backward pass. This makes gradient descent practical for modern neural networks with billions of parameters.

Every time you call loss.backward() in PyTorch or let TensorFlow's autodiff do its work, backpropagation is computing the gradient—the full vector of all partial derivatives—efficiently.

Wrapping Up

The gradient is where partial derivatives become truly powerful. By packaging all the individual slopes into a single vector, we get something remarkable: a compass that always points toward steeper terrain.

For optimization, we flip that compass. The negative gradient points downhill, and gradient descent follows it step by step toward lower loss. This simple idea—compute the gradient, step opposite to it, repeat—is the engine that trains virtually every neural network in existence.

In your visualization, watch the gradient arrow as you move across the surface. See how it always points toward higher ground, and how its length reflects the steepness. That arrow, generalized to millions of dimensions, is what guides a neural network from random initialization to useful predictions.

The gradient isn't just a mathematical curiosity—it's the steering wheel of machine learning.