← Tensor by Tensor
§ Math Fundamentals for AI
Tensor by Tensor · Complete Guide

Differentiation rules

Fourteen lessons that connect high-school mathematics to AI. Start with the plain-language explanation and a small example. Then read the full formulas, derivations, and Python code at your own pace.

How to study a lesson

Read the opening example. Try the question before revealing its answer. Then work through the detailed notes below it. You do not need to understand every proof on the first reading.

Symbols: a subscript such as xi identifies one entry. Σ means add a collection of terms. ∈ means “belongs to”. The symbol ≈ means “approximately equal”, not exactly equal. A parameter is an adjustable number in a model.

01.06 · Derivation Rules & Examples

Introduction

Repeatedly rebuilding a derivative from limits is slow. For a rule containing three times a squared input plus five times the input, a small toolbox differentiates each term and gives a slope of 17 at input 2.

Learning goal

Choose and apply standard derivative rules correctly to polynomial, exponential, logarithmic, trigonometric, and activation functions.

Before you start

Functions, derivatives as local slopes, exponents, logarithms, and basic algebra.

Lesson plan

  1. Build a toolbox from constant, power, sum, product, and quotient rules.
  2. Differentiate exponential, logarithmic, trigonometric, and common neural-network activation functions carefully.
  3. Combine the rules in worked expressions and check the result at a value.

Choose the rule from the expression. A sum needs the sum rule. A multiplication of two changing functions needs the product rule. A function inside another function needs the chain rule. First mark the outermost operation; then work inward.

The toolbox of differentiation

Rather than returning to the limit definition every time, we use a small set of rules that compose to differentiate almost any function encountered in machine learning.

Core differentiation rules
RuleFormulaExample
Constant$\frac{d}{dx}[c] = 0$$\frac{d}{dx}[5] = 0$
Power$\frac{d}{dx}[x^n] = nx^{n-1}$$\frac{d}{dx}[x^3] = 3x^2$
Sum$(f+g)' = f' + g'$$\frac{d}{dx}[x^2+3x] = 2x+3$
Scalar multiple$(cf)' = cf'$$\frac{d}{dx}[5x^2] = 10x$
Product$(fg)' = f'g + fg'$$\frac{d}{dx}[x^2\sin x] = 2x\sin x + x^2\cos x$
Quotient$\left(\frac{f}{g}\right)' = \frac{f'g - fg'}{g^2}$$\frac{d}{dx}\!\left[\frac{x}{x+1}\right] = \frac{1}{(x+1)^2}$
Chain$(f(g(x)))' = f'(g(x)) \cdot g'(x)$$\frac{d}{dx}[\sin(x^2)] = \cos(x^2)\cdot 2x$

Exponentials and logarithms

These two functions are central to information theory and probability in ML:

$$\frac{d}{dx}[e^x] = e^x, \qquad \frac{d}{dx}[\ln x] = \frac{1}{x}, \qquad \frac{d}{dx}[a^x] = a^x \ln a$$
Example · Softmax gradient

The softmax function $p_i = \frac{e^{z_i}}{\sum_j e^{z_j}}$ appears in classification. Its derivative with respect to $z_i$ is:

$$\frac{\partial p_i}{\partial z_i} = p_i(1 - p_i)$$

This follows from the quotient rule and the fact that $\frac{d}{dz_i}[e^{z_i}] = e^{z_i}$.

Trigonometric functions

$$\frac{d}{dx}[\sin x] = \cos x, \qquad \frac{d}{dx}[\cos x] = -\sin x, \qquad \frac{d}{dx}[\tan x] = \sec^2 x$$

Activation functions in neural networks

Every activation function must be differentiable (almost everywhere) so gradients can flow backward:

FunctionFormulaDerivative
Sigmoid$\sigma(z) = \frac{1}{1+e^{-z}}$$\sigma(z)(1-\sigma(z))$
Tanh$\tanh(z)$$1 - \tanh^2(z)$
ReLU$\max(0, z)$$\begin{cases}1 & z>0\\0 & z<0\end{cases}$
Leaky ReLU$\max(\alpha z, z)$$\begin{cases}1 & z>0\\\alpha & z<0\end{cases}$
GELU$z\,\Phi(z)$$\Phi(z) + z\,\phi(z)$
Common mistake

The product rule is not $(fg)' = f'g'$. That's the chain rule applied to something else. The product rule always has two terms: $f'g + fg'$.

Worked examples

Example 1: Mean squared error

$L(\theta) = \frac{1}{N}\sum_{i=1}^N (f_\theta(x_i) - y_i)^2$. Using the chain rule and power rule:

$$\frac{\partial L}{\partial \theta} = \frac{2}{N}\sum_{i=1}^N (f_\theta(x_i) - y_i) \cdot \frac{\partial f_\theta(x_i)}{\partial \theta}$$

Example 2: Cross-entropy loss

$L = -\sum_i y_i \log p_i$. With respect to $p_j$:

$$\frac{\partial L}{\partial p_j} = -\frac{y_j}{p_j}$$

Example 3: L2 regularisation

$R(\theta) = \frac{\lambda}{2}\|\theta\|^2 = \frac{\lambda}{2}\sum_i \theta_i^2$. Then:

$$\frac{\partial R}{\partial \theta_j} = \lambda\theta_j \implies \nabla_\theta R = \lambda\theta$$