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-12The 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.
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
- MIT OpenCourseWare: 18.01SC Single Variable Calculus
- MIT OpenCourseWare: 18.02SC Multivariable Calculus
Nav