Calculus

Calculus turns functions into local rates of change and accumulated quantities. In machine learning, the most common use is differential: approximate how a loss changes when inputs, weights, or logits move a small amount.

Defining math

For a scalar function , the derivative at is

The first-order Taylor approximation is

Multivariable calculus replaces with gradients, Jacobians and Hessians, and directional derivatives. The same chain rule is what lets backpropagation compose local derivatives through a computational graph.

Executed demo

This snippet compares a central-difference derivative of with the analytic derivative at , then reports the absolute numerical error.

import numpy as np
 
h = 1e-5
f = lambda x: np.sin(x) * np.exp(-0.2*x)
x = 0.7
fd = (f(x+h) - f(x-h)) / (2*h)
true = np.exp(-0.2*x) * (np.cos(x) - 0.2*np.sin(x))
print("central_diff", round(fd, 8))
print("analytic", round(true, 8))
print("abs_error", f"{abs(fd-true):.2e}")

Observed output:

central_diff 0.55291066
analytic 0.55291066
abs_error 7.01e-12

The code estimates the derivative of at using the symmetric slope between and . That finite-difference estimate, 0.55291066, matches the analytic derivative to an absolute error of about , so the local tangent slope is being measured accurately.

Plot of f(x)=sin(x)e^{-0.2x} with a tangent line at x=0.7 whose slope is 0.5529.

The dashed tangent in the plot is the geometric version of the number printed by the code. That local slope is exactly the kind of signal gradient descent uses when minimizing a loss.

Caveats

Numerical derivatives depend on step size: too large gives truncation error, too small magnifies floating-point cancellation. Non-smooth points can have subgradients or one-sided derivatives rather than a single ordinary derivative.

References