Type at least 2 letters. Enter opens the first result, Esc closes.

    Calculus for ML · Lesson 1 · Derivatives in one and many variables

    Derivatives in one variable: slope, linear approximation and the rules

    Beginner 60 min 4 interactive figures 3 videos Colab notebook

    Before this lesson: nothing from this track; this is the first lesson. You need functions, exe^x, log⁡x\log x and sums (foundations). You may have seen derivatives in school; we review them from the start, with the view that the rest of the track needs. Time: about 60 minutes, plus the notebook.


    Why this matters #

    Training a model means changing its parameters so that a loss goes down. The derivative says which way is down and how steep it is. Everything in this track is built on the one-variable ideas of this lesson:

    • Backpropagation is the chain rule, applied many times (lessons 04–06).
    • Activation functions are chosen partly for their derivatives. The sigmoid has σ′(x)≤1/4\sigma'(x) \le 1/4, so long chains of sigmoids give tiny gradients (vanishing gradients). ReLU has derivative 1 where it is active, but no derivative at 0.
    • Learning rates are limited by curvature: on a quadratic with curvature aa, gradient descent diverges for any step size above 2/a2/a. The same rule, with the largest Hessian eigenvalue in place of aa, holds in many dimensions (lesson 03).
    • Taylor polynomials are behind Newton's method (lesson 03), numerical differentiation and gradient checks (lesson 07), and the Laplace approximation (lesson 11).

    1. Predict first #

    2. The picture #

    Slope of a straight line. A line y=mx+cy = mx + c has slope mm: move 1 step to the right and yy changes by mm. For a curve, the slope is different at each point. We find it in two steps.

    Step 1: the secant. Take two points on the graph, at xx and at x+hx + h. The line through them is a secant line. Its slope is "rise over run", the difference quotient

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

    Step 2: let hh go to 0. For f(x)=x2f(x) = x^2 at x=1x = 1, the difference quotient is ((1+h)2−1)/h=(2h+h2)/h=2+h\big((1+h)^2 - 1\big)/h = (2h + h^2)/h = 2 + h. With h=0.1,0.01,0.001h = 0.1, 0.01, 0.001 it is 2.12.1, 2.012.01, 2.0012.001. The secant turns into the tangent line, and its slope goes to 22. This limit is the derivative: f′(1)=2f'(1) = 2.

    Interactive figure: loading…

    Press Play h → 0, or drag the orange point towards the violet one. The secant turns into the tangent line, and the table shows its slope approaching f′(x0)f'(x_0). Then choose ReLU with x0=0x_0 = 0: from the right the slopes are all 1, from the left all 0, so the limit does not exist.

    Zoom in. A second way to see the derivative: zoom in on the graph around a point. A smooth curve looks more and more like a straight line, and that line is the tangent. The vertical gap between the curve and the tangent at distance hh is the error

    r(h)=f(x+h)−f(x)−f′(x) h.r(h) = f(x+h) - f(x) - f'(x)\,h .

    It is not enough that r(h)r(h) goes to 0 (that is true for every continuous ff and any line through the point). The important fact is that r(h)r(h) goes to 0 faster than hh: r(h)/h→0r(h)/h \to 0. In the zoomed picture, where hh is always "one window wide", the gap disappears.

    Interactive figure: loading…

    Press Play zoom. The right panel shows the window around the point, rescaled to a fixed size. The graph straightens into the tangent line, and the ratio r(h)/hr(h)/h in the readout goes to 0. Now choose ReLU or |x|: at the kink the picture looks the same at every zoom, and r(h)/hr(h)/h stays 1.

    Quick check

    What is the slope of the secant line of f(x)=x2f(x) = x^2 through the points at x=3x = 3 and x=3.5x = 3.5?

    3. Definitions and main results #

    The derivative #

    Definition. ff is differentiable at xx if the limit

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

    exists (as a finite number). The number f′(x)f'(x) is the derivative of ff at xx. Other notations: dfdx\dfrac{df}{dx}, ddxf(x)\dfrac{d}{dx} f(x), Df(x)Df(x). The limit must be the same when hh goes to 0 from the right (h>0h > 0) and from the left (h<0h < 0).

    Linear approximation and o(h)o(h) #

    Theorem (derivative = best linear approximation). ff is differentiable at xx exactly when there is a number mm such that

    f(x+h)=f(x)+m h+r(h),r(h)h→0  as h→0.f(x+h) = f(x) + m\,h + r(h), \qquad \frac{r(h)}{h} \to 0 \ \text{ as } h \to 0 .

    Then m=f′(x)m = f'(x).

    Proof.

    1. Define r(h)=f(x+h)−f(x)−mhr(h) = f(x+h) - f(x) - m h. Divide by hh: r(h)h=f(x+h)−f(x)h−m\dfrac{r(h)}{h} = \dfrac{f(x+h) - f(x)}{h} - m.
    2. So r(h)/h→0r(h)/h \to 0 holds exactly when the difference quotient tends to mm, that is, when f′(x)f'(x) exists and equals mm. ■\blacksquare

    We write r(h)=o(h)r(h) = o(h) (read "little-o of hh") for "r(h)/h→0r(h)/h \to 0". In plain words: the error is small even compared with the small step hh. Examples: h2h^2 and h3h^3 are o(h)o(h); 0.5h0.5h and ∣h∣\sqrt{|h|} are not.

    For f(x)=x2f(x) = x^2: (x+h)2=x2+2x h+h2(x+h)^2 = x^2 + 2x\,h + h^2, so f′(x)=2xf'(x) = 2x and r(h)=h2r(h) = h^2. At x=1x = 1 with h=0.1h = 0.1: r=0.01r = 0.01 and r/h=0.1r/h = 0.1. With h=0.01h = 0.01: r=0.0001r = 0.0001 and r/h=0.01r/h = 0.01.

    This is the form of the derivative that generalizes. In lesson 02 the number mm becomes the gradient vector, and in lesson 03 it becomes the Jacobian matrix.

    One consequence: a differentiable function is continuous at xx, because f(x+h)−f(x)=f′(x)h+r(h)→0f(x+h) - f(x) = f'(x)h + r(h) \to 0.

    Kinks: where the derivative does not exist #

    For f(x)=∣x∣f(x) = |x| at x=0x = 0, the difference quotient is ∣h∣/h|h|/h. It is +1+1 for every h>0h > 0 and −1-1 for every h<0h < 0. The one-sided derivatives (limits from the right and from the left) exist but differ, so f′(0)f'(0) does not exist. For ReLU(x)=max⁡(0,x)\mathrm{ReLU}(x) = \max(0, x) at 0 they are 1 and 0.

    Neural networks use ReLU anyway. Two reasons:

    • The kink is a single point. A pre-activation is exactly 0.00.0 only rarely, and everywhere else the derivative is 1 or 0.
    • At the kink, frameworks return a fixed value: PyTorch and JAX use ReLU′(0)=0\mathrm{ReLU}'(0) = 0. This choice is a subgradient: for a convex ff, a number gg with f(y)≥f(x)+g (y−x)f(y) \ge f(x) + g\,(y - x) for all yy (a line through the point that stays below the graph). For ReLU at 0, every g∈[0,1]g \in [0, 1] is a subgradient. The optimization track builds methods on subgradients.

    The rules #

    Let ff and gg be differentiable at xx and let cc be a number.

    Rule Formula
    linearity (f+g)′=f′+g′(f + g)' = f' + g',  (cf)′=cf′\ (cf)' = c f'
    product (fg)′=f′g+fg′(fg)' = f'g + fg'
    quotient (f/g)′=(f′g−fg′)/g2(f/g)' = (f'g - fg')/g^2, where g(x)≠0g(x) \ne 0
    chain (f(g(x)))′=f′(g(x)) g′(x)\big(f(g(x))\big)' = f'(g(x))\, g'(x)

    Why the chain rule multiplies. Let u=g(x)u = g(x) and y=f(u)y = f(u). A small change Δx\Delta x changes uu by about g′(x) Δxg'(x)\,\Delta x. That change of uu changes yy by about f′(u)f'(u) times itself. So Δy≈f′(u) g′(x) Δx\Delta y \approx f'(u)\, g'(x)\, \Delta x: rates of change multiply. Example: y=(3x+1)2y = (3x + 1)^2 at x=1x = 1. Here u=3x+1=4u = 3x + 1 = 4, dy/du=2u=8dy/du = 2u = 8 and du/dx=3du/dx = 3, so dy/dx=8⋅3=24dy/dx = 8 \cdot 3 = 24. Check by expanding: y=9x2+6x+1y = 9x^2 + 6x + 1, y′=18x+6=24y' = 18x + 6 = 24 at x=1x = 1. ✓

    Proofs of the product rule and the chain rule

    Product rule. Write f(x+h)=f+f′h+r1(h)f(x+h) = f + f'h + r_1(h) and g(x+h)=g+g′h+r2(h)g(x+h) = g + g'h + r_2(h), with all values at xx and r1,r2=o(h)r_1, r_2 = o(h). Multiply: f(x+h)g(x+h)=fg+(f′g+fg′)h+[f′g′h2+(f+f′h)r2+(g+g′h)r1+r1r2]f(x+h)g(x+h) = fg + (f'g + fg')h + \big[f'g'h^2 + (f + f'h) r_2 + (g + g'h) r_1 + r_1 r_2\big]. Every term in the bracket, divided by hh, goes to 0. By the linear-approximation theorem, (fg)′=f′g+fg′(fg)' = f'g + fg'.

    Chain rule. Let u=g(x)u = g(x) and k(h)=g(x+h)−g(x)k(h) = g(x+h) - g(x). Then k(h)=g′(x)h+rg(h)k(h) = g'(x)h + r_g(h) and k(h)→0k(h) \to 0.

    1. For ff at uu, write f(u+k)−f(u)=f′(u)k+ρ(k) kf(u + k) - f(u) = f'(u)k + \rho(k)\,k, where ρ(k)=rf(k)/k\rho(k) = r_f(k)/k for k≠0k \ne 0 and ρ(0)=0\rho(0) = 0. Then ρ(k)→0\rho(k) \to 0 as k→0k \to 0.
    2. So f(g(x+h))−f(g(x))=f′(u) k(h)+ρ(k(h)) k(h)f(g(x+h)) - f(g(x)) = f'(u)\,k(h) + \rho(k(h))\,k(h).
    3. Divide by hh: k(h)h→g′(x)\dfrac{k(h)}{h} \to g'(x), and ρ(k(h))→0\rho(k(h)) \to 0 because k(h)→0k(h) \to 0.
    4. The limit is f′(u) g′(x)f'(u)\,g'(x). ■\blacksquare

    Lesson 04 states the chain rule for many variables: the rates become Jacobian matrices, and "multiply" becomes matrix multiplication.

    The derivatives that ML uses #

    f(x)f(x) f′(x)f'(x) where it appears
    xnx^n nxn−1n x^{n-1} polynomials, squared error
    exe^x exe^x softmax, exponential families
    log⁡x\log x (x>0x > 0) 1/x1/x log-likelihood, cross-entropy
    sin⁡x\sin x, cos⁡x\cos x cos⁡x\cos x, −sin⁡x-\sin x positional encodings, Fourier features
    sigmoid σ(x)=11+e−x\sigma(x) = \dfrac{1}{1 + e^{-x}} σ(x)(1−σ(x))\sigma(x)\big(1 - \sigma(x)\big) logistic regression, gates in LSTMs
    tanh⁡x\tanh x 1−tanh⁡2x1 - \tanh^2 x RNNs, older MLPs
    softplus log⁡(1+ex)\log(1 + e^x) σ(x)\sigma(x) smooth ReLU, positive parameters
    log⁡σ(x)\log \sigma(x) 1−σ(x)=σ(−x)1 - \sigma(x) = \sigma(-x) logistic loss, GAN losses
    ReLU max⁡(0,x)\max(0, x) 11 if x>0x > 0, 00 if x<0x < 0 almost every modern network
    GELU x Φ(x)x\,\Phi(x) Φ(x)+x φ(x)\Phi(x) + x\,\varphi(x) transformers

    Here Φ\Phi and φ\varphi are the cumulative distribution function and the density of the standard normal distribution.

    Derivation of σ′\sigma', step by step.

    1. Write σ(x)=(1+e−x)−1\sigma(x) = (1 + e^{-x})^{-1}: an outer function u−1u^{-1} and an inner function u=1+e−xu = 1 + e^{-x}.
    2. Outer derivative: dduu−1=−u−2\dfrac{d}{du} u^{-1} = -u^{-2}. Inner derivative: dudx=−e−x\dfrac{du}{dx} = -e^{-x} (chain rule again, for e−xe^{-x}).
    3. Multiply: σ′(x)=−(1+e−x)−2⋅(−e−x)=e−x(1+e−x)2\sigma'(x) = -(1 + e^{-x})^{-2} \cdot (-e^{-x}) = \dfrac{e^{-x}}{(1 + e^{-x})^2}.
    4. Split the fraction: σ′(x)=11+e−x⋅e−x1+e−x\sigma'(x) = \dfrac{1}{1 + e^{-x}} \cdot \dfrac{e^{-x}}{1 + e^{-x}}.
    5. The first factor is σ(x)\sigma(x). The second is (1+e−x)−11+e−x=1−σ(x)\dfrac{(1 + e^{-x}) - 1}{1 + e^{-x}} = 1 - \sigma(x).
    6. So σ′(x)=σ(x)(1−σ(x))\sigma'(x) = \sigma(x)\big(1 - \sigma(x)\big).

    The product s(1−s)s(1 - s) of two numbers that add up to 1 is largest when s=1/2s = 1/2, that is, at x=0x = 0. So σ′(x)≤σ′(0)=1/4\sigma'(x) \le \sigma'(0) = 1/4.

    The same method gives the others. Softplus: ddxlog⁡(1+ex)=ex1+ex=σ(x)\dfrac{d}{dx}\log(1 + e^x) = \dfrac{e^x}{1 + e^x} = \sigma(x). Log-sigmoid: ddxlog⁡σ(x)=σ′(x)σ(x)=1−σ(x)\dfrac{d}{dx}\log\sigma(x) = \dfrac{\sigma'(x)}{\sigma(x)} = 1 - \sigma(x). Tanh, by the quotient rule on sinh⁡/cosh⁡\sinh/\cosh: tanh⁡′=(cosh⁡2−sinh⁡2)/cosh⁡2=1−tanh⁡2\tanh' = (\cosh^2 - \sinh^2)/\cosh^2 = 1 - \tanh^2.

    Quick check

    What is the derivative of softplus, f(x)=log⁡(1+ex)f(x) = \log(1 + e^x)?

    Second derivative, curvature and convexity #

    The second derivative f′′(x)f''(x) is the derivative of f′f'. It says how the slope changes.

    • f′′>0f'' > 0: the slope increases, the graph bends upwards (like x2x^2).
    • f′′<0f'' < 0: the slope decreases, the graph bends downwards (like −x2-x^2).

    A function is convex on an interval if every chord lies above the graph: f(λa+(1−λ)b)≤λf(a)+(1−λ)f(b)f\big(\lambda a + (1-\lambda) b\big) \le \lambda f(a) + (1 - \lambda) f(b) for all a,ba, b in the interval and λ∈[0,1]\lambda \in [0, 1].

    Theorem. If ff is twice differentiable on an open interval II, then ff is convex on II exactly when f′′(x)≥0f''(x) \ge 0 for all x∈Ix \in I.

    Examples: x2x^2, exe^x, −log⁡x-\log x (f′′=1/x2f'' = 1/x^2) and softplus (f′′=σ′>0f'' = \sigma' > 0) are convex. The sigmoid is not: σ′′=σ(1−σ)(1−2σ)\sigma'' = \sigma(1 - \sigma)(1 - 2\sigma) is positive for x<0x < 0 and negative for x>0x > 0. Note: f′′>0f'' > 0 everywhere implies strictly convex, but the converse is false. x4x^4 is strictly convex, yet f′′(0)=0f''(0) = 0.

    Critical points and the second-derivative test #

    A critical point is a point x∗x^* with f′(x∗)=0f'(x^*) = 0. If ff is differentiable and has a local minimum or maximum at an inner point x∗x^*, then f′(x∗)=0f'(x^*) = 0 (Fermat's theorem: otherwise a small step in the direction −f′(x∗)-f'(x^*) or +f′(x∗)+f'(x^*) would go lower or higher). The converse is false.

    Theorem (second-derivative test). Let f′(x∗)=0f'(x^*) = 0 and let f′′(x∗)f''(x^*) exist.

    • f′′(x∗)>0f''(x^*) > 0: x∗x^* is a strict local minimum.
    • f′′(x∗)<0f''(x^*) < 0: x∗x^* is a strict local maximum.
    • f′′(x∗)=0f''(x^*) = 0: the test says nothing.

    Proof of the first case. Taylor's theorem (next section) gives f(x∗+h)=f(x∗)+0⋅h+12f′′(x∗)h2+o(h2)f(x^* + h) = f(x^*) + 0 \cdot h + \tfrac12 f''(x^*) h^2 + o(h^2). For small h≠0h \ne 0 the term 12f′′(x∗)h2>0\tfrac12 f''(x^*)h^2 > 0 is larger than the o(h2)o(h^2) error, so f(x∗+h)>f(x∗)f(x^* + h) > f(x^*). ■\blacksquare

    The third case really is undecided. All three functions x3x^3, x4x^4 and −x4-x^4 have f′(0)=f′′(0)=0f'(0) = f''(0) = 0. But x4x^4 has a minimum at 0, −x4-x^4 a maximum, and x3x^3 neither (it goes up through 0: an inflection point).

    Taylor polynomials #

    The tangent line matches ff and f′f' at x0x_0. A parabola can also match f′′f''. In general, the Taylor polynomial of order kk at x0x_0 matches the first kk derivatives:

    Tk(x0+h)=f(x0)+f′(x0) h+f′′(x0)2 h2+⋯+f(k)(x0)k! hk.T_k(x_0 + h) = f(x_0) + f'(x_0)\,h + \frac{f''(x_0)}{2}\,h^2 + \dots + \frac{f^{(k)}(x_0)}{k!}\,h^k .

    T1T_1 is the tangent line. T2T_2 is the parabola that best fits the graph near x0x_0.

    Theorem (Taylor, Lagrange remainder). If ff is k+1k + 1 times differentiable on an interval that contains x0x_0 and x0+hx_0 + h, then

    f(x0+h)=Tk(x0+h)+f(k+1)(ξ)(k+1)! hk+1f(x_0 + h) = T_k(x_0 + h) + \frac{f^{(k+1)}(\xi)}{(k+1)!}\, h^{k+1}

    for some ξ\xi between x0x_0 and x0+hx_0 + h.

    So if ∣f(k+1)∣≤M|f^{(k+1)}| \le M near x0x_0, the error is at most M∣h∣k+1/(k+1)!M |h|^{k+1}/(k+1)!. The error has order hk+1h^{k+1}: halve hh, and the error becomes about 2k+12^{k+1} times smaller.

    Two examples at x0=0x_0 = 0, with h=0.1h = 0.1:

    • eh≈1+h+h2/2=1.105e^h \approx 1 + h + h^2/2 = 1.105. The true value is 1.1051709…1.1051709\ldots, so the error is 1.71⋅10−41.71 \cdot 10^{-4}. The remainder is eξh3/6e^\xi h^3/6 with 0<ξ<0.10 < \xi < 0.1: between 1.67⋅10−41.67 \cdot 10^{-4} and 1.84⋅10−41.84 \cdot 10^{-4}. ✓
    • log⁡(1+h)≈h−h2/2=0.095\log(1 + h) \approx h - h^2/2 = 0.095. The true value is 0.0953102…0.0953102\ldots, so the error is 3.10⋅10−43.10 \cdot 10^{-4}, close to the next term h3/3=3.33⋅10−4h^3/3 = 3.33 \cdot 10^{-4}.
    Interactive figure: loading…

    Raise the order from 0 to 4. The Taylor polynomial hugs the graph on a wider and wider band (error below 0.01). The readout shows the error at h=0.4h = 0.4 and h=0.2h = 0.2: their ratio is close to 2k+12^{k+1}. Drag the point to expand somewhere else. Try σ(x)\sigma(x): its order-2 polynomial at 00 is just the tangent line, because σ′′(0)=0\sigma''(0) = 0.

    Proof of Taylor's theorem with the Lagrange remainder

    Write b=x0+hb = x_0 + h. Choose the number MM so that the function

    g(t)=f(b)−∑j=0kf(j)(t)j!(b−t)j−M (b−t)k+1(k+1)!g(t) = f(b) - \sum_{j=0}^{k} \frac{f^{(j)}(t)}{j!}(b - t)^j - M\,\frac{(b - t)^{k+1}}{(k+1)!}

    has g(x0)=0g(x_0) = 0. (This is one linear equation for MM.)

    1. At t=bt = b every power of (b−t)(b - t) is 0 except the j=0j = 0 term, so g(b)=f(b)−f(b)=0g(b) = f(b) - f(b) = 0.
    2. By Rolle's theorem there is a ξ\xi strictly between x0x_0 and bb with g′(ξ)=0g'(\xi) = 0.
    3. Differentiate the sum term by term with the product rule. The terms cancel in pairs (a telescoping sum), and only −f(k+1)(t)k!(b−t)k-\dfrac{f^{(k+1)}(t)}{k!}(b - t)^k is left. So g′(t)=−f(k+1)(t)k!(b−t)k+M (b−t)kk!g'(t) = -\dfrac{f^{(k+1)}(t)}{k!}(b - t)^k + M\,\dfrac{(b - t)^k}{k!}.
    4. g′(ξ)=0g'(\xi) = 0 and b−ξ≠0b - \xi \ne 0 give M=f(k+1)(ξ)M = f^{(k+1)}(\xi).
    5. Put this MM in g(x0)=0g(x_0) = 0: this is the theorem. ■\blacksquare

    First use: gradient descent in one variable #

    To minimize ff, gradient descent repeats

    xk+1=xk−η f′(xk),x_{k+1} = x_k - \eta\, f'(x_k),

    with a step size (learning rate) η>0\eta > 0.

    The sign logic. If f′(xk)>0f'(x_k) > 0, ff goes up to the right, so we step to the left. If f′(xk)<0f'(x_k) < 0, we step to the right. In both cases the linear approximation says f(xk+1)≈f(xk)−ηf′(xk)2<f(xk)f(x_{k+1}) \approx f(x_k) - \eta f'(x_k)^2 < f(x_k): a small enough step always goes down.

    How large may η\eta be? Take f(x)=a2x2f(x) = \tfrac a2 x^2 with a>0a > 0 (minimum at 0, f′′=af'' = a).

    1. f′(x)=axf'(x) = a x, so one step gives xk+1=xk−ηaxk=(1−ηa) xkx_{k+1} = x_k - \eta a x_k = (1 - \eta a)\, x_k.
    2. After kk steps: xk=(1−ηa)kx0x_k = (1 - \eta a)^k x_0.
    3. This goes to 0 for every start x0x_0 exactly when ∣1−ηa∣<1|1 - \eta a| < 1, that is, 0<η<2/a\boxed{0 < \eta < 2/a}.

    The five cases:

    step size factor 1−ηa1 - \eta a what happens
    0<η<1/a0 < \eta < 1/a between 0 and 1 monotone convergence
    η=1/a\eta = 1/a 0 the minimum in one step
    1/a<η<2/a1/a < \eta < 2/a between −1-1 and 0 jumps over 0, still converges
    η=2/a\eta = 2/a −1-1 bounces between x0x_0 and −x0-x_0 forever
    η>2/a\eta > 2/a below −1-1 diverges

    Curvature limits the step size. For a general ff, near a minimum x∗x^* with f′′(x∗)>0f''(x^*) > 0, the Taylor polynomial says ff looks like a parabola with a=f′′(x∗)a = f''(x^*). So gradient descent can settle at x∗x^* only if η<2/f′′(x∗)\eta < 2/f''(x^*). Sharper minima need smaller steps.

    Interactive figure: loading…

    Predict, then move the step size η\eta. On a2x2\tfrac a2 x^2 the readout shows the factor 1−ηa1 - \eta a that multiplies xx at every step; the right panel plots xkx_k against kk. Cross η=1/a\eta = 1/a (the steps start to jump over 0) and then 2/a2/a (they diverge). On the non-convex quartic, drag the start point: it decides which minimum you reach.

    Answers to section 1. (1) 1/41/4. σ′=σ(1−σ)\sigma' = \sigma(1-\sigma) is largest at σ=1/2\sigma = 1/2, that is, at x=0x = 0. (2) No. Here a=10a = 10, so the condition is η<2/10=0.2\eta < 2/10 = 0.2. With η=0.21\eta = 0.21 each step multiplies xx by 1−2.1=−1.11 - 2.1 = -1.1: xx changes sign and grows, and the iterates diverge. (3) No. Near 0, ReLU is max⁡(0,x)\max(0, x) at every zoom level: the corner never straightens, and r(h)/hr(h)/h does not go to 0. So ReLU has no derivative at 0.

    Quick check

    Gradient descent on f(x)=5x2f(x) = 5x^2. For which step sizes η\eta does it converge to 0 from every start?

    Quick check

    f(x)=x3f(x) = x^3 has f′(0)=0f'(0) = 0 and f′′(0)=0f''(0) = 0. What is x=0x = 0?

    4. Worked example #

    The logistic loss of one example with label +1+1, as a function of one weight ww (input 1):

    ℓ(w)=log⁡(1+e−w).\ell(w) = \log\big(1 + e^{-w}\big).

    (a) First derivative, by the chain rule. Outer function log⁡u\log u, inner function u=1+e−wu = 1 + e^{-w}.

    ℓ′(w)=11+e−w⋅(−e−w)=−e−w1+e−w=−1ew+1=−σ(−w)=σ(w)−1.\begin{aligned} \ell'(w) &= \frac{1}{1 + e^{-w}} \cdot \big(-e^{-w}\big) = -\frac{e^{-w}}{1 + e^{-w}} \\ &= -\frac{1}{e^{w} + 1} = -\sigma(-w) = \sigma(w) - 1 . \end{aligned}

    (We divided the top and the bottom by e−we^{-w}, then used σ(−w)=1−σ(w)\sigma(-w) = 1 - \sigma(w).) At w=0w = 0: ℓ′(0)=1/2−1=−1/2\ell'(0) = 1/2 - 1 = -1/2. The derivative is negative for every ww: the loss always decreases when ww grows.

    (b) Second derivative. ℓ′′(w)=σ′(w)=σ(w)(1−σ(w))>0\ell''(w) = \sigma'(w) = \sigma(w)\big(1 - \sigma(w)\big) > 0. So ℓ\ell is convex. At 0: ℓ′′(0)=1/4\ell''(0) = 1/4.

    (c) Taylor polynomial of order 2 at 0. ℓ(0)=log⁡2≈0.6931\ell(0) = \log 2 \approx 0.6931, so

    T2(h)=log⁡2−h2+14⋅h22=log⁡2−h2+h28.T_2(h) = \log 2 - \frac{h}{2} + \frac{1}{4}\cdot\frac{h^2}{2} = \log 2 - \frac h2 + \frac{h^2}{8}.

    At h=1h = 1: T2(1)=0.6931−0.5+0.125=0.3181T_2(1) = 0.6931 - 0.5 + 0.125 = 0.3181. The true value is ℓ(1)=log⁡(1+e−1)=0.3133\ell(1) = \log(1 + e^{-1}) = 0.3133. The error is −0.0049-0.0049. The Lagrange bound uses ∣ℓ′′′∣=∣σ′′∣≤1/(63)≈0.096|\ell'''| = |\sigma''| \le 1/(6\sqrt3) \approx 0.096: the error is at most 0.096/6=0.0160.096/6 = 0.016. ✓

    (d) One gradient-descent step from w0=0w_0 = 0 with η=1\eta = 1: w1=0−1⋅(−1/2)=0.5w_1 = 0 - 1 \cdot (-1/2) = 0.5. The loss goes from 0.69310.6931 to ℓ(0.5)=0.4741\ell(0.5) = 0.4741. The linear approximation predicted 0.6931−0.5⋅0.5=0.44310.6931 - 0.5 \cdot 0.5 = 0.4431; the curvature makes the real decrease a bit smaller. The quadratic T2(0.5)=0.4744T_2(0.5) = 0.4744 is almost exact.

    (e) A look ahead. T2T_2 has its minimum where T2′(h)=−1/2+h/4=0T_2'(h) = -1/2 + h/4 = 0, at h=2h = 2. Jumping to the minimum of the quadratic model is Newton's method (lesson 03). Here ℓ\ell itself has no minimum: ℓ′<0\ell' < 0 everywhere, and ℓ(w)→0\ell(w) \to 0 only as w→∞w \to \infty.

    5. In code #

    NumPy checks the worked example. The central difference (f(x+h)−f(x−h))/(2h)\big(f(x+h) - f(x-h)\big)/(2h) is a numerical derivative (lesson 07 studies its error).

    import numpy as np
    
    def sigmoid(x):
        e = np.exp(-np.abs(x))                 # never exp of a large positive number
        return np.where(x >= 0, 1 / (1 + e), e / (1 + e))
    
    loss = lambda w: np.log1p(np.exp(-w))      # logistic loss of one positive example
    dloss = lambda w: sigmoid(w) - 1           # (a) first derivative
    d2loss = lambda w: sigmoid(w) * (1 - sigmoid(w))   # (b) second derivative
    
    # check (a) and (b) with central differences
    h = 1e-5
    print(dloss(0.0), (loss(h) - loss(-h)) / (2 * h))     # -0.5 -0.49999999999772443
    print(d2loss(0.0), (dloss(h) - dloss(-h)) / (2 * h))  # 0.25 0.24999999999608666
    
    # (c) second-order Taylor polynomial at 0, evaluated at w = 1
    T2 = lambda h: np.log(2) - h / 2 + h**2 / 8
    print(loss(1.0), T2(1.0), loss(1.0) - T2(1.0))        # 0.3132... 0.3181... -0.004885...
    
    # (d) one gradient-descent step with eta = 1
    w1 = 0.0 - 1.0 * dloss(0.0)
    print(w1, loss(w1))                                   # 0.5 0.4740769841801067
    
    # sigma' stays finite at |x| = 1000
    x = np.array([-1000., 0., 1000.])
    print(sigmoid(x) * (1 - sigmoid(x)))                  # [0.   0.25 0.  ]
    

    The formula e−x/(1+e−x)2e^{-x}/(1 + e^{-x})^2 is the same function as σ(x)(1−σ(x))\sigma(x)(1 - \sigma(x)), but in floating point it gives nan at x=−1000x = -1000 (e1000e^{1000} overflows to inf, and inf/inf is nan). The stable sigmoid above avoids this.

    6. Where it shows up #

    • Activation functions. Sigmoid and tanh saturate: for large ∣x∣|x| their derivatives are almost 0. Since σ′≤1/4\sigma' \le 1/4, a chain of 10 sigmoids multiplies the gradient by at most 4−10≈10−64^{-10} \approx 10^{-6} (before the weights). This is one cause of vanishing gradients, and one reason ReLU and GELU replaced them in hidden layers.
    • Logistic regression and classifiers. The derivative of the logistic loss with respect to the logit zz is σ(z)−y\sigma(z) - y for a label y∈{0,1}y \in \{0, 1\}: "prediction minus target". The worked example is the case y=1y = 1. The same pattern appears for softmax with cross-entropy (lesson 06).
    • Learning-rate limits. On a quadratic, η<2/a\eta < 2/a. In deep networks the largest Hessian eigenvalue (the "sharpness") plays the role of aa. Cohen et al. (2021) observed that full-batch gradient descent drives the sharpness up to about 2/η2/\eta and then stays near this "edge of stability".
    • Numerical methods in ML for science. The gradient-descent step on a2x2\tfrac a2 x^2 is the explicit Euler method for the ODE x˙=−ax\dot x = -a x with time step η\eta. Its stability rule, η<2/a\eta < 2/a, is the same: stiff physical systems (large aa) force small time steps.
    • Taylor expansions everywhere. Finite-difference gradient checks (lesson 07), Newton and Gauss-Newton methods (lessons 03 and 08), and the Laplace approximation (lesson 11) all start from a Taylor polynomial.

    7. Common mistakes #

    • Forgetting the inner derivative. ddxe−x2/2\dfrac{d}{dx} e^{-x^2/2} is not e−x2/2e^{-x^2/2}. The chain rule gives −x e−x2/2-x\,e^{-x^2/2}.
    • (fg)′=f′g′(fg)' = f'g'. Wrong. The product rule is f′g+fg′f'g + fg'. Check with f=g=xf = g = x: (x2)′=2x(x^2)' = 2x, not 11.
    • "f′(x∗)=0f'(x^*) = 0, so x∗x^* is a minimum." It may be a maximum or an inflection point (x3x^3 at 0).
    • "f′′(x∗)=0f''(x^*) = 0, so x∗x^* is not a minimum." x4x^4 has a minimum at 0 with f′′(0)=0f''(0) = 0. The test is silent there.
    • Computing σ′\sigma' with an unstable formula. e−x/(1+e−x)2e^{-x}/(1 + e^{-x})^2 gives nan for x≈−1000x \approx -1000. Use σ(1−σ)\sigma(1 - \sigma) with a stable σ\sigma.
    • "A larger learning rate is always faster." On a2x2\tfrac a2 x^2, the best rate is η=1/a\eta = 1/a. Beyond 2/a2/a the method diverges.
    • ddxlog⁡x=1/x\dfrac{d}{dx}\log x = 1/x for all xx. log⁡x\log x exists only for x>0x > 0. For x≠0x \ne 0, ddxlog⁡∣x∣=1/x\dfrac{d}{dx}\log|x| = 1/x.

    8. Check yourself #

    1. Use the limit definition to find the derivative of f(x)=1/xf(x) = 1/x at x=2x = 2.
      Answerf(2+h)−f(2)h=1h(12+h−12)=1h⋅2−(2+h)2(2+h)=−12(2+h)\dfrac{f(2+h) - f(2)}{h} = \dfrac{1}{h}\Big(\dfrac{1}{2+h} - \dfrac12\Big) = \dfrac{1}{h}\cdot\dfrac{2 - (2+h)}{2(2+h)} = \dfrac{-1}{2(2+h)}. As h→0h \to 0 this tends to −1/4-1/4. (Check with the rule (x−1)′=−x−2=−1/4(x^{-1})' = -x^{-2} = -1/4.)
    2. Find the critical points of f(x)=x3−3xf(x) = x^3 - 3x and classify them.
      Answerf′(x)=3x2−3=0f'(x) = 3x^2 - 3 = 0 gives x=±1x = \pm 1. f′′(x)=6xf''(x) = 6x. At x=1x = 1: f′′=6>0f'' = 6 > 0, a strict local minimum with f(1)=−2f(1) = -2. At x=−1x = -1: f′′=−6<0f'' = -6 < 0, a strict local maximum with f(−1)=2f(-1) = 2.
    3. Write the order-2 Taylor polynomial of cos⁡x\cos x at 0. Bound its error at h=0.1h = 0.1.
      AnswerT2(h)=1−h2/2T_2(h) = 1 - h^2/2. The h3h^3 coefficient of cos⁡\cos is 0, so T2=T3T_2 = T_3, and the Lagrange remainder of order 3 applies: ∣cos⁡(ξ) h4/24∣≤h4/24≈4.17⋅10−6|\cos(\xi)\,h^4/24| \le h^4/24 \approx 4.17 \cdot 10^{-6}. The true error is cos⁡(0.1)−0.995=4.165⋅10−6\cos(0.1) - 0.995 = 4.165 \cdot 10^{-6}.
    4. Gradient descent on f(x)=2x2f(x) = 2x^2 with η=0.3\eta = 0.3, from x0=1x_0 = 1. Give x1x_1 and x2x_2. Does it converge?
      Answerf′(x)=4xf'(x) = 4x, so xk+1=(1−1.2)xk=−0.2 xkx_{k+1} = (1 - 1.2)x_k = -0.2\,x_k. Then x1=−0.2x_1 = -0.2 and x2=0.04x_2 = 0.04. It converges, with jumps over 0, because 1/a=0.25<η=0.3<2/a=0.51/a = 0.25 < \eta = 0.3 < 2/a = 0.5.
    5. Show that tanh⁡(x)=2σ(2x)−1\tanh(x) = 2\sigma(2x) - 1 gives tanh⁡′(0)=1\tanh'(0) = 1, and check it with tanh⁡′=1−tanh⁡2\tanh' = 1 - \tanh^2.
      AnswerChain rule: tanh⁡′(x)=2⋅σ′(2x)⋅2=4σ′(2x)\tanh'(x) = 2 \cdot \sigma'(2x) \cdot 2 = 4\sigma'(2x). At 0: 4⋅1/4=14 \cdot 1/4 = 1. Also 1−tanh⁡2(0)=1−0=11 - \tanh^2(0) = 1 - 0 = 1. ✓
    6. Why does it not matter much in practice that ReLU has no derivative at 0?
      AnswerThe kink is one point, and a pre-activation hits exactly 0 only rarely. At that point frameworks use the value 0, which is a valid subgradient (any value in [0,1][0, 1] is). Everywhere else the derivative exists and is 0 or 1.
    Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
    1. Define the derivative f′(x)f'(x) as a limit.

    2. Write the linear approximation of ff near xx, with its error condition.

    3. State the chain rule in one variable.

    4. Write σ′(x)\sigma'(x) in terms of σ(x)\sigma(x). What is its largest value?

    5. What is the derivative of softplus, log⁡(1+ex)\log(1 + e^x)?

    6. What is tanh⁡′(x)\tanh'(x)?

    7. What does the second-derivative test say at a critical point x∗x^*?

    8. Write the order-2 Taylor polynomial of ff at x0x_0 and the order of its error.

    9. Gradient descent on f(x)=a2x2f(x) = \tfrac a2 x^2: what does one step do to xx, and when does it converge?

    Open the review →

    9. Interview angle #

    • "Derive the derivative of the sigmoid." — Chain rule on (1+e−x)−1(1 + e^{-x})^{-1}, then split the fraction: σ(1−σ)\sigma(1 - \sigma). Mention that it is at most 1/41/4 (vanishing gradients).
    • "ReLU is not differentiable at 0. Why can we train with it?" — The kink is a single point, frameworks use the subgradient value 0 there, and gradient methods only need a valid subgradient at such points.
    • "What is the largest stable learning rate for f(x)=a2x2f(x) = \tfrac a2 x^2?" — η<2/a\eta < 2/a, because each step multiplies xx by 1−ηa1 - \eta a. In many dimensions: η<2/λmax⁡\eta < 2/\lambda_{\max} of the Hessian.
    • "Is a point with f′=0f' = 0 and f′′=0f'' = 0 a minimum?" — The test cannot decide: compare x3x^3, x4x^4, −x4-x^4.
    • "Why is the derivative of the log-loss with respect to the logit σ(z)−y\sigma(z) - y?" — Chain rule through log⁡σ\log\sigma, using (log⁡σ)′=1−σ(\log\sigma)' = 1 - \sigma.

    10. Practice #

    The notebook for this lesson runs in Colab. It has five short exercises, each with a check cell and a folded solution:

    1. Implement a stable sigmoid, its derivative, tanh⁡′\tanh', softplus and its derivative; check them against central differences and at x=±1000x = \pm 1000.
    2. Differentiate a polynomial given by its coefficient array.
    3. Evaluate a Taylor polynomial from a list of derivatives, and measure the error order on exe^x.
    4. Run gradient descent on a2x2\tfrac a2 x^2 and find the stability threshold 2/a2/a by experiment.
    5. Implement the second difference (f(x+h)−2f(x)+f(x−h))/h2\big(f(x+h) - 2f(x) + f(x-h)\big)/h^2 and show that its error is O(h2)O(h^2).

    11. Watch and read #

    The derivative as the limit of "rise over run"; compare with the secant figure.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    Why the chain rule multiplies rates: the same picture as section 3.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    How each new term matches one more derivative; compare with the Taylor figure.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play