Guide To AI LogoGuide To AI
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

Calculus is the mathematical engine that powers modern machine learning optimization. To understand how complex deep learning networks iteratively minimize error, we must start with the building blocks of limits, continuity, and single-variable derivatives.

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 is equal to its actual value f(c)f(c) at that point. Differentiability is a stronger condition: a function is differentiable if its derivative exists at every point in its domain. Loss functions must be continuous and differentiable so that gradient descent can compute smooth mathematical updates without sudden breaks.

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

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}.

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.

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,

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, proving mathematically that a square maximizes area subject to a perimeter constraint!

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 do we require loss functions in machine learning to be continuous and differentiable?

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?