Art of DS

Appearance

Palette
Theme
Unlock $20 one time
Sign in, or create an account

Sign in to Art of DSCreate your Art of DS account

We email you a link and a six-digit code. Either one signs you in; there is no password.Enter your email. We send a link and a six-digit code to confirm it — no password to remember. The account is free.

  • You come back to this page, right where you were.
  • Already have an account? The same email simply signs you in.

By continuing you accept the Terms and the Privacy policy.

Your account

Free accountLifetime access

difficulty 2/5 Free

Gradient

Definition

The vector of a function's partial derivatives — it points in the direction of steepest increase, and its length says how steep that increase is.

Formula
∇f(x)=[∂f∂x1,…,∂f∂xn] ⁣⊤\nabla f(x) = \left[\frac{\partial f}{\partial x_1}, \dots, \frac{\partial f}{\partial x_n}\right]^{\!\top}

Intuition

Stand on a hillside in fog. You cannot see the summit, but you can feel which way the ground rises most steeply under your feet, and how sharply.

That is the gradient: a direction and a magnitude. The direction is the way the function increases fastest from where you are standing; the magnitude is how fast. Everything in gradient-based optimisation follows from having that information locally, without ever seeing the whole landscape.

Formal definition

For a function of several variables, the gradient is the vector whose entries are the partial derivatives with respect to each variable — each one measuring how the output changes when that single input is nudged, holding the others fixed [2, §4.3].

The result is a vector in the same space as the input. A function of 10 million parameters has a gradient with 10 million components, which is exactly why training a large network is possible at all: one evaluation gives you a direction for every parameter simultaneously.

How it works

minimumparameter →loss
Gradient descent, one step at a time. Each step moves against the slope, and its length is proportional to how steep the slope is — so the steps are long where the curve is steep and shrink to almost nothing near the bottom. Nothing tells the algorithm it has arrived; it simply stops moving because the gradient has gone flat.

Two properties do all the work:

It points uphill. The gradient gives the direction of steepest ascent, so minimising means moving in the direction of -∇f. This is the source of the minus sign that appears in every update rule in this section [2, §4.3].

Its length measures steepness. On a steep slope the gradient is long; on a plateau it is near zero. That is why gradient descent with a fixed learning rate takes long steps on steep ground and short steps near a minimum: each step is the learning rate times the gradient, so nobody schedules that — the geometry does it [2, §4.3].

Where the gradient is exactly zero the function is flat: a minimum, a maximum, or a saddle point. The gradient alone cannot tell you which; the second derivative can [2, §4.3.1].

Where it's used

  • Training every neural network. Backpropagation is an efficient method for computing exactly this vector [1, §5.3.3].
  • Fitting many other models. Logistic regression and gradient boosting are fitted with gradients too.
  • Sensitivity analysis. The partial derivative for one input says how much the output responds to it.
  • Adversarial examples. Take the gradient of the loss with respect to the input rather than the parameters, and a tiny step in the direction of its sign can change the prediction [2, §7.13].

Why & when

Gradients are the reason large-scale machine learning works. They give a search direction at a cost comparable to one function evaluation, no matter how many parameters there are — which is what makes optimising millions of them tractable [2, §6.5.3].

They are not always available. A gradient needs the function to be differentiable. Discrete choices, hard thresholds and non-differentiable losses have no gradient at the point that matters, which is why decision trees are fitted by search rather than descent [3, §1.10.7], and why a network trained by gradients uses a sigmoid where a hard threshold would leave it with a gradient of zero [2, §6.2.2.2].

Worked example

Python 15 lines Show the code Hide the code
import numpy as np

def f(x):
    # a simple bowl, steeper along the second axis
    return x[0]**2 + 5 * x[1]**2

def grad(x):
    return np.array([2 * x[0], 10 * x[1]])

point = np.array([3.0, 1.0])
g = grad(point)

print(g)                     # [ 6. 10.] — steepest-ascent direction
print(np.linalg.norm(g))     # 11.66  — how steep it is here
print(-g / np.linalg.norm(g))  # unit vector pointing downhill

The second component is larger even though its coordinate is smaller, because the function rises five times faster along that axis. That imbalance is exactly what makes badly scaled problems hard to optimise — the gradient points mostly across the valley rather than along it.

Pitfalls

The gradient is local, and only local. It describes the surface at one point. It says nothing about whether a minimum lies ahead or how far away it is.

Zero gradient does not mean minimum. Saddle points also have zero gradient, and in high dimensions they are more likely than minima [1, §12.1.2.2].

Scale changes the direction. Rescale one feature and the gradient rotates. This is not a numerical artefact — it is why feature scaling changes how fast gradient descent converges, and sometimes whether it converges at all.

A large gradient is not necessarily a large step. How far you actually move depends on the learning rate, which is a separate decision entirely.

Not read yetReach the end of the page and it counts as read.

Keep your progressWith lifetime access, every page you finish is marked and you pick up where you stopped, on all your devices.

Unlock

Check yourself

3 questions

Each answer is explained, with the section it tests and its source.

Sources

  1. Zhang, Lipton, Li, Smola. Dive into Deep Learningbook · retrieved 2026-08-31https://d2l.ai/
  2. Goodfellow, Bengio, Courville. Deep Learning (2016)bookhttps://www.deeplearningbook.org/
  3. scikit-learn User Guide — Decision Treesdocs · retrieved 2026-10-04https://scikit-learn.org/stable/modules/tree.html

Have a promo code?

A code that takes 100% off unlocks everything at once. A smaller one takes its share off the price in the payment window: enter the same code there.

Write to support

A question, a problem with access, a refund? Write to us and we answer by email.

What is it about?

Your address is used only to answer you.

Message sent

Thank you. We will reply to the address you gave, by email.

Or write to us directly

support@artofds.com

Message sent Thank you. We will reply to the address you gave, by email.

Check the email address: we need it to reply.

That is our own address. Enter yours, so the reply reaches you.

This address cannot receive email. Check it for a typo.

Write a little more: at least 10 characters.

Too many messages in a short time. Try again in a few minutes.

The message could not be sent. Please write to us directly: support@artofds.com