2. Derivatives

A derivative is a rate: how fast an output moves when its input moves. This chapter defines it as a limit (section 1.3), gives the rules that compute it without limits, and then uses it four ways that recur through the book: to approximate a function by a line, to measure a derivative numerically (and see floating point bite), to solve equations by Newton's method, and to bound how far a function can move. That last use, the mean value theorem of section 2.8, is the shape of most bounds in the paper [refinekv]: a change in the output is at most the steepest slope times the change in the input. Gradients and backpropagation (chapter 7) are this chapter with more inputs.

2.1 The slope of a curve

The slope of a line is rise over run. A curve has no single slope, but over a short run it is nearly straight. Take two points on the graph of \(f\), at \(x\) and \(x + h\). The line through them, a secant, has slope

\[ \frac{f(x + h) - f(x)}{h}. \]

Now shrink \(h\). The second point slides along the curve toward the first, and the secant turns into the line that just touches the curve at \(x\): the tangent. Its slope is the limit of the secant slopes.

x x+h tangent y = x²
Figure 2.1. Secants of \(y = x^2\) through \(x = 0.7\) and \(x + h\) for \(h = 1.4\) (orange), \(0.8\) (green), \(0.35\) (blue). As \(h\) shrinks they turn toward the tangent (red), whose slope is \(f'(0.7) = 1.4\).

Definition. The derivative of \(f\) at \(x\) is

\[ f'(x) = \lim_{h \to 0} \frac{f(x + h) - f(x)}{h}, \]

when the limit exists; then \(f\) is differentiable at \(x\). Other spellings: \(\dfrac{df}{dx}\), \(\dfrac{d}{dx} f(x)\). The derivative of \(f'\) is the second derivative \(f''\), and so on.

The quotient is \(0/0\) at \(h = 0\), which is why a limit is needed. For \(f(x) = x^2\) the algebra removes the \(0/0\):

\[ \frac{(x + h)^2 - x^2}{h} = \frac{2xh + h^2}{h} = 2x + h \;\to\; 2x. \]

So \((x^2)' = 2x\). At \(x = 1\) the secant slope is exactly \(2 + h\): 3, 2.1, 2.01, 2.001 for \(h = 1, 0.1, 0.01, 0.001\). For \(e^x\) at 0 there is no algebra to do yet, but the secant slopes \((e^h - 1)/h\) still settle: 1.7183, 1.0517, 1.0050, 1.0005 for the same \(h\), each about \(1 + h/2\), heading for 1.

That the slope of \(e^x\) at 0 is exactly 1 is what singles out the number \(e\) among all bases (section 4.1). A function can fail to have a derivative: \(|x|\) has slope \(-1\) to the left of 0 and \(+1\) to the right, so the secant slopes at 0 have no single limit. A corner has no tangent.

2.2 The rules

Nobody computes derivatives from the definition twice. A few base cases and a few combining rules cover every function built from them, the way a compiler handles any expression with a rule per node type [openstax].

FunctionDerivativeNote
\(c\) (constant)\(0\)flat line
\(x^n\)\(n x^{n-1}\)power rule; any real \(n\) for \(x > 0\): \((\sqrt{x})' = \tfrac{1}{2} x^{-1/2}\)
\(e^x\)\(e^x\)its own slope (chapter 4)
\(\ln x\)\(1/x\)for \(x > 0\) (section 4.3)
\(\sin x\), \(\cos x\)\(\cos x\), \(-\sin x\)\(x\) in radians

And for combinations, where \(c\) is a constant:

\[ \begin{aligned} (c f)' &= c f' & (f + g)' &= f' + g' \\ (f g)' &= f' g + f g' & \left(\frac{f}{g}\right)' &= \frac{f' g - f g'}{g^2} \quad (g \ne 0). \end{aligned} \]

The product rule has a picture. Think of \(f g\) as the area of a rectangle with sides \(f\) and \(g\). Move \(x\) by \(h\): the sides grow by about \(f' h\) and \(g' h\), and the area gains two strips, \(f' h \cdot g\) and \(f \cdot g' h\), plus a corner \(f' g' h^2\) that vanishes faster than \(h\). Divide by \(h\), let \(h \to 0\), and the corner is gone. The quotient rule follows from the product rule applied to \(f = (f/g) \cdot g\).

Example: \(\tan x = \sin x / \cos x\). The quotient rule gives \(\dfrac{\cos x \cos x - \sin x \, (-\sin x)}{\cos^2 x} = \dfrac{\cos^2 x + \sin^2 x}{\cos^2 x} = \dfrac{1}{\cos^2 x}\). (Every derivative in this section and the next was checked against a central difference, section 2.5, at \(x = 0.7\); they agree to about \(10^{-10}\), the accuracy of that check.)

2.3 The chain rule

A composition \(f(g(x))\) is a two-stage pipeline: \(g\) turns \(x\) into \(u = g(x)\), and \(f\) turns \(u\) into the output. If \(u\) moves \(g'(x)\) times as fast as \(x\), and the output moves \(f'(u)\) times as fast as \(u\), then the output moves \(f'(u) \cdot g'(x)\) times as fast as \(x\). Rates through a chain multiply, like gear ratios.

Theorem (chain rule). If \(g\) is differentiable at \(x\) and \(f\) at \(g(x)\), then

\[ \big(f(g(x))\big)' = f'(g(x)) \, g'(x). \]

A proof that handles the case \(g' = 0\) properly is in [spivak].

Examples. With \(u = \sin x\): \((e^{\sin x})' = e^{\sin x} \cos x\). With \(u = 1 + x^2\): \((\sqrt{1 + x^2})' = \dfrac{1}{2\sqrt{1 + x^2}} \cdot 2x = \dfrac{x}{\sqrt{1 + x^2}}\). With \(u = -x^2/2\), the shape of the normal density (section 12.8):

\[ \frac{d}{dx} e^{-x^2/2} = e^{-x^2/2} \cdot (-x) = -x \, e^{-x^2/2}. \]

A scale inside is the commonest case: \((f(cx))' = c f'(cx)\). Attention divides its logits by \(\sqrt{d}\) before the exponential (section 16.4), so \(\frac{d}{ds} e^{s/\sqrt{d}} = e^{s/\sqrt{d}}/\sqrt{d}\).

A long chain \(f_n(\dots f_2(f_1(x)))\) has derivative \(f_n' \cdots f_2' \cdot f_1'\), each factor evaluated at the value that stage saw. Computing those factors from the output back to the input, reusing the values saved on the way forward, is backpropagation (section 7.4).

2.4 The linear approximation

Near \(x\), a differentiable function is close to its tangent line:

\[ f(x + h) \approx f(x) + f'(x)\, h. \]

This is the definition of the derivative read backwards. The error is \(o(h)\) (section 1.6): it shrinks faster than \(h\). When \(f''\) exists it is \(O(h^2)\), and more precisely about \(\tfrac{1}{2} f''(x) h^2\), the first neglected term of the Taylor polynomial (section 5.4). Two cases used again and again:

\[ \sqrt{1 + h} \approx 1 + \frac{h}{2}, \qquad e^h \approx 1 + h. \]

Here \(f''\) is \(-\tfrac14\) for \(\sqrt{1+h}\) at \(h = 0\), and 1 for \(e^h\), so the errors should be near \(-h^2/8\) and \(h^2/2\). Computed:

\(h\)\(\sqrt{1+h} - (1 + h/2)\)divided by \(h^2\)\(e^h - (1 + h)\)divided by \(h^2\)
0.1\(-1.191 \times 10^{-3}\)\(-0.1191\)\(5.171 \times 10^{-3}\)0.5171
0.01\(-1.244 \times 10^{-5}\)\(-0.1244\)\(5.017 \times 10^{-5}\)0.5017
0.001\(-1.249 \times 10^{-7}\)\(-0.1249\)\(5.002 \times 10^{-7}\)0.5002

Divide \(h\) by 10 and the error drops by 100: the signature of \(O(h^2)\). The ratio to \(h^2\) tends to \(\tfrac12 f''\), \(-0.125\) and \(0.5\). The approximation is how an engineer reads a formula: a small change in the input changes the output by the derivative times that change. The paper's bounds are of this kind. If a token's logit is within \(\delta = 0.1\) of its cluster representative's, its weight \(e^{s}\) is within a factor \(e^{\pm 0.1} \approx 1 \pm 0.1\) of the representative's: about 10% at worst, and section 4.6 shows that on average it is far less.

2.5 Finite differences and their error

When \(f\) is a program with no formula, its derivative is estimated from values. Two standard estimates [burden]:

\[ \text{forward: } \frac{f(x + h) - f(x)}{h}, \qquad \text{central: } \frac{f(x + h) - f(x - h)}{2h}. \]

Their errors come from the Taylor expansion \(f(x \pm h) = f(x) \pm f'(x) h + \tfrac12 f''(x) h^2 \pm \tfrac16 f'''(x) h^3 + \dots\) (section 5.4). For the forward difference the \(h^2\) term survives the division: the error is about \(\tfrac12 f''(x) h\), which is \(O(h)\). In the central difference the even terms cancel in the subtraction, and the error is about \(\tfrac16 f'''(x) h^2\), which is \(O(h^2)\). Same two function calls, one order better.

In exact arithmetic, smaller \(h\) is always better. In doubles it is not. Each computed \(f\) value carries a relative error of about \(u = 2^{-53} \approx 1.1 \times 10^{-16}\), so the difference \(f(x+h) - f(x)\) carries an absolute error up to about \(\varepsilon |f(x)|\) with \(\varepsilon = 2u\), and dividing by \(h\) magnifies it. This is the cancellation of section 1.3 again. The total error of the forward difference is about

\[ E(h) \approx \frac{|f''|}{2} h + \frac{\varepsilon |f|}{h}. \]

The first term falls as \(h\) falls, the second rises. Their sum is smallest where its derivative is zero (section 2.7): \(h^* = \sqrt{2 \varepsilon |f| / |f''|}\). When \(f\) and \(f''\) are of similar size, \(h^*\) is near \(\sqrt{\varepsilon} \approx 1.5 \times 10^{-8}\) and the best error is also near \(\sqrt{\varepsilon}\): half the digits are lost. For the central difference \(E(h) \approx \tfrac16 |f'''| h^2 + \varepsilon |f| / (2h)\), smallest at \(h^* = (3 \varepsilon |f| / (2 |f'''|))^{1/3}\), near \(\sqrt[3]{\varepsilon} \approx 6 \times 10^{-6}\), with a best error near \(\varepsilon^{2/3} \approx 4 \times 10^{-11}\).

The error model is a prediction; running both formulas shows it. The program differentiates \(e^x\) at many step sizes:

#include <float.h>
#include <math.h>
#include <stdio.h>

int main(void)
{
    for (int k = 1; k <= 15; k++) {
        double h = pow(10, -k), ef = 0, ec = 0;
        for (int j = 0; j < 1000; j++) {       /* average over x in [0.5, 1.5) */
            double x = 0.5 + j / 1000.0, exact = exp(x);   /* (e^x)' = e^x */
            double fwd = (exp(x + h) - exp(x)) / h;
            double cen = (exp(x + h) - exp(x - h)) / (2 * h);
            ef += fabs(fwd - exact) / exact / 1000;
            ec += fabs(cen - exact) / exact / 1000;
        }
        printf("h = 1e-%02d  forward err %.1e  central err %.1e\n", k, ef, ec);
    }
    printf("sqrt(eps) = %.1e   cbrt(eps) = %.1e\n", sqrt(DBL_EPSILON), cbrt(DBL_EPSILON));
    return 0;
}
h = 1e-01  forward err 5.2e-02  central err 1.7e-03
h = 1e-02  forward err 5.0e-03  central err 1.7e-05
h = 1e-03  forward err 5.0e-04  central err 1.7e-07
h = 1e-04  forward err 5.0e-05  central err 1.7e-09
h = 1e-05  forward err 5.0e-06  central err 1.8e-11
h = 1e-06  forward err 5.0e-07  central err 6.0e-11
h = 1e-07  forward err 5.0e-08  central err 5.8e-10
h = 1e-08  forward err 8.0e-09  central err 5.7e-09
h = 1e-09  forward err 7.6e-08  central err 6.0e-08
h = 1e-10  forward err 5.7e-07  central err 2.9e-07
h = 1e-11  forward err 5.5e-06  central err 2.7e-06
h = 1e-12  forward err 7.8e-05  central err 6.1e-05
h = 1e-13  forward err 7.4e-04  central err 5.9e-04
h = 1e-14  forward err 5.7e-03  central err 2.9e-03
h = 1e-15  forward err 8.6e-02  central err 7.0e-02
sqrt(eps) = 1.5e-08   cbrt(eps) = 6.1e-06

Printed on FreeBSD 15.1 on amd64 (Core i5-9400), clang 19. It depends on the machine: elsewhere the timings, the random numbers or the last digits differ.

How it maps to the math. The test function is \(e^x\) because its exact derivative is known (itself), and because \(f = f'' = f''' = e^x\) the constants in \(E(h)\) are all equal, so the predicted optima are the clean \(\sqrt{2\varepsilon}\) and \(\sqrt[3]{1.5\,\varepsilon}\). The outer loop sets \(h = 10^{-k}\): one point per decade, since \(E(h)\) is a sum of powers of \(h\) and is best read on log scales. fwd and cen are the two formulas verbatim. The inner loop is there because the rounding term \(\varepsilon|f|/h\) is a bound, not a value: at a single \(x\) the roundings may happen to cancel or to add up, and the measured error jumps by factors of ten from one \(h\) to the next. Averaging the relative error \(|{\rm estimate} - f'(x)|/f'(x)\) over 1000 points \(x_j = 0.5 + j/1000\) turns the bound into a typical size. The last line prints the two scales the theory names.

1e−1 1e−4 1e−8 1e−12 1e−15 1e−1 1e−4 1e−8 1e−11 forward central step h relative error
Figure 2.2. The same numbers on log scales. Left of the minimum the method's error (slope 1 for forward, 2 for central) dominates; right of it, rounding (slope \(-1\) for both). The minima are at \(h = 10^{-8}\) and \(10^{-5}\).

The forward error bottoms out near \(10^{-8}\) at \(h = 10^{-8}\), the central one near \(2 \times 10^{-11}\) at \(h = 10^{-5}\), as predicted. Past the minimum both climb with slope \(-1\): at \(h = 10^{-15}\) only one or two digits are left.

In the machine. Two more sources of error hide in the code. x + h is rounded, so the step actually taken is (x + h) - x, not h; computing volatile double t = x + h; h = t - x; first makes the step exact [nr]. And in float, \(\varepsilon = 2^{-23} \approx 1.2 \times 10^{-7}\), so the best forward step is near \(3 \times 10^{-4}\) and only three or four digits survive (exercise 4).

2.6 Newton's method

To solve \(f(x) = 0\), replace \(f\) by its tangent line at a guess \(x_n\) and solve that instead. The tangent \(f(x_n) + f'(x_n)(x - x_n)\) is zero at

\[ x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}. \]
x₀ x₁ √2 y = x² − 2
Figure 2.3. Newton's method on \(f(x) = x^2 - 2\) from \(x_0 = 2.4\). Each tangent (blue, then green) meets the axis much closer to the root \(\sqrt{2}\) than the last guess was.

Square root. With \(f(x) = x^2 - c\), \(f'(x) = 2x\), the step is \(x - \dfrac{x^2 - c}{2x} = \dfrac{1}{2}\left(x + \dfrac{c}{x}\right)\): Heron's rule of section 1.5.

Reciprocal without dividing. With \(f(x) = 1/x - a\), whose root is \(1/a\), \(f'(x) = -1/x^2\) and the step is

\[ x_{n+1} = x_n - \frac{1/x_n - a}{-1/x_n^2} = x_n (2 - a x_n). \]

Two multiplies and a subtract: no division. This is how hardware and fast libraries divide. SSE's rcpps returns an approximate reciprocal from a table with relative error at most \(1.5 \times 2^{-12}\) [intel-sdm], about 12 bits, and one Newton step lifts that to about 22 bits, nearly a full float (24). Why it doubles: write \(a x_n = 1 - e\), where \(e\) is the relative error. Then \(a x_{n+1} = (1 - e)(1 + e) = 1 - e^2\). The new error is exactly the old one squared.

The program starts from a guess with relative error \(2^{-12}\) and also solves \(\cos x = x\), which bisection needed about fifty steps for (section 1.4):

#include <math.h>
#include <stdio.h>

int main(void)
{
    /* 1/a: Newton on f(x) = 1/x - a gives x <- x (2 - a x) */
    double a = 3.7;
    double x = (1 / a) * (1 - 0x1p-12);        /* a guess good to 12 bits, like rcpps */
    for (int n = 0; n <= 4; n++) {
        printf("1/a  step %d: %.17f  rel err %.1e\n", n, x, fabs(x * a - 1));
        x = x * (2 - a * x);
    }

    /* cos x = x: Newton on f(x) = cos x - x, f'(x) = -sin x - 1 */
    double y = 1;
    for (int n = 1; n <= 5; n++) {
        y = y - (cos(y) - y) / (-sin(y) - 1);
        printf("cos x = x  step %d: %.17f  f = %.1e\n", n, y, cos(y) - y);
    }
    return 0;
}
1/a  step 0: 0.27020428631756754  rel err 2.4e-04
1/a  step 1: 0.27027025416090678  rel err 6.0e-08
1/a  step 2: 0.27027027027026929  rel err 3.6e-15
1/a  step 3: 0.27027027027027023  rel err 1.0e-16
1/a  step 4: 0.27027027027027023  rel err 1.0e-16
cos x = x  step 1: 0.75036386784024389  f = -1.9e-02
cos x = x  step 2: 0.73911289091136168  f = -4.6e-05
cos x = x  step 3: 0.73908513338528403  f = -2.8e-10
cos x = x  step 4: 0.73908513321516067  f = 0.0e+00
cos x = x  step 5: 0.73908513321516067  f = 0.0e+00

Printed on FreeBSD 15.1 on amd64 (Core i5-9400), clang 19. It depends on the machine: elsewhere the timings, the random numbers or the last digits differ.

How it maps to the math. The start x = (1/a) * (1 - 0x1p-12) is \(x_0 = \frac{1}{a}(1 - e_0)\) with \(e_0 = 2^{-12}\), the accuracy of a 12-bit table; the division in it only manufactures the test case. The printed error \(|a x_n - 1|\) is \(|e_n|\), the quantity the algebra above says is squared. The update x * (2 - a * x) is \(x_n(2 - a x_n)\): the operation that would run on the hardware, with no division. The second loop is the general step \(x - f(x)/f'(x)\) with \(f(x) = \cos x - x\) and \(f'(x) = -\sin x - 1\), started at \(x_0 = 1\); it prints \(f(x_n)\), which near the root is \(f'(r)\) times the error.

Errors \(2.4 \times 10^{-4}\), \(6 \times 10^{-8}\), \(3.6 \times 10^{-15}\): squared each time until the double runs out. For \(\cos x = x\), four steps where bisection needed about fifty. This is quadratic convergence. In general, near a root \(r\) with \(f'(r) \ne 0\), the error \(e_n = x_n - r\) satisfies \(e_{n+1} \approx \dfrac{f''(r)}{2 f'(r)} e_n^2\) [burden]; for Heron's rule it is exactly \(e_{n+1} = e_n^2 / (2 x_n)\) (exercise 7 of chapter 1).

Watch out. The speed is only near the root. Far away, a tangent can point anywhere: where \(f' = 0\) the step divides by zero, and the reciprocal iteration diverges unless \(0 < x_0 < 2/a\) (then \(|e| < 1\) and \(e^2\) shrinks). Hardware gets a good start from a table; software often brackets the root first with bisection.

2.7 Maxima and minima

At the top of a smooth hill the tangent is flat. If \(f\) has a maximum or minimum at an interior point \(x\) and is differentiable there, then \(f'(x) = 0\). The reverse is not true: \(x^3\) is flat at 0 and has neither. Points with \(f' = 0\) are critical points, the candidates.

The second derivative sorts them. If \(f'(x) = 0\) and \(f''(x) > 0\), the slope is increasing through zero, from downhill to uphill: a minimum. If \(f''(x) < 0\), a maximum. If \(f''(x) = 0\), the test says nothing. On a closed interval \([a, b]\), also compare the values at the ends: the largest value may sit at an end where \(f' \ne 0\). (Functions with \(f'' \ge 0\) everywhere are the convex functions of chapter 11; for them a critical point is always the global minimum.)

Example: the mean. Which single number \(c\) best stands for the numbers \(k_1, \dots, k_n\), in the sense of least total squared distance \(f(c) = \sum_i (k_i - c)^2\)? Differentiate in \(c\):

\[ f'(c) = -2 \sum_{i=1}^{n} (k_i - c) = 0 \iff c = \frac{1}{n} \sum_{i=1}^{n} k_i, \qquad f''(c) = 2n > 0. \]

The mean, and it is a minimum. This is why the representative of a cluster of keys is its mean \(\bar{k}_C\) (section 18.2; with vectors, the same computation runs coordinate by coordinate). Example: a peak. \(g(x) = x e^{-x}\) has \(g'(x) = (1 - x) e^{-x}\), zero at \(x = 1\), and \(g''(x) = (x - 2) e^{-x}\), which is \(-1/e < 0\) there: a maximum, of value \(1/e\). (A brute-force scan in steps of \(10^{-4}\) agrees with both: for \(k = (0.3, 1.1, -0.4, 2.0, 0.9)\) the smallest \(f(c)\) is at \(c = 0.7800\), their mean; the largest \(x e^{-x}\) is 0.367879 at \(x = 1.0000\).)

The optimal step \(h^*\) of section 2.5 came from the same recipe: set \(E'(h) = 0\) and solve.

2.8 The mean value theorem

Drive 120 km in one hour and at some instant the speedometer read exactly 120 km/h. You cannot have been below it the whole time, nor above it.

Theorem (mean value). If \(f\) is continuous on \([a, b]\) and differentiable on \((a, b)\), then for some \(\xi\) strictly between \(a\) and \(b\),

\[ f'(\xi) = \frac{f(b) - f(a)}{b - a}. \]

Some tangent is parallel to the chord. Proof in [spivak] and [openstax] (volume 1, section 4.4).

The theorem never says where \(\xi\) is. Its use is that \(\xi\) is somewhere in the interval, so whatever bounds \(f'\) on the whole interval bounds the chord's slope:

Corollary (slope bound). If \(|f'| \le M\) on an interval, then for \(x, y\) in it

\[ |f(x) - f(y)| \le M \, |x - y|. \]

Three consequences used later: if \(f' = 0\) everywhere on an interval then \(f\) is constant there; if \(f' > 0\) then \(f\) is increasing (so \(e^x\) is, section 4.4); and \(|\sin x - \sin y| \le |x - y|\), because \(|\cos| \le 1\).

This is the paper's style of bound. In a cluster \(C\), every token's logit \(s_i\) is within \(\delta_C\) of the representative's logit \(s_C\). Between them \((e^s)' = e^s \le e^{s_C + \delta_C}\), so

\[ \left| e^{s_i} - e^{s_C} \right| \le e^{s_C} \, e^{\delta_C} \, |s_i - s_C| \le e^{s_C} \, e^{\delta_C} \, \delta_C. \]

Each token's weight is its representative's weight, up to a relative error of \(e^{\delta_C} \delta_C\). Small spread, small error, whatever the keys are. The bound is sharp: over a million random \((s_C, \delta_C, s_i)\) the ratio of the left side to the middle one never exceeds 1, and its largest value is 0.999999, approached when \(\delta_C\) is tiny, so that \(e^s\) is nearly a straight line over the interval.

For a whole cluster the paper does better than this term-by-term bound: the errors of the tokens above and below the mean partly cancel, and the result is \(\cosh \delta_C - 1 \approx \delta_C^2/2\) instead of \(\approx \delta_C\) (section 4.6, section 19.4).

Exercises

  1. From the definition, find the derivative of \(x^3\).
    Answer

    \(\dfrac{(x+h)^3 - x^3}{h} = \dfrac{3x^2 h + 3x h^2 + h^3}{h} = 3x^2 + 3xh + h^2 \to 3x^2\).

  2. Differentiate \(\dfrac{x}{1 + x^2}\). Where is it largest for \(x \ge 0\)?
    Answer

    Quotient rule: \(\dfrac{(1 + x^2) - x \cdot 2x}{(1 + x^2)^2} = \dfrac{1 - x^2}{(1 + x^2)^2}\). It is zero at \(x = 1\), positive before and negative after: the maximum is \(1/2\) at \(x = 1\).

  3. The logistic function is \(\sigma(x) = \dfrac{1}{1 + e^{-x}}\). Show that \(\sigma'(x) = \sigma(x)(1 - \sigma(x))\).
    Answer

    Chain rule on \(u^{-1}\) with \(u = 1 + e^{-x}\): \(\sigma' = -u^{-2} \cdot (-e^{-x}) = \dfrac{e^{-x}}{(1 + e^{-x})^2} = \dfrac{1}{1 + e^{-x}} \cdot \dfrac{e^{-x}}{1 + e^{-x}} = \sigma (1 - \sigma)\). Training code uses this: the derivative costs one multiply once \(\sigma\) is known.

  4. Differentiate \(\sin\) at 1 with the forward difference in float. Which \(h\) is best, and how many digits are right?
    Answer

    Theory says \(h^*\) near \(\sqrt{\varepsilon_f} = \sqrt{1.2 \times 10^{-7}} \approx 3.5 \times 10^{-4}\), with an error of the same order. A scan of \(h = 2^{-k/4}\) finds the best at \(h \approx 2 \times 10^{-4}\) with relative error \(1.6 \times 10^{-5}\): four or five digits, on a lucky step. Never trust more than three.

  5. rsqrtps approximates \(1/\sqrt{a}\). Derive Newton's step for \(f(x) = 1/x^2 - a\) and show it needs no division or square root.
    Answer

    \(f'(x) = -2/x^3\), so \(x - \dfrac{1/x^2 - a}{-2/x^3} = x + \dfrac{x - a x^3}{2} = \dfrac{x (3 - a x^2)}{2}\): multiplies and a subtract (the halving is an exponent decrement). From 12 bits the errors are \(2.4 \times 10^{-4}\), \(8.9 \times 10^{-8}\), \(1.2 \times 10^{-14}\): quadratic again.

  6. With weights \(w_i > 0\) summing to 1, which \(c\) minimizes \(\sum_i w_i (k_i - c)^2\)? What does this say about the output of attention?
    Answer

    \(f'(c) = -2 \sum_i w_i (k_i - c) = 0\) gives \(c = \sum_i w_i k_i\), the weighted mean, and \(f'' = 2 \sum_i w_i > 0\). Attention's output \(\sum_i \alpha_i v_i\) is a weighted mean of the values, so it is the point closest, in weighted squared distance, to all of them (section 16.2).

  7. Let \(|x_i| \le \delta\) for each \(i\), and \(m = \frac{1}{n} \sum_i e^{x_i}\). Use the slope bound to show \(|m - 1| \le e^{\delta} \delta\). For \(x = (-0.1, 0.05, 0.08, -0.03)\), compare \(m - 1\), \(e^{\delta}\delta\) and \(\cosh \delta - 1\) with \(\delta = 0.1\).
    Answer

    On \([-\delta, \delta]\), \((e^x)' \le e^{\delta}\), so \(|e^{x_i} - 1| = |e^{x_i} - e^0| \le e^{\delta} |x_i| \le e^{\delta} \delta\); averaging keeps the bound. The numbers: \(m - 1 = 0.00246\), \(e^{\delta}\delta = 0.1105\), \(\cosh \delta - 1 = 0.0050\). These \(x_i\) average to 0, which is the paper's setting, and there the cosh bound of section 4.6 is twenty times tighter.

References