Discover: one line that fools a classifier
In 2014 three researchers at Google showed that you can make an image classifier fail by changing each pixel so slightly that no human would notice. Here is the whole recipe:
Spotted in the wild
Add to a photo of a panda, with , and a network that was 57.7% sure it saw a panda becomes 99.3% sure it sees a gibbon. The picture looks unchanged.
You can already read parts of this line. Bold letters are vectors (Chamber 1), is a function of three arguments (Chamber 4), and is a small number. The new piece is the upside-down triangle, , with a subscript. Before learning what it means, make a guess:
Take a guess before reading on. In , the paper defines as the model's parameters, as the input image and as its label. Which of them is the equation about to change?
is the notation of calculus, the mathematics of change. Every model you'll ever train is trained with it, and papers use it on almost every page. This chamber teaches you to read it, along with its relatives: , , , , and . It is not a calculus course. You'll derive one derivative by hand, and after that the notation is the point.
Learn: from Δ to d
Change starts with a difference. If moves from to , the change in is , read “delta x”, with capital delta for “difference” (Chamber 1). If , the change in is . Their ratio is rise over run:
Geometrically, it's the slope of the straight line through two points of the curve, a line called a secant. If gives a car's position at time , it's the average speed over the interval.
But what is the speed at the instant ? Setting gives , which means nothing. The way out is a limit:
read “the limit, as h tends to zero, of …”. It asks what the ratio gets closer and closer to as shrinks, while itself is never zero. When the change is shrunk in this way, becomes : is an infinitesimally small change in , and is what becomes in the limit.
- “delta x”A finite change in : new value minus old value. Capital delta means “difference” (Chamber 1).
- “delta y over delta x”Rise over run: the slope of the straight line (the secant) through two points of a curve.
- “the limit as h tends to zero”The value that the expression after it approaches as gets closer and closer to , without ever being set to .
- “d x”What becomes in the limit: an infinitesimally small change in . Thompson's Calculus Made Easy calls it “a little bit of ”.
| Symbol | Say it | Meaning | LaTeX |
|---|---|---|---|
| “delta x” | A finite change in : new value minus old value. Capital delta means “difference” (Chamber 1). | ||
| “delta y over delta x” | Rise over run: the slope of the straight line (the secant) through two points of a curve. | ||
| “the limit as h tends to zero” | The value that the expression after it approaches as gets closer and closer to , without ever being set to . | ||
| “d x” | What becomes in the limit: an infinitesimally small change in . Thompson's Calculus Made Easy calls it “a little bit of ”. |
Learn: the derivative, from the limit
The derivative of at is that limit:
Here is the one derivation this chamber does by hand, for . Watch each step, because the same moves work for any polynomial.
Step (1) substitutes , (2) expands the square, (3) cancels the two terms, and (4) divides by . So the secant slope is exactly , for every . As the vanishes and stays put:
Two details matter. The division by is legal because a limit never sets . And the leftover, itself, is the error of the secant: at with the secant slope is while the true derivative is .
Let . What is the slope of the secant between and , with ?
Now do it with your own hands. Shrink and watch the secant turn into the tangent, the line that just touches the curve with slope . Get within at three points to clear the challenge, then switch on floating-point mode for a surprise.
Interactive lab
From secant to tangent
Drag h towards zero and watch the gold secant swing onto the teal tangent.
Points within 0.001 (at least 0.25 apart)
- ?
- ?
- ?
Learn: four ways to write a derivative
Calculus was invented twice, by Newton and by Leibniz, and it has carried several notations ever since. You'll meet all of them, sometimes in the same paper:
- “d y by d x”Leibniz's notation for the derivative of with respect to : the limit of . It often behaves like a fraction, but it is one symbol.
- “f prime of x”Lagrange's notation: is the derivative of , a new function, and is its value at .
- “d by d x”An operator: “take the derivative, with respect to , of whatever follows”.
- “f double prime of x”The second derivative, the derivative of the derivative. It measures how the slope itself changes: curvature. Leibniz writes it .
- “d f by d x, evaluated at a”The derivative, evaluated at the point . The same number as .
- “the chain rule”Differentiating a function of a function: multiply the rates. If depends on and on , the rates chain together. Backpropagation is this rule, applied over and over.
| Symbol | Say it | Meaning | LaTeX |
|---|---|---|---|
| “d y by d x” | Leibniz's notation for the derivative of with respect to : the limit of . It often behaves like a fraction, but it is one symbol. | ||
| “f prime of x” | Lagrange's notation: is the derivative of , a new function, and is its value at . | ||
| “d by d x” | An operator: “take the derivative, with respect to , of whatever follows”. | ||
| “f double prime of x” | The second derivative, the derivative of the derivative. It measures how the slope itself changes: curvature. Leibniz writes it . | ||
| “d f by d x, evaluated at a” | The derivative, evaluated at the point . The same number as . | ||
| “the chain rule” | Differentiating a function of a function: multiply the rates. If depends on and on , the rates chain together. Backpropagation is this rule, applied over and over. |
Leibniz's shines in the chain rule. If depends on , and depends on , then
It looks like cancelling fractions, which makes it easy to remember, though isn't really a fraction. An example: for , let . Then and , so . A neural network is a long chain of functions, and backpropagation is this rule applied link by link.
Learn: many inputs, ∂ and ∇
A loss depends on many parameters at once. To ask how it changes, we change one input and hold every other fixed. That's a partial derivative, written with a curly (read “partial”, sometimes “del”):
In practice you differentiate as usual and treat the other variables as constants. For a single data point and a line with slope and intercept , the squared error is . With the chain rule,
Let . What is at the point ?
Stack all the partial derivatives into a vector and you have the gradient, , read “grad f” or “nabla f”. It has one entry per input, so it has the same shape as the input (Chamber 6). When a function has several kinds of arguments, a subscript says which ones are being varied: is the gradient of the loss with respect to the parameters only. The Adam paper puts it in one line: denotes “the vector of partial derivatives of , w.r.t ”.
- “partial f by partial x i”The partial derivative: how changes when only moves and every other input is held fixed. The curly warns you there are other inputs.
- “grad f”The gradient: every partial derivative of stacked into a vector, the same shape as the input. is called nabla.
- “grad theta of L”The gradient of the loss with respect to the parameters only. The subscript names the variables being nudged; everything else is held fixed.
- “the Jacobian of f”For , the matrix of all first partials, . Also written .
- “the Hessian of f”For , the matrix of second partials, . Also written .
| Symbol | Say it | Meaning | LaTeX |
|---|---|---|---|
| “partial f by partial x i” | The partial derivative: how changes when only moves and every other input is held fixed. The curly warns you there are other inputs. | ||
| “grad f” | The gradient: every partial derivative of stacked into a vector, the same shape as the input. is called nabla. | ||
| “grad theta of L” | The gradient of the loss with respect to the parameters only. The subscript names the variables being nudged; everything else is held fixed. | ||
| “the Jacobian of f” | For , the matrix of all first partials, . Also written . | ||
| “the Hessian of f” | For , the matrix of second partials, . Also written . |
Here is a short proof that uses exactly this notation. It also explains the adversarial example you met in Discover.
Claim. For fixed , the function has gradient .
Proof. Write the dot product as a sum (Chamber 6): . Fix an index and take the partial derivative with respect to . Every term with doesn't contain , so it's a constant and contributes . The term is a constant times , with derivative . So for every , and stacking the partials gives .
Now think about a linear model. Its cost depends on the input only through , so by the chain rule is this times a single number. The signs of the gradient are the signs of (or all flipped), and the attack nudges every feature by in whichever direction hurts the prediction. Problem 3 runs it.
Two matrices round out the family. The Jacobian collects the first partials of a function with several outputs, one row per output. The Hessian collects the second partials of a single-output function, and describes its curvature. You'll mostly meet them as notation in theory sections: for , and .
Learn: ∫, the continuous sum
The other half of calculus adds things up. Silvanus Thompson's Calculus Made Easy (1910) opens by defusing its two “dreadful symbols”: , “which merely means ‘a little bit of’”, and , “which is merely a long S, and may be called (if you like) ‘the sum of’”.
That's exactly how to read an integral. Chop the interval from to into thin strips of width , add up height times width, and let the strips get thinner:
The of Chamber 5 becomes and becomes . Read the left side “the integral from a to b of f of x, d x”. The is part of the notation: it says which variable the strips are measured along. Geometrically, it's the area under the curve. For example, , the area of a triangle with base 1 and height 1.
- “the integral from a to b of f of x, d x”A continuous sum: add up height times a little bit of width , for every from to . The area under the curve.
- “the Riemann sum”The finite version: cut into strips of width and add up over the strips. As it becomes the integral, and turns into .
| Symbol | Say it | Meaning | LaTeX |
|---|---|---|---|
| “the integral from a to b of f of x, d x” | A continuous sum: add up height times a little bit of width , for every from to . The area under the curve. | ||
| “the Riemann sum” | The finite version: cut into strips of width and add up over the strips. As it becomes the integral, and turns into . |
In machine learning, integrals appear mostly in probability, where continuous sums replace finite ones. The expected value in Chamber 8, , is a weighted average written with this notation.
Learn: min, max and argmin
Training a model means finding the parameters that make the loss as small as possible. Papers write this with two operators that look alike but answer different questions:
The subscript names the variable you're allowed to change. The best parameters are then written , read “theta star is the arg min over theta of L of theta”, or more naturally, “the theta that minimises the loss”. Swap min for max and you get : a classifier's prediction is the of its class probabilities.
Let . What are and ?
Calculus is how you find an argmin. At a smooth minimum the curve is flat, so the derivative (or every partial derivative) is zero there: . Gradient descent, the subject of the Neural Networks course, walks downhill against until it gets there.
Argmin is also a general-purpose way to define things. The LIME method for explaining a model's predictions defines an explanation as the solution of an optimisation problem:
Spotted in the wild
Read it: “xi of x is the in that minimises L of f, g, pi x, plus omega of g.” The explanation is not a number but a whole simple model , chosen from a set (Chamber 2) of candidate models, to balance faithfulness to the real model (the term) against simplicity (the term).
- “the minimum over x of f”The smallest value that takes: a number on the output side.
- “the arg min over x of f”The input at which is smallest. Read it “the that minimises ”.
- “the arg max over k of p k”The index of the largest entry. A classifier's predicted class is of its predicted probabilities.
- “theta star is the arg min of L”The best parameters: the with the lowest loss. The star marks the optimum, as in Chamber 1.
- “the supremum”The least upper bound. Like , but it exists even when no element reaches it.
| Symbol | Say it | Meaning | LaTeX |
|---|---|---|---|
| “the minimum over x of f” | The smallest value that takes: a number on the output side. | ||
| “the arg min over x of f” | The input at which is smallest. Read it “the that minimises ”. | ||
| “the arg max over k of p k” | The index of the largest entry. A classifier's predicted class is of its predicted probabilities. | ||
| “theta star is the arg min of L” | The best parameters: the with the lowest loss. The star marks the optimum, as in Chamber 1. | ||
| “the supremum” | The least upper bound. Like , but it exists even when no element reaches it. |
Learn: approximations and big-O
The derivative's most useful job is prediction. Rearranging the secant slope, for small :
This is the first-order approximation: near , a smooth function looks like a straight line. For example, , while the true value is . The same idea with a gradient, , is why a small step against the gradient lowers the loss.
How good is an approximation? Big-O notation answers without fussing over constants. In the derivation, the secant slope was : the error is proportional to . In general the forward difference satisfies
read “plus big O of h”: an error no bigger than some constant times once is small. The same notation measures cost. The Transformer paper lists self-attention's cost per layer as for tokens of dimension : double the length of the text, and the work roughly quadruples.
- “the first-order approximation”Close to , a smooth function is almost a straight line through with slope .
- “big O of h”Some quantity no bigger than a constant times , once is small enough. It hides the constant to show the rate: halve , roughly halve the error.
- “big O of n squared”A cost that grows at most like for large : double and the work roughly quadruples. Self-attention's cost in the sequence length is quoted this way.
| Symbol | Say it | Meaning | LaTeX |
|---|---|---|---|
| “the first-order approximation” | Close to , a smooth function is almost a straight line through with slope . | ||
| “big O of h” | Some quantity no bigger than a constant times , once is small enough. It hides the constant to show the rate: halve , roughly halve the error. | ||
| “big O of n squared” | A cost that grows at most like for large : double and the work roughly quadruples. Self-attention's cost in the sequence length is quoted this way. |
Read beyond the course
Short, free, and each one uses the notation of this chamber on every page.
Book · free online · ~25 min
Calculus Made EasySilvanus P. Thompson (Project Gutenberg) · Prologue and Chapters I–III
Written in 1910 “to deliver you from the preliminary terrors”, and still one of the kindest introductions to and ever printed. The first three chapters are about reading the symbols, which is exactly where you are.
Article · free online · ~30 min
The Matrix Calculus You Need For Deep LearningTerence Parr & Jeremy Howard · “Review: Scalar derivative rules” and “Introduction to vector calculus and partial derivatives”
Written for people who know a little calculus and want to read deep-learning papers. These two sections cover derivatives, partials and gradients in the notation papers use; come back for the Jacobian sections after the Linear Algebra course.
Book · free online · ~40 min
Mathematics for Machine LearningMarc Peter Deisenroth, A. Aldo Faisal & Cheng Soon Ong · Sections 5.1–5.2
Derivatives from the limit definition, then partial derivatives and gradients, with ML examples throughout. Its notation matches this chamber closely.
Book · free online · ~30 min
Deep Learning, Chapter 4: Numerical ComputationIan Goodfellow, Yoshua Bengio & Aaron Courville · Sections 4.1 and 4.3
Section 4.3 introduces derivatives, , gradients, the Jacobian and the Hessian in a few pages, in the notation most deep-learning papers copy. Section 4.1 is about rounding error, the villain of the lab's floating-point mode.
Lecture notes · free online · ~20 min
The Definition of the DerivativePaul Dawkins, Paul's Online Math Notes
More worked examples of the limit calculation you did for , one careful line at a time. Try each before reading its solution.
Papers and lectures
The adversarial examples paper is a good first read at your level. Start with Figure 1 and its caption, the panda. Then read Section 4: one page, with the equation from Discover and a paragraph of results. If you'd like to see why the attack works, read the short Section 3, which uses only a dot product and a sign. Skip Sections 6 onwards for now.
For the second paper, return to Adam's Algorithm 1, which you met in Chamber 1. This time read its first line inside the loop, the one with the .
Explaining and Harnessing Adversarial ExamplesIan J. Goodfellow, Jonathon Shlens, Christian Szegedy · ICLR, 2015Shows that tiny, targeted changes to an input can fool neural networks, explains why in a page of simple algebra, and introduces the fast gradient sign method. Short, readable, and full of gradients taken with respect to the input.
Adam: A Method for Stochastic OptimizationDiederik P. Kingma, Jimmy Ba · ICLR, 2015Every step of Adam starts by computing a gradient. Section 2 defines it in words, which makes it a good place to check your reading of .
Decode the paper · Section 4, the “fast gradient sign method”
Explaining and Harnessing Adversarial ExamplesIan J. Goodfellow, Jonathon Shlens, Christian Szegedy · ICLR, 2015
The paper's own definitions: “Let be the parameters of a model, the input to the model, the targets associated with … and be the cost used to train the neural network.” Match each symbol to its meaning.
Options
Decode the paper · Algorithm 1, first line of the loop
Adam: A Method for Stochastic OptimizationDiederik P. Kingma, Jimmy Ba · ICLR, 2015
The paper annotates this line “Get gradients w.r.t. stochastic objective at timestep ”, and Section 2 spells it out: is “the vector of partial derivatives of , w.r.t evaluated at timestep ”. Chamber 1 decoded Adam's last line; this is its first.
Options
Watch
The first video shows where and come from, by slicing a circle into rings. The second is this chamber's lab in animation: watch for the moment the notation is introduced, and for how the derivative of is worked out with a small , just as you did for .
Your turn
Say the symbols, turn them into code, prove two derivatives, then compute: a secant, a minimum and an adversarial attack.
Match · Symbols ↔ Read aloud as
Say it aloud
Match each piece of calculus notation to the way you'd read it.
Options
Match · Maths ↔ Python
From notation to code
Match each formula to the Python (with NumPy) that computes or approximates it. Here L is an array of loss values and grad holds .
Options
Proofs
The puzzle repeats this chamber's derivation for , with a trap or two among the lines. The proof to write combines the limit result with Chamber 6's index notation.
Proof puzzle
The derivative of x³, from the limit
Claim
For , the derivative is at every real .
Tap lines in the order they should appear. Not every line belongs. Tap a line in your proof to send it back.
Your proof
- Pick the first line below.
Available lines
Prove it yourself
The gradient of a squared length
Claim
For , let . Prove that .
Your typeset proof appears here.
Code it up
Each problem has a single answer. The third one is the fast gradient sign method, straight from the paper, run on a model small enough to check by hand.
Problem 19·Warm-up
How small is small enough?
The derivative of at is . A computer can't take a limit, so it uses the secant slope
for some small . Try the step sizes for (a half, a quarter, an eighth, …).
What is the smallest for which ?
Problem 20·Standard
The min, not the argmin
Fit a line through the origin, , to the 20 points with
for , by minimising the squared error
Setting finds . But this problem asks for something else: submit , the smallest value of the loss, as a reduced fraction like 7/12.
Problem 21·Challenge
Fooling a classifier with its own gradient
Section 5 of the FGSM paper studies logistic regression: with labels , the model predicts , where , and is trained on the cost
where .
Take features and, for ,
with and true label . The model gets this input right, confidently: .
The fast gradient sign method replaces by with , where acts on each entry. With , what probability does the model now give the true class, ? Round to 4 decimal places.
Key takeaways
- is a finite change; is the change in the limit. is a secant's slope, and is the derivative.
- Same derivative, different spellings: , and . Primes and count further derivatives, and the chain rule multiplies rates: .
- nudges one input; collects every nudge. The gradient has the input's shape, and its subscript names what varies: trains a model, attacks its input.
- is a continuous sum of , the limit of .
- is a value; is the input that achieves it. names the best parameters.
- Near a point, functions are nearly lines: , with errors described by big-O. Computers can't shrink forever.
Checkpoint
Prove it to the labyrinth
Answer every question to clear this chamber. First-try answers earn the most XP.
What is the difference between and ?
Let . What is ?
Let . What is at the point ?
Let where . Using , what is at ?
A function takes three inputs and returns two outputs. What is the shape of its Jacobian?
Use the first-order approximation with , , and . What estimate does it give for ?
Read as the area under the line between and . What is it?
The forward difference has error : . Roughly what happens to the error when shrinks from to ?
End of the chamber
Clear this chamber
- Questions in this chamber (0/12 solved)Next unsolved
- Bonus: To the limit (+30 XP)
- Bonus: Problem 19: How small is small enough? (+20 XP)
- Bonus: Problem 20: The min, not the argmin (+35 XP)
- Bonus: Problem 21: Fooling a classifier with its own gradient (+50 XP)
- Bonus: Proof: The derivative of x³, from the limit (+25 XP)
- Bonus: Proof: The gradient of a squared length (+40 XP)
- Bonus: Decode the paper (1) (+30 XP)
- Bonus: Decode the paper (2) (+20 XP)
- Bonus: Match: Say it aloud (+25 XP)
- Bonus: Match: From notation to code (+25 XP)