Guide To AI Logo
Unit 06

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:

limxcf(x)=L\lim_{x \to c} f(x) = L

A function is continuous if its limit as xx approaches cc equals f(c)f(c). 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 L1L_1 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:

f(x)=limh0f(x+h)f(x)hf'(x) = \lim_{h \to 0} \frac{f(x+h) - f(x)}{h}

To find derivatives of complex, nested networks, we use four standard rules:

Power Rule: ddx(xn)=nxn1\frac{d}{dx}(x^n) = n x^{n-1}.

Product Rule: ddx(f(x)g(x))=f(x)g(x)+f(x)g(x)\frac{d}{dx}(f(x)g(x)) = f'(x)g(x) + f(x)g'(x).

Quotient Rule: ddx(f(x)g(x))=f(x)g(x)f(x)g(x)(g(x))2\frac{d}{dx}\left(\frac{f(x)}{g(x)}\right) = \frac{f'(x)g(x) - f(x)g'(x)}{(g(x))^2}.

Chain Rule (for composite layers): If y=f(g(x))y = f(g(x)), let u=g(x)u = g(x), then: dydx=dydududx=f(g(x))g(x)\frac{dy}{dx} = \frac{dy}{du} \cdot \frac{du}{dx} = f'(g(x)) \cdot g'(x),

Chain Rule Example: Suppose we have a nested function y=(3x2+1)4y = (3x^2 + 1)^4. Let the inner function be u=g(x)=3x2+1u = g(x) = 3x^2 + 1 (with derivative g(x)=6xg'(x) = 6x), and the outer function be y=f(u)=u4y = f(u) = u^4 (with derivative f(u)=4u3f'(u) = 4u^3). Applying the Chain Rule: dydx=4(3x2+1)3(6x)=24x(3x2+1)3\frac{dy}{dx} = 4(3x^2 + 1)^3 \cdot (6x) = 24x(3x^2 + 1)^3

Secant Slopes Approach the Tangent

tangent: f'(x)secanth → 0
Read diagram labels
  • tangent: f'(x)
  • secant
  • h → 0
Worked Example 1

A Limit and Its Tangent Slope

Problem

For f(x)=x2f(x)=x^2, evaluate limh0f(3+h)f(3)h\lim_{h\to0}\frac{f(3+h)-f(3)}{h} and interpret the result.

Step-by-step solution

1.Substitute f(x)=x2f(x)=x^2: (3+h)232h=9+6h+h29h\frac{(3+h)^2-3^2}{h}=\frac{9+6h+h^2-9}{h}.

2.For h0h\neq0, simplify to 6+h6+h. Therefore, limh0(6+h)=6\lim_{h\to0}(6+h)=6.

Final answer and interpretation

The derivative is f(x)=2xf'(x)=2x, so f(3)=6f'(3)=6. The tangent line at (3,9)(3,9) is y9=6(x3)y-9=6(x-3).

The derivative is the limiting slope of secant lines, not merely a symbolic rule.

Worked Example 2

Product and Chain Rules Together

Problem

Differentiate g(x)=x2e3xg(x)=x^2e^{3x}.

Step-by-step solution

1.Use the product rule with u=x2u=x^2 and v=e3xv=e^{3x}: g=uv+uvg'=u'v+uv'.

2.Because u=2xu'=2x and the chain rule gives v=3e3xv'=3e^{3x}, g(x)=2xe3x+3x2e3xg'(x)=2xe^{3x}+3x^2e^{3x}.

Final answer and interpretation

Factor the common term: g(x)=xe3x(2+3x)g'(x)=xe^{3x}(2+3x).

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 fxi\frac{\partial f}{\partial x_i}, measures the rate of change of a multivariable function f(x1,x2,...,xn)f(x_1, x_2, ..., x_n) with respect to one variable xix_i, while holding all other variables constant.

The Gradient of a multivariable function, denoted by f(x)\nabla f(x) (read as 'nabla f'), is a vector of all its partial derivatives: f(x)=[fx1fx2fxn]\nabla f(x) = \begin{bmatrix} \frac{\partial f}{\partial x_1} \\ \frac{\partial f}{\partial x_2} \\ \vdots \\ \frac{\partial f}{\partial x_n} \end{bmatrix}

Concrete Gradient Example: Let f(x,y)=3x2y+eyf(x, y) = 3x^2 y + e^y. Let's compute its gradient vector f(x,y)\nabla f(x, y): - The partial derivative with respect to xx (treating yy as a constant) is: fx=6xy\frac{\partial f}{\partial x} = 6xy. - The partial derivative with respect to yy (treating xx as a constant) is: fy=3x2+ey\frac{\partial f}{\partial y} = 3x^2 + e^y. - Therefore, the gradient vector is: f(x,y)=[6xy3x2+ey]\nabla f(x, y) = \begin{bmatrix} 6xy \\ 3x^2 + e^y \end{bmatrix} At the specific coordinate point (1,0)(1, 0), the gradient is evaluated as: f(1,0)=[6(1)(0)3(1)2+e0]=[04]\nabla f(1, 0) = \begin{bmatrix} 6(1)(0) \\ 3(1)^2 + e^0 \end{bmatrix} = \begin{bmatrix} 0 \\ 4 \end{bmatrix}.

A unit vector uu defines a direction. The directional derivative is Duf=fTuD_u f=\nabla f^T u; it is largest when uu 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 (f(x)-\nabla f(x)), gradient descent moves down the steepest path to find the local minimum.

Gradient, Descent, and a Constraint

−∇f stepsconstraint g(x,y)=0∇g ∥ ∇f
Read diagram labels
  • −∇f steps
  • constraint g(x,y)=0
  • ∇g ∥ ∇f
Worked Example 1

Gradient and Directional Change

Problem

Let f(x,y)=x2+2y2f(x,y)=x^2+2y^2. At (1,1)(1,-1), find the gradient and the directional derivative toward u=(3/5,4/5)u=(3/5,4/5).

Step-by-step solution

1.f=(2x,4y)T\nabla f=(2x,4y)^T, so f(1,1)=(2,4)T\nabla f(1,-1)=(2,-4)^T.

2.Duf=fTu=2(3/5)4(4/5)=13/5D_u f=\nabla f^Tu=2(3/5)-4(4/5)=-13/5.

Final answer and interpretation

The negative value means the function decreases at an instantaneous rate of 13/513/5 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: f(x)=0\nabla f(x) = 0.

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 f(x,y)f(x, y) subject to an equality constraint g(x,y)=0g(x, y) = 0, we introduce a new variable λ\lambda (the Lagrange Multiplier) and construct the Lagrangian function: L(x,y,λ)=f(x,y)λg(x,y)\mathcal{L}(x, y, \lambda) = f(x, y) - \lambda g(x, y),

We find critical points of the Lagrangian by setting its gradient with respect to all variables (x,y,x, y, and λ\lambda) to zero: xL=0,yL=0,Lλ=0\nabla_x \mathcal{L} = 0, \quad \nabla_y \mathcal{L} = 0, \quad \frac{\partial \mathcal{L}}{\partial \lambda} = 0,

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 f(x,y)=xyf(x, y) = xy subject to a perimeter boundary constraint g(x,y)=2x+2y20=0g(x, y) = 2x + 2y - 20 = 0. We construct the Lagrangian: L(x,y,λ)=xyλ(2x+2y20)\mathcal{L}(x, y, \lambda) = xy - \lambda(2x + 2y - 20) Taking partial derivatives and setting them to zero: Lx=y2λ=0    y=2λ\frac{\partial \mathcal{L}}{\partial x} = y - 2\lambda = 0 \implies y = 2\lambda Ly=x2λ=0    x=2λ\frac{\partial \mathcal{L}}{\partial y} = x - 2\lambda = 0 \implies x = 2\lambda Since x=2λx = 2\lambda and y=2λy = 2\lambda, we find x=yx = y. Substituting into the constraint 2(2λ)+2(2λ)=20    8λ=20    λ=2.52(2\lambda) + 2(2\lambda) = 20 \implies 8\lambda = 20 \implies \lambda = 2.5. This yields the optimal dimensions x=5,y=5x = 5, y = 5.

Constrained Optimum: Tangent Level Set and Feasible Curve

∇f∇g = λ⁻¹∇fg(x,y)=0
Read diagram labels
  • ∇f
  • ∇g = λ⁻¹∇f
  • g(x,y)=0
Worked Example 1

Classifying a Critical Point

Problem

Find and classify the critical point of f(x,y)=x2+4xy+5y2f(x,y)=x^2+4xy+5y^2.

Step-by-step solution

1.f=(2x+4y,4x+10y)T\nabla f=(2x+4y,4x+10y)^T. Solving f=0\nabla f=0 gives (x,y)=(0,0)(x,y)=(0,0).

2.The Hessian is H=[24410]H=\begin{bmatrix}2&4\\4&10\end{bmatrix}. Its leading principal minors are 2>02>0 and det(H)=2016=4>0\det(H)=20-16=4>0.

Final answer and interpretation

Therefore HH is positive definite and (0,0)(0,0) is a strict minimum.

Worked Example 2

Cumulative Constraint Problem

Problem

Minimize x2+y2x^2+y^2 subject to x+y=6x+y=6.

Step-by-step solution

1.Form L=x2+y2λ(x+y6)\mathcal{L}=x^2+y^2-\lambda(x+y-6).

2.2xλ=02x-\lambda=0 and 2yλ=02y-\lambda=0 imply x=yx=y.

Final answer and interpretation

The constraint gives 2x=62x=6, hence x=y=3x=y=3 and the minimum value is 1818.

At the constrained optimum, the objective and constraint gradients are parallel.

Interactive Practice Quiz

Test your understanding with instant feedback

QUESTION 01

Which differentiation rule is used to compute the derivative of a nested composite function like f(g(x))f(g(x))?

QUESTION 02

Which of the following describes the geometric meaning of the Gradient Vector f(x)\nabla f(x)?

QUESTION 03

Compute the partial derivative fy\frac{\partial f}{\partial y} of the function f(x,y)=3x2+5xy+y3f(x, y) = 3x^2 + 5xy + y^3:

QUESTION 04

Why are smooth, differentiable losses convenient for ordinary gradient descent?

QUESTION 05

What is the purpose of constructing the Lagrangian function L(x,λ)=f(x)λg(x)\mathcal{L}(x, \lambda) = f(x) - \lambda g(x)?

QUESTION 06

Evaluate the limit: limx3x29x3\lim_{x \to 3} \frac{x^2 - 9}{x - 3}?

QUESTION 07

Compute the derivative f(x)f'(x) of the function f(x)=4x3+5x2f(x) = 4x^3 + 5x - 2 using the Power Rule:

QUESTION 08

Apply the Product Rule to differentiate the function f(x)=x2ln(x)f(x) = x^2 \ln(x):

QUESTION 09

If the gradient of a loss function is evaluated as f(w)=[2,3]T\nabla f(w) = [2, -3]^T at a specific point, what direction should gradient descent take to decrease the loss?

QUESTION 10

In multivariable optimization, what is a 'Critical Point' of an unconstrained function?