Machine Learning as Function Approximation: A Mathematical Foundation
One of the things that frustrates me about most introductions to machine learning is that they either go too far in one direction — drowning the reader in notation before they have any intuition — or too far in the other, offering analogies and metaphors that feel satisfying but leave you no better equipped to reason about what is actually happening. This primer tries to find the middle ground.
The central claim I want to establish is this: machine learning, at its mathematical core, is the problem of function approximation. We have data. We believe there is some underlying function that generated that data. We want to find a good approximation to that function. Everything else — neural networks, gradient descent, regularisation, the bias-variance tradeoff — is machinery in service of that goal.
The Core Problem
Suppose we have a dataset 𝒟 = {(x₁, y₁), (x₂, y₂), ..., (xₙ, yₙ)} where each xᵢ is an input (a vector of features) and each yᵢ is the corresponding output we are trying to predict. We want to find a function f such that f(x) ≈ y for inputs we have not seen before.
The key word is "approximate." We are not looking for a function that perfectly fits our training data — that is almost always the wrong goal, for reasons I will explain when we get to the bias-variance tradeoff. We are looking for a function that generalises well to new data drawn from the same underlying distribution.
In the machine learning framework, we parameterise our function family. We choose a class of functions f_θ indexed by a parameter vector θ ∈ ℝᵈ, and we search for the value of θ that makes f_θ a good approximation to the true underlying function. The formal statement of this optimisation problem is:
The first term is the empirical risk — the average loss over our training data. The second term, λΩ(θ), is a regularisation penalty that controls the complexity of the function we are fitting. I will explain both in detail below.
Loss Functions: Measuring How Wrong We Are
The loss function ℒ(ŷ, y) measures the discrepancy between our prediction ŷ = f_θ(x) and the true output y. The choice of loss function is not arbitrary — it encodes assumptions about the nature of the errors we expect and the costs we assign to different kinds of mistakes.
The standard loss for regression. Penalises large errors quadratically, which means outliers have a disproportionate influence. Corresponds to a maximum likelihood estimate under the assumption of Gaussian noise.
The standard loss for classification. Measures the divergence between the predicted probability distribution and the true label distribution. Derived from the principle of maximum likelihood under a Bernoulli or categorical distribution.
Used in Support Vector Machines. Enforces a margin — points correctly classified with sufficient confidence incur zero loss. This geometric property is what gives SVMs their theoretical guarantees.
Used in generative models and variational inference. Measures the information lost when distribution Q is used to approximate P. Asymmetric — D_KL(P‖Q) ≠ D_KL(Q‖P) — which has important practical implications.
Gradient Descent: How We Find the Minimum
Having defined our loss function, we need to find the parameter vector θ that minimises it. For most interesting function families — and certainly for neural networks — there is no closed-form solution. We have to search for the minimum iteratively.
Gradient descent is the foundational algorithm for this search. The gradient ∇_θ ℒ(θ) points in the direction of steepest ascent of the loss surface. We move in the opposite direction — downhill — by a step size controlled by the learning rate η:
The learning rate is one of the most important hyperparameters in machine learning, and getting it right is more art than science. Too large, and the updates overshoot the minimum and the optimisation diverges. Too small, and convergence is prohibitively slow. In practice, learning rate schedules — which reduce η over the course of training — and adaptive optimisers like Adam have largely superseded the need to hand-tune a fixed learning rate.
Computes the gradient over the entire training set before each update. Stable convergence but computationally expensive for large datasets. Rarely used in practice for large-scale problems.
Computes the gradient from a single training example per update. Noisy, but the noise can actually help escape local minima. The theoretical foundation for online learning.
The industry standard. Computes gradients over small batches (typically 32–512 examples). Balances the stability of batch GD with the computational efficiency of SGD.
Maintains adaptive learning rates for each parameter, based on first and second moment estimates of the gradient. Robust to hyperparameter choices and widely used in deep learning.
The Bias-Variance Tradeoff
This is, in my view, the most important concept in statistical learning theory — and the one that is most frequently misunderstood in practice. The expected generalisation error of a model can be decomposed as follows:
The three terms represent three distinct sources of error. The bias term captures how far the expected prediction of our model is from the true value — it reflects the systematic errors introduced by our modelling assumptions. The variance term captures how much our model's predictions vary across different training sets drawn from the same distribution — it reflects sensitivity to the particular data we happened to train on. The final term, σ², is irreducible noise — the inherent randomness in the data that no model can eliminate.
The tradeoff arises because increasing model complexity generally reduces bias (the model can fit more complex patterns) but increases variance (the model becomes more sensitive to the specific training data). A linear model applied to a non-linear problem will have high bias. A deep neural network with millions of parameters trained on a small dataset will have high variance. The art of machine learning is finding the right point on this tradeoff for your particular problem and dataset.
Regularisation is the primary tool for managing this tradeoff. By adding a penalty term to the loss function, we discourage the model from fitting the training data too closely:
Penalises large weights, shrinking them towards zero but rarely to exactly zero. Equivalent to placing a Gaussian prior on the parameters in a Bayesian framework.
Induces sparsity — drives irrelevant weights to exactly zero, performing implicit feature selection. Equivalent to a Laplace prior. Particularly useful when you believe many features are irrelevant.
Neural Networks and Universal Approximation
A natural question arises: what function families should we use? For many problems, the answer over the past decade has been neural networks. The theoretical justification comes from the Universal Approximation Theorem, first proved by Cybenko in 1989 and extended by Hornik in 1991: a feedforward neural network with a single hidden layer of sufficient width can approximate any continuous function on a compact subset of ℝⁿ to arbitrary precision.
The basic architecture of a two-layer network is:
Where W₁, W₂ are weight matrices, b₁, b₂ are bias vectors, and σ is a non-linear activation function applied element-wise. The non-linearity is essential — without it, the composition of linear transformations is itself linear, and the network collapses to a linear model regardless of depth.
The default activation for hidden layers in modern deep networks. Computationally efficient and avoids the vanishing gradient problem that plagued earlier sigmoid-based networks.
Squashes output to (0,1). Still used in binary classification output layers, but largely superseded by ReLU in hidden layers due to vanishing gradient issues in deep networks.
Normalises a vector of logits to a probability distribution summing to one. The standard output activation for multi-class classification.
Gaussian Error Linear Unit. Used in Transformer architectures (BERT, GPT). A smooth approximation to ReLU that has empirically outperformed it on many language modelling tasks.
Security Implications: Why This Framework Matters
I want to close with something that is directly relevant to my work as a security researcher. Understanding machine learning as function approximation is not just mathematically satisfying — it reveals the attack surface of ML systems in a way that more intuitive descriptions do not.
Consider what we have established: a machine learning model is a parameterised function f_θ whose parameters were chosen to minimise a loss function over a training dataset. The parameters encode information about the training data. The function maps inputs to outputs according to patterns learned from that data. Each of these facts has a direct security implication:
Small perturbations δ to input x such that f(x+δ) ≠ f(x). These exploit the fact that the learned decision boundaries of neural networks are not robust to small, carefully crafted perturbations — a consequence of the high dimensionality of the input space.
Injecting malicious samples into the training set to corrupt θ*. Because the parameters are determined by the training data, an attacker who can influence the training data can influence the learned function — shifting it towards attacker-desired behaviour.
Recovering training data from model parameters θ. Because the parameters encode information about the training distribution, it is sometimes possible to reconstruct individual training examples — a significant privacy concern for models trained on sensitive data.
Determining whether a specific sample was in the training set. Models typically exhibit lower loss on training samples than on held-out data — a detectable signal that can be exploited to infer membership with statistical confidence.
These are not exotic attacks. They are direct consequences of the mathematical structure of machine learning systems. Understanding that structure is a prerequisite for reasoning clearly about how to defend against them.
"You cannot secure a system you do not understand. For AI systems, that understanding begins with the mathematics."