Calculus for Machine Learning
understanding derivatives, optimization, and the mathematical foundations behind learning algorithms
Core Concepts Covered
- Limits, continuity, and single-variable derivatives
- Multivariable calculus: partial derivatives and gradients
- Optimization fundamentals and Lagrange multipliers
1. Limits, Continuity, and Derivatives
Training a model means changing its parameters in a direction that reduces error. Calculus gives us the language for describing that change, beginning with limits, continuity, and derivatives of one variable.
A limit describes the behavior of a function as its input approaches a specific target value. Formally, we write:
A function is continuous if its limit as approaches equals . Differentiability is stronger: a derivative must exist locally. Smooth losses give ordinary gradient descent a well-defined direction, but useful objectives such as MAE and penalties have isolated kinks and can still be optimized with subgradients or related methods.
The derivative of a function measures its instantaneous rate of change. Geometrically, it is the slope of the tangent line to the curve at a specific point. Mathematically, it is defined as:
To find derivatives of complex, nested networks, we use four standard rules:
• Power Rule: .
• Product Rule: .
• Quotient Rule: .
• Chain Rule (for composite layers): If , let , then: ,
Chain Rule Example: Suppose we have a nested function . Let the inner function be (with derivative ), and the outer function be (with derivative ). Applying the Chain Rule:
Secant Slopes Approach the Tangent
Read diagram labels
- tangent: f'(x)
- secant
- h → 0
A Limit and Its Tangent Slope
For , evaluate and interpret the result.
1.Substitute : .
2.For , simplify to . Therefore, .
The derivative is , so . The tangent line at is .
The derivative is the limiting slope of secant lines, not merely a symbolic rule.
Product and Chain Rules Together
Differentiate .
1.Use the product rule with and : .
2.Because and the chain rule gives , .
Factor the common term: .
2. Multivariable Calculus: Partial Derivatives & Gradients
In machine learning, models have millions of input parameters (weights and biases). To measure how changing each parameter affects the overall loss, we use partial derivatives.
A partial derivative, written as , measures the rate of change of a multivariable function with respect to one variable , while holding all other variables constant.
The Gradient of a multivariable function, denoted by (read as 'nabla f'), is a vector of all its partial derivatives:
Concrete Gradient Example: Let . Let's compute its gradient vector : - The partial derivative with respect to (treating as a constant) is: . - The partial derivative with respect to (treating as a constant) is: . - Therefore, the gradient vector is: At the specific coordinate point , the gradient is evaluated as: .
A unit vector defines a direction. The directional derivative is ; it is largest when points along the gradient and most negative in the opposite direction.
Geometrically, the gradient vector points in the direction of the steepest ascent (fastest increase). By stepping in the exact opposite direction of the gradient (), gradient descent moves down the steepest path to find the local minimum.
Gradient, Descent, and a Constraint
Read diagram labels
- −∇f steps
- constraint g(x,y)=0
- ∇g ∥ ∇f
Gradient and Directional Change
Let . At , find the gradient and the directional derivative toward .
1., so .
2..
The negative value means the function decreases at an instantaneous rate of per unit distance in that direction.
3. Multivariable Optimization and Lagrange Multipliers
Unconstrained optimization is the process of locating a function's maximum or minimum points by finding where its gradient is equal to the zero vector: .
In many machine learning algorithms, however, our parameters are subject to strict constraints. For instance, Support Vector Machines (SVMs) must maximize decision margins subject to classification boundaries. We solve these constrained systems using the Method of Lagrange Multipliers.
To find the local minimum of a function subject to an equality constraint , we introduce a new variable (the Lagrange Multiplier) and construct the Lagrangian function: ,
We find critical points of the Lagrangian by setting its gradient with respect to all variables ( and ) to zero: ,
The Hessian distinguishes curvature at a stationary point. A positive-definite Hessian indicates a strict local minimum, a negative-definite Hessian a strict local maximum, and mixed-sign curvature a saddle point.
Constraint Example: Suppose we want to maximize our model's feature coverage area subject to a perimeter boundary constraint . We construct the Lagrangian: Taking partial derivatives and setting them to zero: Since and , we find . Substituting into the constraint . This yields the optimal dimensions .
Constrained Optimum: Tangent Level Set and Feasible Curve
Read diagram labels
- ∇f
- ∇g = λ⁻¹∇f
- g(x,y)=0
Classifying a Critical Point
Find and classify the critical point of .
1.. Solving gives .
2.The Hessian is . Its leading principal minors are and .
Therefore is positive definite and is a strict minimum.
Cumulative Constraint Problem
Minimize subject to .
1.Form .
2. and imply .
The constraint gives , hence and the minimum value is .
At the constrained optimum, the objective and constraint gradients are parallel.
Interactive Practice Quiz
Test your understanding with instant feedback
Which differentiation rule is used to compute the derivative of a nested composite function like ?
Which of the following describes the geometric meaning of the Gradient Vector ?
Compute the partial derivative of the function :
Why are smooth, differentiable losses convenient for ordinary gradient descent?
What is the purpose of constructing the Lagrangian function ?
Evaluate the limit: ?
Compute the derivative of the function using the Power Rule:
Apply the Product Rule to differentiate the function :
If the gradient of a loss function is evaluated as at a specific point, what direction should gradient descent take to decrease the loss?
In multivariable optimization, what is a 'Critical Point' of an unconstrained function?
Further Readings
Explore these highly recommended external references to deepen your understanding
