1.2.4 · D2Calculus & Optimization Basics

Visual walkthrough — Gradients and directional derivatives

2,509 words11 min readBack to topic

Step 1 — The hill, the spot, and the question

WHAT. We have a landscape. Picture it first, then we name things.

PICTURE. Below, the wavy sheet is a hill. The black dot on the sheet is where we stand; the black dot on the flat floor beneath it is our ground position. Look at the two floor axes: one runs east, one runs north — a ground position is just "how far east, how far north."

Now that the picture is in front of you, we name what it shows. Give every ground position a pair of numbers — the east amount and the north amount you just saw on the axes. Above each such position the hill has a height, and we call that height : the letter is a machine — feed it a floor position, it hands back the height of the sheet there. The floor dot in the figure is the fixed position we stand over; we name it . The bold symbol (bold = "this is a pair of numbers, an arrow-able thing", not a single number) simply means "the spot we stand at."

WHY. Machine learning trains by rolling downhill on an error landscape, so the very first thing we need is a precise way to say "how high am I, and how does that height change as I move?" Everything else is answering that.


Step 2 — Pick a walking direction

WHAT. From the spot we choose a way to walk. A direction on the floor is itself a pair of numbers : " east and north for each unit you step." We insist the arrow has length exactly 1. Length of is written (the double bars mean "how long is this arrow"), and we compute it by Pythagoras: . We demand .

WHY the length-1 rule. We want a slope per one metre walked. If our step-arrow were 2 metres long, every change would look twice as big — we'd be measuring "how far" tangled together with "how steep". Fixing the length to strips out "how far" and leaves pure steepness. This single choice is the reason a certain factor drops out later.

PICTURE. The red arrow is , drawn on the floor, exactly one unit long. The dashed circle shows all allowed directions — every red arrow we could pick has its tip on this circle.


Step 3 — Turn the 2-D walk into a 1-D walk: the slice

WHAT. As we walk, let be how many metres we have travelled ( at the start). Our floor position after walking metres is which in plain coordinates means and . Now define a brand-new one-input machine: So says: "give me metres walked , I give back the height there."

WHY. A hill is a hard 2-D object. But walking a straight line on it is a simple 1-D story: height versus distance, like a cross-section of the hill. We already know how to differentiate ordinary one-variable functions, so we shrink the hard problem to an easy one we can attack.

PICTURE. Left: the floor with the straight red walking line . Right: the same walk seen as a curve — horizontal axis is (metres walked), vertical axis is (height). The slope of this red curve at the start, , is the number we want. That slope is written — the prime means "rate of change," and the means "measured right at the starting spot."


Step 4 — Why partial derivatives are the natural pieces

WHAT. A partial derivative asks a narrower question: "if I nudge only east and freeze north, how fast does height change?" We write it (the curly is a "d" that whispers other variables held constant). Likewise freezes east and nudges north.

WHY. Our walk mixes east-motion and north-motion together. To handle the mix we first measure each pure ingredient — the east-slope and the north-slope — then combine them. Partials are exactly those two pure ingredients.

PICTURE. Two thin cross-sections through our spot: one cutting purely east–west (its slope is ), one purely north–south (slope ). The red tangent segment on each shows the slope that partial measures.


Step 5 — The chain rule stitches the ingredients together

WHAT. We need , and is "a function inside a function." The tool for differentiating a function-of-a-function is the Chain rule. It says: the total rate of change is the sum, over each coordinate, of (how fast changes with that coordinate) times (how fast that coordinate changes with ). Because our position moves as grows, each partial must be read at the current point — that is where we are right now, not back at the start:

WHY this tool and not another. No single-variable rule handles two moving inputs at once. The chain rule is precisely the rule for "inner things moving, outer thing responding." It is the only tool that lets the east-slope and north-slope contribute simultaneously.

Now the two inner rates are trivial, because changes with at the constant rate , and at rate : Substitute:

Term by term: is the east-slope; is how much of your step is east; their product is the height gained from the eastward part of the step. The second term is the same story going north. Add them — total height gained per metre.

We only want the slope at the start, so set . Then the current point is exactly our spot , i.e. , and the partials are evaluated at :

PICTURE. The step arrow split into its east piece and north piece ; each piece is multiplied by its matching slope, and the two contributions stack up to the total.


Step 6 — Recognize the pattern: it's a dot product

WHAT. Collect the two slopes into one arrow, the gradient: The upside-down triangle ("nabla") just means "the vector of all the partials." Then the sum from Step 5 is exactly the dot product of two arrows — multiply matching components, add:

WHY name it a dot product. Because the dot product has a geometric meaning we can exploit in the next step. Rewriting the plain sum as isn't new maths — it's re-reading the same number so its geometry becomes visible.

PICTURE. Two arrows on the floor — the gradient (red) and the step (black) — with the dot-product recipe "same-direction pieces multiply and add" annotated.


Step 7 — The geometry: reveals steepest ascent

WHAT. Every dot product also equals "length times length times the cosine of the angle between the arrows": Here is the angle between our step and the gradient, and is a dial running from (arrows aligned) down to (arrows opposite). Since we forced , that factor vanishes:

WHY this form. It isolates the only thing we control — the angle . The steepness we experience is just the fixed number scaled by the dial .

PICTURE. The gradient fixed; the red step arrow swept around a circle; a side gauge shows and how the resulting slope grows to a max at .

Read the three landmark cases straight off the dial:

angle meaning
walk along → steepest ascent
walk opposite → steepest descent (this is , used by Gradient descent)
walk perpendicular → height unchanged → along a contour

Step 8 — The degenerate cases (never left guessing)

WHAT & WHY. A careful derivation must survive the awkward inputs. Three matter here.

  1. Along a contour (). Then : the ground stays level. This is why is perpendicular to the level curves / contours — the only directions giving zero slope are the ones at a right angle to the gradient.
  2. A flat spot: . If both partials are zero (a peak, a valley bottom, or a saddle), then , so for every direction. There is no "uphill" — a summit, or the very minimum Gradient descent is chasing. The "steepest ascent direction" is undefined precisely because all directions tie at zero.
  3. A non-unit step (the classic slip). If you forget and use an arrow with , the factor no longer disappears; — a slope inflated by the arrow length. The number stops meaning "per metre." Always divide by the length first.

PICTURE. Left: circular contours with the red gradient stabbing across them at right angles. Middle: a flat summit where the gradient arrow shrinks to a dot. Right: a length-2 arrow with its inflated (wrong) slope crossed out beside the corrected unit version.


The one-picture summary

Everything compressed: the floor with our spot , the red gradient arrow, a fan of candidate step directions, and beside each the height it buys — biggest along the gradient, zero across it, most-negative opposite it. This single image is .

Recall Feynman retelling (plain words)

You're standing on a bumpy hill. You want to know how steeply the ground rises if you take a step in some chosen direction. First you measure two simple things: how steep it is if you step purely east, and how steep if you step purely north. Those two numbers, bundled into one arrow, are the gradient. Now any real step is part-east, part-north; the chain rule just says "take the east-steepness times your east-part, plus north-steepness times your north-part." That total is the gradient arrow "dotted" with your step arrow. And a dot product is secretly "how much your step lines up with the gradient." So: step exactly along the gradient → maximum climb; step sideways to it → you stay level (that's a contour ring); step straight against it → fastest way down, which is exactly the direction machine-learning training walks. Keep your step arrow one metre long so every number means "steepness per metre." That's the whole story.

Connections