Lesson 4 of 7 · Calculus
Partial derivatives
For multivariable functions we differentiate with respect to one variable while holding the others fixed; this is how we learn the effect of each weight on the loss in isolation.
Lesson contents
1 Introduction
The loss function of a real model depends on thousands or billions of weights. A partial derivative measures the rate of change of the function with respect to one variable, treating the other variables as constants.
Learning objectives
- Computing first-order partial derivatives
- Interpreting the partial derivative geometrically
- Computing second partial derivatives and the Hessian
- Computing the derivatives of a linear model's loss with respect to w and b
2 Why study this topic?
Training needs the effect of each weight \(w_i\) on the loss \(L\), that is \(\partial L/\partial w_i\). Gathering these partial derivatives into a vector gives the gradient that drives gradient descent.
3 Theoretical explanation
Definition
\[\frac{\partial f}{\partial x}(x,y) = \lim_{h\to0}\frac{f(x+h,y) - f(x,y)}{h}\]
We differentiate with respect to \(x\) treating \(y\) as a constant, and vice versa.
Geometric interpretation
Cutting the surface \(z = f(x,y)\) with the plane \(y = y_0\) gives a curve; the slope of this curve is \(\partial f/\partial x\).
Second derivatives and the Hessian
\[H = \begin{bmatrix} f_{xx} & f_{xy} \\ f_{yx} & f_{yy}\end{bmatrix}\]
If the second derivatives are continuous, \(f_{xy} = f_{yx}\) (Schwarz's theorem), so the Hessian is symmetric.
An example from machine learning
For the model \(\hat y = wx + b\) and the single-sample loss \(L = (wx + b - y)^2\): \(\partial L/\partial w = 2(wx+b-y)x\) and \(\partial L/\partial b = 2(wx+b-y)\).
4 Mathematical definitions
- Partial derivative
- The derivative of a multivariable function with respect to one variable with the others held fixed.\[\frac{\partial f}{\partial x_i}\]
- Hessian matrix
- The matrix of second partial derivatives; it describes the curvature of the function.\[H_{ij} = \frac{\partial^2 f}{\partial x_i\,\partial x_j}\]
- Partial derivative
- The derivative of a multivariable function with respect to one variable, with the other variables held fixed.\[\frac{\partial f}{\partial x_i}\]
- Hessian matrix
- The matrix of second partial derivatives of a multivariable function; it describes its curvature.\[H_{ij}=\partial^2 f/\partial x_i\partial x_j\]
5 The equations
6 Step-by-step worked example
Two partial derivatives
Problem: Let \(f(x,y) = x^2y + 3y^2\). Compute \(f_x\) and \(f_y\) at \((1,2)\).
- \(f_x = 2xy\) (treating y as a constant).
- \(f_y = x^2 + 6y\) (treating x as a constant).
- At (1,2): \(f_x = 2\cdot1\cdot2 = 4\) and \(f_y = 1 + 12 = 13\).
Result: \(f_x(1,2) = 4\) and \(f_y(1,2) = 13\)
The gradient of a linear model's loss
Problem: For the sample \(x = 2, y = 5\) and the model \(w = 1, b = 0.5\), compute \(\partial L/\partial w\) and \(\partial L/\partial b\) for the loss \(L = (wx+b-y)^2\).
- The prediction is \(\hat y = 1\cdot2 + 0.5 = 2.5\), and the error is \(2.5 - 5 = -2.5\).
- \(\partial L/\partial w = 2(-2.5)(2) = -10\).
- \(\partial L/\partial b = 2(-2.5) = -5\).
Result: Both derivatives are negative, so increasing w and b decreases the loss.
7 Practical application in AI
- Training: Each weight is updated with its partial derivative: \(w_i \leftarrow w_i - \eta\,\partial L/\partial w_i\).
- Sensitivity analysis: The partial derivative of the model's output with respect to an input feature gives a saliency map that explains its decision.
- Second-order methods: Newton's method uses the Hessian to choose the update step.
8 Interactive lab linked to the lesson
Plotting functions, computing the derivative numerically and drawing the tangent, partial derivatives and the gradient of a two-variable function, the chain rule step by step, and numerical integration with the trapezoidal and Simpson's rules.
Open the Calculus Lab9 Practice exercises
Review questions for reflection
- How many partial derivatives do we need for a model with a million weights?
- Why is the Hessian usually symmetric?
Common mistakes
- Forgetting to hold the other variables fixed while differentiating.
- Confusing \(\partial\) with \(d\).
- Dropping the factor x in \(\partial L/\partial w\).
10 Lesson summary
- A partial derivative holds every variable fixed except one.
- The second partial derivatives form the Hessian matrix.
- Training models needs a partial derivative for every weight.
11 Short quiz
5 questions · 10 minutes · pass mark 60% — passing it completes the lesson.
