AI: Machine Learning Foundations
AI & Machine Learning › Machine Learning Foundations
Before you stack a hundred layers and call it deep learning, it pays to understand why learning from finite data is possible at all, how we actually find good parameters, and what classical tools (kernels, Gaussian processes, variational inference) reveal about modern models. This page builds those foundations gradually—from intuitive concepts to the formal results—then hands off to Neural Network Architectures, which assumes all of it.
Building the Foundation: How Machines Learn
Let’s explore the mathematical principles that make these systems work. Don’t worry—we’ll build up gradually from intuitive concepts to more advanced ideas.
Statistical Learning Theory
At its heart, machine learning is about finding patterns in data. Statistical learning theory gives us the mathematical tools to understand when and why our learning algorithms will work. Think of it as the “physics” of machine learning—fundamental laws that govern what’s possible.
Core Concepts:
- Generalization: How well a model performs on new, unseen data
- Overfitting vs Underfitting: Balancing model complexity with performance
- Bias-Variance Tradeoff: The fundamental tension in model selection
- Cross-Validation: Techniques to evaluate model performance reliably
The Learning Setup
We assume training examples $(x_i, y_i)$ are drawn independently from a fixed but unknown distribution $\mathcal{D}$ over $\mathcal{X} \times \mathcal{Y}$. A learning algorithm picks a hypothesis $h$ from a hypothesis class $\mathcal{H}$ to minimize a loss $\ell(h(x), y)$. We care about the true (population) risk
\[R(h) = \mathbb{E}_{(x,y) \sim \mathcal{D}}\big[\ell(h(x), y)\big]\]but we can only measure the empirical risk on our $n$ samples,
\[\hat{R}_n(h) = \frac{1}{n}\sum_{i=1}^{n} \ell(h(x_i), y_i).\]The entire game of generalization is about controlling the gap $R(h) - \hat{R}_n(h)$. Minimizing $\hat{R}_n$ is called empirical risk minimization (ERM); it only works if that gap is provably small uniformly over the class $\mathcal{H}$.
Bias–Variance Decomposition
For squared-error regression with target $y = f(x) + \varepsilon$ where $\mathbb{E}[\varepsilon] = 0$ and $\operatorname{Var}(\varepsilon) = \sigma^2$, the expected error of a learned predictor $\hat{f}$ at a point $x$ decomposes exactly:
\[\mathbb{E}\big[(y - \hat{f}(x))^2\big] = \underbrace{\big(f(x) - \mathbb{E}[\hat{f}(x)]\big)^2}_{\text{bias}^2} + \underbrace{\mathbb{E}\big[(\hat{f}(x) - \mathbb{E}[\hat{f}(x)])^2\big]}_{\text{variance}} + \underbrace{\sigma^2}_{\text{irreducible}}\]The expectations are taken over random draws of the training set. This is the fundamental tension:
- High bias (too simple a model) systematically misses the true function — underfitting.
- High variance (too flexible a model) chases noise in the particular training sample — overfitting.
- The irreducible term $\sigma^2$ is the noise floor; no model can beat it.
Increasing model complexity lowers bias but raises variance. The sweet spot minimizes their sum. (Modern overparameterized networks complicate this classic U-curve with “double descent,” where test error falls again past the interpolation threshold — but the decomposition itself still holds.)
Generalization Bounds: VC Dimension and Rademacher Complexity
Why should a model that fits the training data also work on new data? The answer is uniform convergence: if the hypothesis class is not too rich, then $\hat{R}_n(h)$ is close to $R(h)$ for every $h \in \mathcal{H}$ simultaneously, so minimizing the empirical risk is safe.
VC dimension measures the capacity of a binary hypothesis class. The VC dimension of $\mathcal{H}$ is the size $d_{VC}$ of the largest set of points that $\mathcal{H}$ can shatter — label in all $2^{d_{VC}}$ possible ways. (A linear classifier in $\mathbb{R}^d$ has $d_{VC} = d + 1$.) The classic VC bound says that with probability at least $1 - \delta$, for all $h \in \mathcal{H}$,
\[R(h) \le \hat{R}_n(h) + \sqrt{\frac{d_{VC}\big(\ln(2n/d_{VC}) + 1\big) + \ln(4/\delta)}{n}}.\]The gap shrinks like $\sqrt{d_{VC}/n}$: more capacity needs proportionally more data. This is exactly the vc_dimension_bound used in the worked example below.
Rademacher complexity is a sharper, data-dependent capacity measure. Given samples $x_1, \dots, x_n$ and i.i.d. signs $\sigma_i \in {-1, +1}$ (Rademacher variables), the empirical Rademacher complexity of a real-valued class $\mathcal{F}$ is
\[\hat{\mathfrak{R}}_n(\mathcal{F}) = \mathbb{E}_{\sigma}\left[\sup_{f \in \mathcal{F}} \frac{1}{n}\sum_{i=1}^{n} \sigma_i\, f(x_i)\right].\]It measures how well the class can correlate with pure random noise — a class that can fit any labeling has high complexity. The corresponding bound is, with probability $\ge 1 - \delta$,
\[R(h) \le \hat{R}_n(h) + 2\,\hat{\mathfrak{R}}_n(\mathcal{F}) + 3\sqrt{\frac{\ln(2/\delta)}{2n}}.\]Rademacher complexity is more refined than VC because it depends on the actual data distribution and applies to real-valued classes (e.g., margin-based bounds for SVMs and neural nets).
| PAC learning sketch. The Probably Approximately Correct framework formalizes “learnable.” A class $\mathcal{H}$ is PAC-learnable if there is an algorithm that, for any $\varepsilon, \delta \in (0,1)$, returns with probability $\ge 1 - \delta$ a hypothesis with risk $\le \varepsilon$, using a sample size polynomial in $1/\varepsilon$, $1/\delta$, and the relevant capacity. For a finite hypothesis class the argument is a one-line union bound: a single bad hypothesis with true error $> \varepsilon$ survives $n$ samples with probability $\le (1-\varepsilon)^n \le e^{-\varepsilon n}$, so union-bounding over $ | \mathcal{H} | $ candidates, |
| samples suffice to drive the probability of any consistent-but-bad hypothesis below $\delta$. For infinite classes, $\ln | \mathcal{H} | $ is replaced by the VC dimension (the “fundamental theorem of statistical learning”: finite VC dimension $\iff$ PAC-learnable). |
Cross-Validation
Bounds tell us what is possible; cross-validation tells us what is happening on our data. k-fold cross-validation splits the data into $k$ folds, trains on $k-1$ of them, validates on the held-out fold, and averages over all $k$ choices. It gives a nearly unbiased estimate of out-of-sample risk while using every example for both training and validation. Leave-one-out ($k = n$) is the extreme low-bias, high-variance end; $k = 5$ or $10$ is the usual practical compromise.
Looking for rigorous mathematical proofs? See our Advanced AI Mathematics page for PAC learning, VC dimension theory, and formal generalization bounds.
Optimization: Finding the Best Parameters
Learning theory says a good hypothesis exists in the class; optimization is how we actually find it by minimizing the empirical risk.
Practical Optimization Techniques:
- Gradient Descent: The workhorse of machine learning optimization
- Stochastic Methods: How to learn from large datasets efficiently
- Momentum and Acceleration: Making optimization faster and more stable
At the heart of training is a simple update rule: nudge each parameter $\theta$ a small step in the direction that most reduces the loss $\mathcal{L}$, scaled by the learning rate $\eta$:
\[\theta_{t+1} = \theta_t - \eta\,\nabla_\theta \mathcal{L}(\theta_t)\]Stochastic gradient descent (SGD) estimates $\nabla_\theta \mathcal{L}$ from a small mini-batch rather than the full dataset, trading a noisier gradient for vastly faster iterations. Optimizers like Adam adapt $\eta$ per parameter using running estimates of the gradient’s mean and variance.
Convexity: When Optimization Is Easy
A function $f$ is convex if its graph lies below every chord:
\[f\big(\lambda x + (1-\lambda) y\big) \le \lambda f(x) + (1-\lambda) f(y), \quad \lambda \in [0,1].\]Convexity is the property that makes optimization tractable: for a convex function, every local minimum is a global minimum, and a zero gradient certifies optimality. A twice-differentiable $f$ is convex iff its Hessian is positive semidefinite, $\nabla^2 f \succeq 0$. Linear regression (squared loss), logistic regression, and the SVM hinge loss all give convex objectives — which is why these classical methods are reliable. Deep networks are non-convex, riddled with saddle points; the surprise of deep learning is that gradient methods work well anyway (see the optimization-landscape discussion in Neural Network Architectures).
For a convex $f$ with $L$-Lipschitz gradient (smoothness), gradient descent with step size $\eta = 1/L$ converges at rate $f(\theta_t) - f^\star = O(1/t)$; adding strong convexity (Hessian $\succeq \mu I$) upgrades this to a linear (geometric) rate $O\big((1 - \mu/L)^t\big)$.
Stochastic Gradient Descent
Computing the full-batch gradient over millions of examples each step is wasteful. SGD replaces $\nabla \mathcal{L}$ with an unbiased estimate $\nabla \mathcal{L}_{B}$ from a mini-batch $B$:
\[\theta_{t+1} = \theta_t - \eta_t\, \nabla_\theta \mathcal{L}_{B_t}(\theta_t), \qquad \mathbb{E}[\nabla \mathcal{L}_{B}] = \nabla \mathcal{L}.\]The gradient noise has two faces. It slows asymptotic convergence (in convex problems SGD with a decaying schedule $\sum \eta_t = \infty$, $\sum \eta_t^2 < \infty$ achieves $O(1/\sqrt{t})$ rather than $O(1/t)$), but it also acts as an implicit regularizer that helps escape saddle points and biases the iterate toward flat minima that generalize better.
Momentum accumulates an exponentially decaying average of past gradients to dampen oscillation across steep, narrow valleys and accelerate along consistent directions:
\[v_{t+1} = \beta\, v_t + \nabla_\theta \mathcal{L}(\theta_t), \qquad \theta_{t+1} = \theta_t - \eta\, v_{t+1}.\]Nesterov’s accelerated gradient evaluates the gradient at the look-ahead point $\theta_t - \eta\beta v_t$, achieving the optimal $O(1/t^2)$ rate for smooth convex problems.
Adam and Adaptive Methods
Adam (Adaptive Moment Estimation) maintains running estimates of both the first moment (mean) $m_t$ and second moment (uncentered variance) $v_t$ of the gradient $g_t = \nabla_\theta \mathcal{L}(\theta_t)$:
\[m_t = \beta_1 m_{t-1} + (1 - \beta_1) g_t, \qquad v_t = \beta_2 v_{t-1} + (1 - \beta_2) g_t^2\]Because $m_0 = v_0 = 0$, these estimates are biased toward zero early in training; Adam corrects with $\hat{m}_t = m_t / (1 - \beta_1^t)$ and $\hat{v}_t = v_t / (1 - \beta_2^t)$, then takes a per-parameter step:
\[\theta_{t+1} = \theta_t - \eta\,\frac{\hat{m}_t}{\sqrt{\hat{v}_t} + \epsilon}\]Dividing by $\sqrt{\hat{v}_t}$ gives each parameter its own effective learning rate — large for low-variance directions, small for noisy ones. Typical defaults are $\beta_1 = 0.9$, $\beta_2 = 0.999$, $\epsilon = 10^{-8}$. AdamW decouples weight decay from the adaptive step, restoring proper L2 regularization and improving generalization; it is the de facto optimizer for training transformers.
Regularization
Regularization controls the variance term of the bias–variance tradeoff by penalizing complexity, shrinking the effective hypothesis class so the generalization bounds above tighten.
- L2 (ridge / weight decay) adds $\frac{\lambda}{2}\lVert\theta\rVert_2^2$ to the loss, shrinking weights smoothly toward zero. The penalized least-squares solution is $\hat{\theta} = (X^\top X + \lambda I)^{-1} X^\top y$ — the $\lambda I$ term also fixes ill-conditioning.
- L1 (lasso) adds $\lambda \lVert\theta\rVert_1$, whose non-smooth corners drive many weights exactly to zero, producing sparse, feature-selecting solutions.
- Early stopping halts training before the model overfits — provably equivalent to L2 regularization for linear models.
- Dropout randomly zeroes activations during training, approximating an ensemble over exponentially many sub-networks and discouraging brittle co-adaptation.
From a Bayesian view, L2 is a Gaussian prior on the weights and L1 a Laplace prior; the penalty strength $\lambda$ encodes prior confidence and is itself a hyperparameter chosen by cross-validation.
Want the convergence proofs? See Advanced AI Mathematics for SGD convergence analysis, the implicit-regularization story, and PAC-Bayes flat-minima bounds.
For those ready to experiment with these concepts, here’s how you might use them in practice:
# Example usage:
from machine_learning_foundations import PACLearning, ConvexOptimization
# Compute generalization bound
vc_dim = 10
n_samples = 1000
delta = 0.05
bound = PACLearning.vc_dimension_bound(vc_dim, n_samples, delta)
print(f"Generalization bound: {bound:.4f}")
The Kernel Trick: Making Linear Methods Powerful
Linear methods are powerful but limited—what if your data isn’t linearly separable? Kernel methods offer an elegant solution: instead of making the model more complex, we transform the data into a higher-dimensional space where linear separation becomes possible.
Intuitive Understanding:
Imagine trying to separate two classes of points on a 2D plane that form concentric circles. No straight line can separate them. But if we add a third dimension (say, the distance from the center), suddenly they become separable by a plane. That’s the kernel trick in action!
The Trick Itself
Suppose a learning algorithm only ever touches the data through inner products $\langle x_i, x_j \rangle$. (Many do, including SVMs and ridge regression in dual form.) Then we can map the inputs through a feature map $\phi: \mathcal{X} \to \mathcal{F}$ into a high- (even infinite-) dimensional space and run the same linear algorithm there — but we never compute $\phi(x)$ explicitly. Instead we evaluate a kernel function
\[k(x, x') = \langle \phi(x), \phi(x') \rangle\]directly. This is the kernel trick: linear methods in a vast feature space at the cost of a cheap kernel evaluation in the original space. A function $k$ is a valid kernel iff it is symmetric and positive semidefinite — Mercer’s condition — equivalently, for any finite set of points the Gram matrix $K_{ij} = k(x_i, x_j)$ is PSD. Mercer’s theorem guarantees such a $k$ corresponds to some feature map.
Common Kernels and Their Uses:
- RBF (Radial Basis Function): Good default choice, creates smooth decision boundaries
The RBF (Gaussian) kernel corresponds to an infinite-dimensional feature space; the bandwidth $\sigma$ sets how far a single training point’s influence reaches.
- Polynomial: Useful when interactions between features matter, $k(x, x’) = (\langle x, x’\rangle + c)^d$ — its features are all monomials of degree up to $d$.
- Linear: $k(x, x’) = \langle x, x’\rangle$, recovering the original linear method when data is already separable.
Support Vector Machines
The SVM is the canonical kernel method. For separable data it finds the hyperplane that maximizes the margin — the distance to the nearest points of either class — which is exactly the capacity-controlling, low-variance choice the Rademacher margin bounds reward. The (soft-margin) primal problem is
\[\min_{w, b, \xi}\; \frac{1}{2}\lVert w \rVert^2 + C \sum_{i=1}^{n} \xi_i \quad \text{s.t.}\quad y_i\big(\langle w, \phi(x_i)\rangle + b\big) \ge 1 - \xi_i,\; \xi_i \ge 0,\]where the slack variables $\xi_i$ allow margin violations and $C$ trades margin width against training errors. The Lagrangian dual depends on the data only through inner products — which is where the kernel enters:
\[\max_{\alpha}\; \sum_{i=1}^{n}\alpha_i - \frac{1}{2}\sum_{i,j} \alpha_i \alpha_j\, y_i y_j\, k(x_i, x_j) \quad \text{s.t.}\quad 0 \le \alpha_i \le C,\; \sum_i \alpha_i y_i = 0.\]The solution is sparse: only the support vectors (points on or inside the margin) have $\alpha_i > 0$, and the decision function $f(x) = \sum_i \alpha_i y_i\, k(x_i, x) + b$ sums over just those. The hinge loss $\max(0,\, 1 - y\,f(x))$ makes the whole objective convex, so the global optimum is found reliably by quadratic programming.
Want the mathematical theory? Explore Reproducing Kernel Hilbert Spaces and Mercer's theorem in our advanced mathematics section.
Beyond the Basics: Advanced Machine Learning Algorithms
As we push the boundaries of what machine learning can do, we need more sophisticated tools. These advanced algorithms tackle problems that simpler methods struggle with—uncertainty quantification, complex probability distributions, and learning from limited data.
Gaussian Processes: When You Need to Know Uncertainty
What are Gaussian Processes?
Imagine you’re trying to predict temperature throughout the day, but you only have measurements at a few times. A Gaussian Process not only gives you predictions for the missing times but also tells you how confident it is about each prediction. It’s like having error bars on your predictions automatically.
Why use Gaussian Processes?
- Uncertainty Estimates: Know when your model is guessing vs. confident
- Few Data Points: Works well with limited training data
- Flexible: Can model complex, non-linear relationships
- No Architecture Decisions: Unlike neural networks, no need to choose layer sizes
The Math: A Distribution Over Functions
A Gaussian process is a distribution over functions such that any finite collection of function values is jointly Gaussian. It is fully specified by a mean function $m(x)$ (usually taken as $0$) and a covariance/kernel function $k(x, x’)$ — the same PSD kernels from the SVM section:
\[f(x) \sim \mathcal{GP}\big(m(x),\, k(x, x')\big).\]This is the function-space dual of the kernel trick: the GP prior is a Gaussian over the (possibly infinite-dimensional) feature weights, and $k$ is again the inner product of feature maps. Given noisy observations $y = f(X) + \varepsilon$ with $\varepsilon \sim \mathcal{N}(0, \sigma_n^2 I)$, the posterior at test points $X_\star$ is also Gaussian, with closed-form mean and covariance:
\[\boldsymbol{\mu}_\star = K_{\star X}\big(K_{XX} + \sigma_n^2 I\big)^{-1} y\] \[\boldsymbol{\Sigma}_\star = K_{\star\star} - K_{\star X}\big(K_{XX} + \sigma_n^2 I\big)^{-1} K_{X\star}\]Here $K_{XX}$, $K_{\star X}$, etc. are kernel Gram matrices between training and test inputs. The predictive mean is the model’s best guess; the diagonal of $\boldsymbol{\Sigma}_\star$ gives the per-point variance — automatic error bars that grow far from the training data. Kernel hyperparameters (length scale, signal variance, noise $\sigma_n$) are fit by maximizing the log marginal likelihood
\[\log p(y \mid X) = -\tfrac{1}{2}\, y^\top \big(K_{XX} + \sigma_n^2 I\big)^{-1} y - \tfrac{1}{2}\log\big|K_{XX} + \sigma_n^2 I\big| - \tfrac{n}{2}\log 2\pi,\]whose two data-dependent terms automatically balance fit against model complexity (the Occam’s-razor determinant term). The cost is the $O(n^3)$ matrix inversion, which limits exact GPs to a few thousand points (hence sparse/inducing-point approximations).
Common Applications:
- Hyperparameter tuning (Bayesian optimization)
- Time series with uncertainty
- Spatial data modeling
- Robotics and control
Notably, an infinitely wide neural network at initialization is exactly a Gaussian process — the bridge that the Neural Tangent Kernel makes precise (see Neural Network Architectures).
Ready for the math? Dive into the formal treatment of GPs including prior/posterior distributions and marginal likelihood optimization.
# Example usage:
from advanced_ml_algorithms import GaussianProcess
# Define RBF kernel
kernel = lambda x, y: np.exp(-0.5 * np.linalg.norm(x - y)**2)
# Fit GP
gp = GaussianProcess(kernel)
gp.fit(X_train, y_train)
# Predict with uncertainty
mean, std = gp.predict(X_test)
Variational Inference: Making the Impossible Possible
In the real world, we often face probability distributions too complex to work with directly. Variational inference offers a clever workaround: approximate the complex distribution with a simpler one that we can actually compute.
The Big Idea:
Think of it like trying to describe the shape of a cloud. The exact shape is too complex, so instead we might say “it looks like a rabbit.” We’re approximating something complex with something simpler that captures the essential features.
From Integration to Optimization
In Bayesian inference we want the posterior $p(z \mid x) = p(x, z) / p(x)$ over latent variables $z$, but the evidence $p(x) = \int p(x, z)\, dz$ is usually an intractable integral. Variational inference sidesteps it by positing a tractable family $q_\phi(z)$ (the variational distribution) and finding the member closest to the true posterior in KL divergence:
\[\phi^\star = \arg\min_\phi\; \mathrm{KL}\big(q_\phi(z) \,\|\, p(z \mid x)\big).\]We can’t evaluate that KL directly (it contains the unknown $p(x)$), but a short algebra step rewrites the log-evidence as
\[\log p(x) = \underbrace{\mathbb{E}_{q_\phi}\big[\log p(x, z) - \log q_\phi(z)\big]}_{\text{ELBO}\,(\mathcal{L})} + \mathrm{KL}\big(q_\phi(z)\,\|\,p(z\mid x)\big).\]Since KL $\ge 0$, the first term is a lower bound on the evidence — the Evidence Lower BOund (ELBO). Because $\log p(x)$ is constant in $\phi$, maximizing the ELBO is equivalent to minimizing the KL: we have turned intractable integration into tractable optimization. The ELBO splits into an interpretable reconstruction-versus-regularization form,
\[\mathcal{L}(\phi) = \mathbb{E}_{q_\phi(z)}\big[\log p(x \mid z)\big] - \mathrm{KL}\big(q_\phi(z) \,\|\, p(z)\big),\]the first term rewarding explanations of the data, the second keeping $q$ close to the prior.
Mean-Field and the Reparameterization Trick
The classic mean-field approximation assumes the latent variables are independent, $q_\phi(z) = \prod_j q_j(z_j)$, which yields closed-form coordinate-ascent updates for conjugate models. For deep models we instead make $q_\phi$ a neural network and optimize the ELBO by stochastic gradient ascent. The catch is differentiating through the sampling step; the reparameterization trick expresses a sample as a deterministic function of $\phi$ and external noise — e.g. for a Gaussian $q_\phi = \mathcal{N}(\mu_\phi, \sigma_\phi^2)$,
\[z = \mu_\phi + \sigma_\phi \odot \epsilon, \qquad \epsilon \sim \mathcal{N}(0, I),\]so gradients flow through $\mu_\phi$ and $\sigma_\phi$ with low variance. This is precisely the engine inside a Variational Autoencoder (VAE), where an encoder network outputs $q_\phi(z \mid x)$ and a decoder outputs $p_\theta(x \mid z)$.
Where is it used?
- Variational Autoencoders (VAEs): Generate new images or data
- Bayesian Deep Learning: Neural networks that know what they don’t know
- Topic Modeling: Discover themes in large document collections
- Recommendation Systems: Model user preferences with uncertainty
Key Benefit: Turns intractable probability problems into optimization problems we can solve.
Want the technical details? Learn about ELBO derivation, mean-field approximation, and normalizing flows in our advanced section.
The Building Blocks: Core Machine Learning Algorithms
Now that we understand the foundations, let’s meet the algorithms that do the actual work. Each has its strengths and ideal use cases—choosing the right one is both an art and a science.
| Algorithm | What it does |
|---|---|
| Linear Regression | Predicts a continuous target from one or more input features. |
| Logistic Regression | Regression adapted for binary classification. |
| Decision Trees | Recursively splits data on the most informative feature. |
| Support Vector Machines | Finds the maximum-margin hyperplane separating classes. |
| Random Forests | Ensembles many decision trees to improve accuracy and reduce variance. |
| Neural Networks | Layered models inspired by biological neurons, capable of learning complex patterns. |
These six algorithms are the workhorses of classical machine learning. Linear and logistic regression are the convex, low-variance baselines; decision trees and random forests are the flexible, low-bias nonparametric methods; SVMs apply the kernel trick for maximum-margin classification; and neural networks generalize all of them, scaling into the deep architectures that the rest of this section explores.
See Also
- Neural Network Architectures — CNNs, RNNs, transformers, and multimodal models built on these foundations
- Generative Models — VAEs (variational inference in action), diffusion, and GANs
- Frontier Research & Ethics — scaling laws and interpretability of large models
- AI Deep Dive (Lecture) — transformers and LLM internals in depth
- AI Mathematics — formal proofs for the theory above (PAC, RKHS, ELBO)