Skip to content
AriadneTechnology

The Inner Ring · Chamber 7 of 9

Change: The Notation of Calculus

Limits, derivatives, partials and gradients: how papers write change, and what ∇ is really asking.

40 min 60 XP + 12 questions + 1 challengeNotationVideoPapersProofsCodeLab

In this chamber you will

  • Read lim, d/dx, f′, ∂ and ∇
  • Derive a derivative from the limit definition
  • Tell min from argmin, and read θ* = argmin L(θ)
  • Estimate derivatives numerically in code
DiscoverLearnRead beyondPapers & lecturesYour turn

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

η=ϵ sign(∇xJ(θ,x,y))\boldsymbol{\eta} = \epsilon\, \text{sign}\left(\nabla_{\boldsymbol{x}} J(\boldsymbol{\theta}, \boldsymbol{x}, y)\right)
Goodfellow, Shlens & Szegedy (2015), “Explaining and Harnessing Adversarial Examples”, Section 4

Add η\boldsymbol{\eta} to a photo of a panda, with ϵ=0.007\epsilon = 0.007, 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), J(θ,x,y)J(\boldsymbol{\theta}, \boldsymbol{x}, y) is a function of three arguments (Chamber 4), and ϵ\epsilon is a small number. The new piece is the upside-down triangle, ∇\nabla, with a subscript. Before learning what it means, make a guess:

Quick check +20 XP

Take a guess before reading on. In η=ϵ sign(∇xJ(θ,x,y))\boldsymbol{\eta} = \epsilon\, \text{sign}\left(\nabla_{\boldsymbol{x}} J(\boldsymbol{\theta}, \boldsymbol{x}, y)\right), the paper defines θ\boldsymbol{\theta} as the model's parameters, x\boldsymbol{x} as the input image and yy as its label. Which of them is the equation about to change?

∇\nabla 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: Δ\Delta, lim⁡\lim, dd, ∂\partial, ∫\int and arg min⁡\argmin. It is not a calculus course. You'll derive one derivative by hand, and after that the notation is the point.

DiscoverLearnRead beyondPapers & lecturesYour turn

Learn: from Δ to d

Change starts with a difference. If xx moves from x0x_0 to x0+hx_0 + h, the change in xx is Δx=h\Delta x = h, read “delta x”, with capital delta for “difference” (Chamber 1). If y=f(x)y = f(x), the change in yy is Δy=f(x0+h)−f(x0)\Delta y = f(x_0 + h) - f(x_0). Their ratio is rise over run:

ΔyΔx=f(x0+h)−f(x0)h.\frac{\Delta y}{\Delta x} = \frac{f(x_0 + h) - f(x_0)}{h}.

Geometrically, it's the slope of the straight line through two points of the curve, a line called a secant. If ff gives a car's position at time xx, it's the average speed over the interval.

But what is the speed at the instant x0x_0? Setting h=0h = 0 gives 00\frac{0}{0}, which means nothing. The way out is a limit:

lim⁡h→0f(x0+h)−f(x0)h,\lim_{h \to 0} \frac{f(x_0 + h) - f(x_0)}{h},

read “the limit, as h tends to zero, of …”. It asks what the ratio gets closer and closer to as hh shrinks, while hh itself is never zero. When the change is shrunk in this way, Δ\Delta becomes dd: dxdx is an infinitesimally small change in xx, and dydx\frac{dy}{dx} is what ΔyΔx\frac{\Delta y}{\Delta x} becomes in the limit.

Change and limits
  • Δx\Delta x“delta x”
    A finite change in xx: new value minus old value. Capital delta means “difference” (Chamber 1).
    Δx=xnew−xold\Delta x = x_{\text{new}} - x_{\text{old}}
  • ΔyΔx\frac{\Delta y}{\Delta x}“delta y over delta x”
    Rise over run: the slope of the straight line (the secant) through two points of a curve.
    ΔyΔx=f(x0+h)−f(x0)h\frac{\Delta y}{\Delta x} = \frac{f(x_0 + h) - f(x_0)}{h}
  • lim⁡h→0\lim_{h \to 0}“the limit as h tends to zero”
    The value that the expression after it approaches as hh gets closer and closer to 00, without ever being set to 00.
    lim⁡h→0(6+h)=6\lim_{h \to 0} (6 + h) = 6
  • dxdx“d x”
    What Δx\Delta x becomes in the limit: an infinitesimally small change in xx. Thompson's Calculus Made Easy calls it “a little bit of xx”.
    dydx\frac{dy}{dx}

Learn: the derivative, from the limit

The derivative of ff at xx is that limit:

f′(x)=lim⁡h→0f(x+h)−f(x)h.f'(x) = \lim_{h \to 0} \frac{f(x + h) - f(x)}{h}.

Here is the one derivation this chamber does by hand, for f(x)=x2f(x) = x^2. Watch each step, because the same moves work for any polynomial.

f(x+h)−f(x)h=(x+h)2−x2h(1)=x2+2xh+h2−x2h(2)=2xh+h2h(3)=2x+h.(4)\begin{aligned} \frac{f(x + h) - f(x)}{h} &= \frac{(x + h)^2 - x^2}{h} && \text{(1)} \\ &= \frac{x^2 + 2xh + h^2 - x^2}{h} && \text{(2)} \\ &= \frac{2xh + h^2}{h} && \text{(3)} \\ &= 2x + h. && \text{(4)} \end{aligned}

Step (1) substitutes f(x)=x2f(x) = x^2, (2) expands the square, (3) cancels the two x2x^2 terms, and (4) divides by hh. So the secant slope is exactly 2x+h2x + h, for every hh. As h→0h \to 0 the hh vanishes and 2x2x stays put:

f′(x)=lim⁡h→0 (2x+h)=2x.f'(x) = \lim_{h \to 0}\, (2x + h) = 2x.

Two details matter. The division by hh is legal because a limit never sets h=0h = 0. And the leftover, hh itself, is the error of the secant: at x=1x = 1 with h=0.5h = 0.5 the secant slope is 2.52.5 while the true derivative is 22.

Quick check +20 XP

Let f(x)=x2f(x) = x^2. What is the slope ΔyΔx\frac{\Delta y}{\Delta x} of the secant between x0=3x_0 = 3 and x0+hx_0 + h, with h=0.1h = 0.1?

Now do it with your own hands. Shrink hh and watch the secant turn into the tangent, the line that just touches the curve with slope f′(x0)f'(x_0). Get within 0.0010.001 at three points to clear the challenge, then switch on floating-point mode for a surprise.

Interactive lab

From secant to tangent

The gold line joins two points of the curve, a distance hh apart: its slope is ΔyΔx\tfrac{\Delta y}{\Delta x}. The teal line is the tangent, whose slope is the derivative f′(x0)f'(x_0). Shrink hh and watch one become the other.
−2−1012f(x) = x²Δx = hΔy

Drag h towards zero and watch the gold secant swing onto the teal tangent.

f(x)=x2,f′(x)=2xf(x) = x^2, \quad f'(x) = 2x

ΔyΔx=f(x0+h)−f(x0)h=3.000000\frac{\Delta y}{\Delta x} = \frac{f(x_0 + h) - f(x_0)}{h} = 3.000000
f′(x0)=2.000000f'(x_0) = 2.000000
error=∣ΔyΔx−f′(x0)∣=1\text{error} = \left|\frac{\Delta y}{\Delta x} - f'(x_0)\right| = 1

Points within 0.001 (at least 0.25 apart)

  • ?
  • ?
  • ?
Challenge: To the limitShrink h until the secant slope is within 0.001 of the derivative, at three different points.+30 XP

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:

Derivative notation
  • dydx\frac{dy}{dx}“d y by d x”
    Leibniz's notation for the derivative of yy with respect to xx: the limit of ΔyΔx\frac{\Delta y}{\Delta x}. It often behaves like a fraction, but it is one symbol.
    y=x2  ⟹  dydx=2xy = x^2 \implies \frac{dy}{dx} = 2x
  • f′(x)f'(x)“f prime of x”
    Lagrange's notation: f′f' is the derivative of ff, a new function, and f′(x)f'(x) is its value at xx.
    f(x)=x3  ⟹  f′(x)=3x2f(x) = x^3 \implies f'(x) = 3x^2
  • ddx\frac{d}{dx}“d by d x”
    An operator: “take the derivative, with respect to xx, of whatever follows”.
    ddx ex=ex\frac{d}{dx}\, e^x = e^x
  • f′′(x)f''(x)“f double prime of x”
    The second derivative, the derivative of the derivative. It measures how the slope itself changes: curvature. Leibniz writes it d2ydx2\frac{d^2 y}{dx^2}.
    f(x)=x4  ⟹  f′′(x)=12x2f(x) = x^4 \implies f''(x) = 12x^2
  • dfdx∣x=a\left.\frac{df}{dx}\right|_{x=a}“d f by d x, evaluated at a”
    The derivative, evaluated at the point x=ax = a. The same number as f′(a)f'(a).
    ddxx2∣x=3=6\left.\frac{d}{dx} x^2\right|_{x=3} = 6
  • dydx=dydu dudx\frac{dy}{dx} = \frac{dy}{du}\,\frac{du}{dx}“the chain rule”
    Differentiating a function of a function: multiply the rates. If yy depends on uu and uu on xx, the rates chain together. Backpropagation is this rule, applied over and over.

Leibniz's dydx\frac{dy}{dx} shines in the chain rule. If yy depends on uu, and uu depends on xx, then

dydx=dydu dudx.\frac{dy}{dx} = \frac{dy}{du}\,\frac{du}{dx}.

It looks like cancelling fractions, which makes it easy to remember, though dydu\frac{dy}{du} isn't really a fraction. An example: for y=e2xy = e^{2x}, let u=2xu = 2x. Then dydu=eu\frac{dy}{du} = e^{u} and dudx=2\frac{du}{dx} = 2, so dydx=2e2x\frac{dy}{dx} = 2e^{2x}. 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 ∂\partial (read “partial”, sometimes “del”):

∂f∂xi=lim⁡h→0f(…,xi+h,…)−f(…,xi,…)h.\frac{\partial f}{\partial x_i} = \lim_{h \to 0} \frac{f(\ldots, x_i + h, \ldots) - f(\ldots, x_i, \ldots)}{h}.

In practice you differentiate as usual and treat the other variables as constants. For a single data point (x,y)(x, y) and a line with slope ww and intercept bb, the squared error is ℓ(w,b)=(wx+b−y)2\ell(w, b) = (wx + b - y)^2. With the chain rule,

∂ℓ∂w=2(wx+b−y) x,∂ℓ∂b=2(wx+b−y).\begin{aligned} \frac{\partial \ell}{\partial w} &= 2(wx + b - y)\,x, \\ \frac{\partial \ell}{\partial b} &= 2(wx + b - y). \end{aligned}
Quick check +20 XP

Let f(x,y)=x2y+3yf(x, y) = x^2 y + 3y. What is ∂f∂x\frac{\partial f}{\partial x} at the point (x,y)=(2,5)(x, y) = (2, 5)?

Stack all the partial derivatives into a vector and you have the gradient, ∇f\nabla f, 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: ∇θL\nabla_\theta \mathcal{L} is the gradient of the loss with respect to the parameters only. The Adam paper puts it in one line: gt=∇θft(θ)g_t = \nabla_\theta f_t(\theta) denotes “the vector of partial derivatives of ftf_t, w.r.t θ\theta”.

Partial derivatives, gradients and their matrices
  • ∂f∂xi\frac{\partial f}{\partial x_i}“partial f by partial x i”
    The partial derivative: how ff changes when only xix_i moves and every other input is held fixed. The curly ∂\partial warns you there are other inputs.
    f=x12x2  ⟹  ∂f∂x1=2x1x2f = x_1^2 x_2 \implies \frac{\partial f}{\partial x_1} = 2x_1 x_2
  • ∇f\nabla f“grad f”
    The gradient: every partial derivative of ff stacked into a vector, the same shape as the input. ∇\nabla is called nabla.
    ∇f=(∂f∂x1,…,∂f∂xn)⊤\nabla f = \left(\frac{\partial f}{\partial x_1}, \ldots, \frac{\partial f}{\partial x_n}\right)^\top
  • ∇θL\nabla_\theta \mathcal{L}“grad theta of L”
    The gradient of the loss with respect to the parameters θ\theta only. The subscript names the variables being nudged; everything else is held fixed.
    θ←θ−η ∇θL(θ)\theta \leftarrow \theta - \eta\, \nabla_\theta \mathcal{L}(\theta)
  • JfJ_{\mathbf{f}}“the Jacobian of f”
    For f:Rn→Rm\mathbf{f} : \mathbb{R}^n \to \mathbb{R}^m, the m×nm \times n matrix of all first partials, Jij=∂fi∂xjJ_{ij} = \frac{\partial f_i}{\partial x_j}. Also written ∂f∂x\frac{\partial \mathbf{f}}{\partial \mathbf{x}}.
    Jij=∂fi∂xjJ_{ij} = \frac{\partial f_i}{\partial x_j}
  • HfH_f“the Hessian of f”
    For f:Rn→Rf : \mathbb{R}^n \to \mathbb{R}, the n×nn \times n matrix of second partials, Hij=∂2f∂xi ∂xjH_{ij} = \frac{\partial^2 f}{\partial x_i\, \partial x_j}. Also written ∇2f\nabla^2 f.
    Hij=∂2f∂xi ∂xjH_{ij} = \frac{\partial^2 f}{\partial x_i\, \partial x_j}

Here is a short proof that uses exactly this notation. It also explains the adversarial example you met in Discover.

Claim. For fixed w∈Rn\mathbf{w} \in \mathbb{R}^n, the function f(x)=w⊤xf(\mathbf{x}) = \mathbf{w}^\top \mathbf{x} has gradient ∇xf=w\nabla_{\mathbf{x}} f = \mathbf{w}.

Proof. Write the dot product as a sum (Chamber 6): f(x)=∑i=1nwixif(\mathbf{x}) = \sum_{i=1}^{n} w_i x_i. Fix an index kk and take the partial derivative with respect to xkx_k. Every term with i≠ki \neq k doesn't contain xkx_k, so it's a constant and contributes 00. The term wkxkw_k x_k is a constant times xkx_k, with derivative wkw_k. So ∂f∂xk=wk\frac{\partial f}{\partial x_k} = w_k for every kk, and stacking the partials gives ∇xf=(w1,…,wn)⊤=w\nabla_{\mathbf{x}} f = (w_1, \ldots, w_n)^\top = \mathbf{w}. ■\blacksquare

Now think about a linear model. Its cost depends on the input only through w⊤x\mathbf{w}^\top \mathbf{x}, so by the chain rule ∇xJ\nabla_{\boldsymbol{x}} J is this w\mathbf{w} times a single number. The signs of the gradient are the signs of w\mathbf{w} (or all flipped), and the attack nudges every feature by ϵ\epsilon 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: J∈Rm×nJ \in \mathbb{R}^{m \times n} for f:Rn→Rm\mathbf{f} : \mathbb{R}^n \to \mathbb{R}^m, and ∇2f∈Rn×n\nabla^2 f \in \mathbb{R}^{n \times n}.

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”: dd, “which merely means ‘a little bit of’”, and ∫\int, “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 aa to bb into thin strips of width Δx\Delta x, add up height times width, and let the strips get thinner:

∫abf(x) dx=lim⁡Δx→0∑if(xi) Δx.\int_a^b f(x)\,dx = \lim_{\Delta x \to 0} \sum_{i} f(x_i)\,\Delta x.

The Σ\Sigma of Chamber 5 becomes ∫\int and Δx\Delta x becomes dxdx. Read the left side “the integral from a to b of f of x, d x”. The dxdx is part of the notation: it says which variable the strips are measured along. Geometrically, it's the area under the curve. For example, ∫01x dx=12\int_0^1 x\,dx = \frac{1}{2}, the area of a triangle with base 1 and height 1.

Integrals
  • ∫abf(x) dx\int_a^b f(x)\,dx“the integral from a to b of f of x, d x”
    A continuous sum: add up height f(x)f(x) times a little bit of width dxdx, for every xx from aa to bb. The area under the curve.
    ∫01x dx=12\int_0^1 x\,dx = \tfrac{1}{2}
  • ∑if(xi) Δx\sum_{i} f(x_i)\,\Delta x“the Riemann sum”
    The finite version: cut [a,b][a, b] into strips of width Δx\Delta x and add up f(xi) Δxf(x_i)\,\Delta x over the strips. As Δx→0\Delta x \to 0 it becomes the integral, and Σ\Sigma turns into ∫\int.

In machine learning, integrals appear mostly in probability, where continuous sums replace finite ones. The expected value in Chamber 8, E[X]=∫x p(x) dx\mathbb{E}[X] = \int x\, p(x)\,dx, 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:

min⁡θ L(θ)the smallest value of the loss,arg min⁡θ L(θ)the θ that achieves it.\begin{aligned} &\min_{\theta}\, \mathcal{L}(\theta) && \text{the smallest value of the loss,} \\ &\argmin_{\theta}\, \mathcal{L}(\theta) && \text{the } \theta \text{ that achieves it.} \end{aligned}

The subscript names the variable you're allowed to change. The best parameters are then written θ⋆=arg min⁡θL(θ)\theta^\star = \argmin_\theta \mathcal{L}(\theta), 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 arg max⁡\argmax: a classifier's prediction is the arg max⁡\argmax of its class probabilities.

Quick check +20 XP

Let L(θ)=(θ−3)2+1\mathcal{L}(\theta) = (\theta - 3)^2 + 1. What are min⁡θL(θ)\min_\theta \mathcal{L}(\theta) and arg min⁡θL(θ)\argmin_\theta \mathcal{L}(\theta)?

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: ∇L(θ⋆)=0\nabla \mathcal{L}(\theta^\star) = \mathbf{0}. Gradient descent, the subject of the Neural Networks course, walks downhill against ∇θL\nabla_\theta \mathcal{L} 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

ξ(x)=argmin⁡g∈G  L(f,g,πx)+Ω(g)\xi(x) = \operatorname*{argmin}_{g \in G} \; \mathcal{L}(f, g, \pi_x) + \Omega(g)
Ribeiro, Singh & Guestrin (2016), “Why Should I Trust You?”: Explaining the Predictions of Any Classifier, Eq. (1)

Read it: “xi of x is the gg in GG that minimises L of f, g, pi x, plus omega of g.” The explanation ξ(x)\xi(x) is not a number but a whole simple model gg, chosen from a set GG (Chamber 2) of candidate models, to balance faithfulness to the real model ff (the L\mathcal{L} term) against simplicity (the Ω\Omega term).

Optimisation
  • min⁡xf(x)\min_{x} f(x)“the minimum over x of f”
    The smallest value that ff takes: a number on the output side.
    min⁡x(x+2)2+5=5\min_{x} (x + 2)^2 + 5 = 5
  • arg min⁡xf(x)\argmin_{x} f(x)“the arg min over x of f”
    The input at which ff is smallest. Read it “the xx that minimises ff”.
    arg min⁡x(x+2)2+5=−2\argmin_{x} (x + 2)^2 + 5 = -2
  • arg max⁡kpk\argmax_{k} p_k“the arg max over k of p k”
    The index of the largest entry. A classifier's predicted class is arg max⁡k\argmax_k of its predicted probabilities.
    arg max⁡k(0.1,0.7,0.2)=2\argmax_{k} (0.1, 0.7, 0.2) = 2
  • θ⋆=arg min⁡θL(θ)\theta^\star = \argmin_{\theta} \mathcal{L}(\theta)“theta star is the arg min of L”
    The best parameters: the θ\theta with the lowest loss. The star marks the optimum, as in Chamber 1.
  • sup⁡\sup“the supremum”
    The least upper bound. Like max⁡\max, but it exists even when no element reaches it.
    sup⁡{1−1n:n=1,2,3,…}=1\sup\left\{1 - \tfrac{1}{n} : n = 1, 2, 3, \ldots\right\} = 1

Learn: approximations and big-O

The derivative's most useful job is prediction. Rearranging the secant slope, for small hh:

f(x+h)≈f(x)+f′(x) h.f(x + h) \approx f(x) + f'(x)\,h.

This is the first-order approximation: near xx, a smooth function looks like a straight line. For example, e0.1≈e0+e0⋅0.1=1.1e^{0.1} \approx e^0 + e^0 \cdot 0.1 = 1.1, while the true value is 1.1052…1.1052\ldots. The same idea with a gradient, L(θ+h)≈L(θ)+∇θL⊤h\mathcal{L}(\theta + \mathbf{h}) \approx \mathcal{L}(\theta) + \nabla_\theta \mathcal{L}^\top \mathbf{h}, 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 x2x^2 derivation, the secant slope was f′(x)+hf'(x) + h: the error is proportional to hh. In general the forward difference satisfies

f(x+h)−f(x)h=f′(x)+O(h),\frac{f(x + h) - f(x)}{h} = f'(x) + O(h),

read “plus big O of h”: an error no bigger than some constant times hh once hh is small. The same notation measures cost. The Transformer paper lists self-attention's cost per layer as O(n2⋅d)O(n^2 \cdot d) for nn tokens of dimension dd: double the length of the text, and the work roughly quadruples.

Approximation and big-O
  • f(x+h)≈f(x)+f′(x) hf(x + h) \approx f(x) + f'(x)\,h“the first-order approximation”
    Close to xx, a smooth function is almost a straight line through (x,f(x))(x, f(x)) with slope f′(x)f'(x).
    e0.1≈e0+e0⋅0.1=1.1e^{0.1} \approx e^0 + e^0 \cdot 0.1 = 1.1
  • O(h)O(h)“big O of h”
    Some quantity no bigger than a constant times hh, once hh is small enough. It hides the constant to show the rate: halve hh, roughly halve the error.
    f(x+h)−f(x)h=f′(x)+O(h)\frac{f(x+h) - f(x)}{h} = f'(x) + O(h)
  • O(n2)O(n^2)“big O of n squared”
    A cost that grows at most like n2n^2 for large nn: double nn and the work roughly quadruples. Self-attention's cost in the sequence length nn is quoted this way.
    O(n2⋅d)O(n^2 \cdot d)
DiscoverLearnRead beyondPapers & lecturesYour turn

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 Easy

Silvanus 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 dd and ∫\int 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 Learning

Terence 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 Learning

Marc 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 Computation

Ian Goodfellow, Yoshua Bengio & Aaron Courville · Sections 4.1 and 4.3

Section 4.3 introduces derivatives, arg min⁡\argmin, 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 Derivative

Paul Dawkins, Paul's Online Math Notes

More worked examples of the limit calculation you did for x2x^2, one careful line at a time. Try each before reading its solution.

DiscoverLearnRead beyondPapers & lecturesYour turn

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 w⊤x~\mathbf{w}^\top \tilde{\mathbf{x}} 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 ∇\nabla.

Explaining and Harnessing Adversarial ExamplesIan J. Goodfellow, Jonathon Shlens, Christian Szegedy · ICLR, 2015

Shows 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, 2015

Every 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 ∇θ\nabla_\theta.

Decode the paper · Section 4, the “fast gradient sign method”

Explaining and Harnessing Adversarial Examples

Ian J. Goodfellow, Jonathon Shlens, Christian Szegedy · ICLR, 2015

+30 XP
η=ϵ sign(∇xJ(θ,x,y))\boldsymbol{\eta} = \epsilon\, \text{sign}\left(\nabla_{\boldsymbol{x}} J(\boldsymbol{\theta}, \boldsymbol{x}, y)\right)

The paper's own definitions: “Let θ\boldsymbol{\theta} be the parameters of a model, x\boldsymbol{x} the input to the model, yy the targets associated with x\boldsymbol{x} … and J(θ,x,y)J(\boldsymbol{\theta}, \boldsymbol{x}, y) be the cost used to train the neural network.” Match each symbol to its meaning.

η\boldsymbol{\eta}
ϵ\epsilon
sign\text{sign}
∇x\nabla_{\boldsymbol{x}}
JJ
θ\boldsymbol{\theta}
yy

Options

Decode the paper · Algorithm 1, first line of the loop

Adam: A Method for Stochastic Optimization

Diederik P. Kingma, Jimmy Ba · ICLR, 2015

+20 XP
gt←∇θft(θt−1)g_t \leftarrow \nabla_\theta f_t(\theta_{t-1})

The paper annotates this line “Get gradients w.r.t. stochastic objective at timestep tt”, and Section 2 spells it out: gt=∇θft(θ)g_t = \nabla_\theta f_t(\theta) is “the vector of partial derivatives of ftf_t, w.r.t θ\theta evaluated at timestep tt”. Chamber 1 decoded Adam's last line; this is its first.

gtg_t
∇θ\nabla_\theta
ftf_t
θt−1\theta_{t-1}

Options

Watch

The essence of calculus3Blue1Brown · 17 min
The paradox of the derivative | Chapter 2, Essence of calculus3Blue1Brown · 17 min

The first video shows where dxdx and ∫\int come from, by slicing a circle into rings. The second is this chamber's lab in animation: watch for the moment the notation dsdt\frac{ds}{dt} is introduced, and for how the derivative of t3t^3 is worked out with a small dtdt, just as you did for x2x^2.

DiscoverLearnRead beyondPapers & lecturesYour turn

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

+25 XP

Match each piece of calculus notation to the way you'd read it.

dydx\frac{dy}{dx}
f′′(x)f''(x)
∂f∂x2\frac{\partial f}{\partial x_2}
∇θL\nabla_\theta \mathcal{L}
lim⁡h→0\lim_{h \to 0}
∫01f(x) dx\int_0^1 f(x)\,dx
arg min⁡θL(θ)\argmin_\theta \mathcal{L}(\theta)

Options

Match · Maths ↔ Python

From notation to code

+25 XP

Match each formula to the Python (with NumPy) that computes or approximates it. Here L is an array of loss values and grad holds ∇θL\nabla_\theta \mathcal{L}.

f(x+h)−f(x)h\frac{f(x+h) - f(x)}{h}
f(x+h)−f(x−h)2h\frac{f(x+h) - f(x-h)}{2h}
min⁡iLi\min_i L_i
arg min⁡iLi\argmin_i L_i
∑if(xi) Δx\sum_i f(x_i)\,\Delta x
θ←θ−η ∇θL\theta \leftarrow \theta - \eta\, \nabla_\theta \mathcal{L}

Options

Proofs

The puzzle repeats this chamber's derivation for x3x^3, 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

+25 XP

Claim

For f(x)=x3f(x) = x^3, the derivative is f′(x)=3x2f'(x) = 3x^2 at every real xx.

Tap lines in the order they should appear. Not every line belongs. Tap a line in your proof to send it back.

Your proof

  1. Pick the first line below.

Available lines

Prove it yourself

The gradient of a squared length

+40 XP

Claim

For x∈Rn\mathbf{x} \in \mathbb{R}^n, let f(x)=∥x∥22=∑i=1nxi2f(\mathbf{x}) = \|\mathbf{x}\|_2^2 = \sum_{i=1}^{n} x_i^2. Prove that ∇xf=2x\nabla_{\mathbf{x}} f = 2\mathbf{x}.

Preview

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?

+20 XP

The derivative of f(x)=x3f(x) = x^3 at x0=2x_0 = 2 is f′(2)=12f'(2) = 12. A computer can't take a limit, so it uses the secant slope

D(h)=f(x0+h)−f(x0)hD(h) = \frac{f(x_0 + h) - f(x_0)}{h}

for some small hh. Try the step sizes h=2−kh = 2^{-k} for k=1,2,3,…k = 1, 2, 3, \ldots (a half, a quarter, an eighth, …).

What is the smallest kk for which ∣D(2−k)−12∣<0.001|D(2^{-k}) - 12| < 0.001?

An exact integer (or a fraction like 7/12)

Problem 20·Standard

The min, not the argmin

+35 XP

Fit a line through the origin, y^=θx\hat{y} = \theta x, to the 20 points (xi,yi)(x_i, y_i) with

xi=i,yi=2i+(5i mod 7)−3,\begin{aligned} x_i &= i, \\ y_i &= 2i + (5i \bmod 7) - 3, \end{aligned}

for i=1,2,…,20i = 1, 2, \ldots, 20, by minimising the squared error

L(θ)=∑i=120(yi−θxi)2.\mathcal{L}(\theta) = \sum_{i=1}^{20} (y_i - \theta x_i)^2.

Setting dLdθ=0\frac{d\mathcal{L}}{d\theta} = 0 finds θ⋆=arg min⁡θL(θ)\theta^\star = \argmin_\theta \mathcal{L}(\theta). But this problem asks for something else: submit min⁡θL(θ)\min_\theta \mathcal{L}(\theta), the smallest value of the loss, as a reduced fraction like 7/12.

An exact integer (or a fraction like 7/12)

Problem 21·Challenge

Fooling a classifier with its own gradient

+50 XP

Section 5 of the FGSM paper studies logistic regression: with labels y∈{−1,1}y \in \{-1, 1\}, the model predicts P(y=1)=σ(w⊤x+b)P(y = 1) = \sigma(w^\top x + b), where σ(z)=11+e−z\sigma(z) = \frac{1}{1 + e^{-z}}, and is trained on the cost

J(θ,x,y)=log⁡(1+e−y (w⊤x+b)),J(\theta, x, y) = \log\left(1 + e^{-y\,(w^\top x + b)}\right),

where θ=(w,b)\theta = (w, b).

Take n=10,000n = 10{,}000 features and, for i=1,…,ni = 1, \ldots, n,

wi=(i mod 6)−2.5100,xi=(i mod 7)−33,\begin{aligned} w_i &= \frac{(i \bmod 6) - 2.5}{100}, \\ x_i &= \frac{(i \bmod 7) - 3}{3}, \end{aligned}

with b=3b = 3 and true label y=1y = 1. The model gets this input right, confidently: σ(w⊤x+b)≈0.953\sigma(w^\top x + b) \approx 0.953.

The fast gradient sign method replaces xx by x~=x+η\tilde{x} = x + \eta with η=ϵ sign(∇xJ(θ,x,y))\eta = \epsilon\, \mathrm{sign}\left(\nabla_x J(\theta, x, y)\right), where sign\mathrm{sign} acts on each entry. With ϵ=0.03\epsilon = 0.03, what probability does the model now give the true class, σ(w⊤x~+b)\sigma(w^\top \tilde{x} + b)? Round to 4 decimal places.

A number, rounded to 4 decimal places

Key takeaways

  • Δ\Delta is a finite change; dd is the change in the limit. ΔyΔx\frac{\Delta y}{\Delta x} is a secant's slope, and dydx=lim⁡h→0f(x+h)−f(x)h\frac{dy}{dx} = \lim_{h \to 0} \frac{f(x+h) - f(x)}{h} is the derivative.
  • Same derivative, different spellings: dydx\frac{dy}{dx}, f′(x)f'(x) and ddxf\frac{d}{dx}f. Primes and d2ydx2\frac{d^2y}{dx^2} count further derivatives, and the chain rule multiplies rates: dydx=dydududx\frac{dy}{dx} = \frac{dy}{du}\frac{du}{dx}.
  • ∂\partial nudges one input; ∇\nabla collects every nudge. The gradient has the input's shape, and its subscript names what varies: ∇θ\nabla_\theta trains a model, ∇x\nabla_{\boldsymbol{x}} attacks its input.
  • ∫\int is a continuous sum of f(x) dxf(x)\,dx, the limit of ∑f(xi) Δx\sum f(x_i)\,\Delta x.
  • min⁡\min is a value; arg min⁡\argmin is the input that achieves it. θ⋆=arg min⁡θL(θ)\theta^\star = \argmin_\theta \mathcal{L}(\theta) names the best parameters.
  • Near a point, functions are nearly lines: f(x+h)≈f(x)+f′(x)hf(x+h) \approx f(x) + f'(x)h, with errors described by big-O. Computers can't shrink hh forever.

Checkpoint

Prove it to the labyrinth

Answer every question to clear this chamber. First-try answers earn the most XP.

0/8
Question 1 of 8 +20 XP

What is the difference between Δx\Delta x and dxdx?

Question 2 of 8 +20 XP

Let f(x)=x3f(x) = x^3. What is f′′(2)f''(2)?

Question 3 of 8 +20 XP

Let f(x1,x2)=x12+3x1x2f(x_1, x_2) = x_1^2 + 3x_1 x_2. What is ∇f\nabla f at the point (x1,x2)=(1,2)(x_1, x_2) = (1, 2)?

Question 4 of 8 +20 XP

Let y=u2y = u^2 where u=3x+1u = 3x + 1. Using dydx=dydu dudx\frac{dy}{dx} = \frac{dy}{du}\,\frac{du}{dx}, what is dydx\frac{dy}{dx} at x=1x = 1?

Question 5 of 8 +20 XP

A function f:R3→R2\mathbf{f} : \mathbb{R}^3 \to \mathbb{R}^2 takes three inputs and returns two outputs. What is the shape of its Jacobian?

Question 6 of 8 +20 XP

Use the first-order approximation f(x+h)≈f(x)+f′(x) hf(x + h) \approx f(x) + f'(x)\,h with f(x)=xf(x) = \sqrt{x}, x=4x = 4, h=0.1h = 0.1 and f′(x)=12xf'(x) = \frac{1}{2\sqrt{x}}. What estimate does it give for 4.1\sqrt{4.1}?

Question 7 of 8 +20 XP

Read ∫032x dx\int_0^3 2x\,dx as the area under the line y=2xy = 2x between x=0x = 0 and x=3x = 3. What is it?

Question 8 of 8 +20 XP

The forward difference has error O(h)O(h): f(x+h)−f(x)h=f′(x)+O(h)\frac{f(x+h) - f(x)}{h} = f'(x) + O(h). Roughly what happens to the error when hh shrinks from 0.010.01 to 0.0010.001?

End of the chamber

Clear this chamber

+60 XPThe LimitDerivative NotationThe GradientMin and Argmin