Hands-On Probabilistic Machine Learning: A Deep Dive into VAE, Self-Supervised Learning, and Reinforcement Learning Core Concepts

A comprehensive walkthrough of probabilistic ML: VAE, self-supervised learning, and reinforcement learning fundamentals.
This article systematically explores core probabilistic machine learning concepts from a coding demo series, covering generalization theory (overfitting, No Free Lunch theorem), unsupervised density estimation and clustering, VAE architecture with reparameterization trick and ELBO loss, self-supervised masked prediction methods (BERT, MAE), and the multi-armed bandit problem's exploration-exploitation trade-off. All topics are unified through a probabilistic lens on modeling uncertainty.
Introduction: A Systematic Practical Guide to Probabilistic Machine Learning
In the journey of learning machine learning, there often exists a gap between theory and code implementation. Recently, a creator published a new coding demo video in their "Probabilistic Machine Learning Series," systematically covering multiple core topics from generalization theory to introductory reinforcement learning. This content not only explains concepts but also provides complete code implementations, making it a valuable reference for learners looking to solidify their machine learning foundations.
This article will provide an in-depth analysis of the major core topics covered in this series, helping readers understand the intrinsic connections between these concepts.

Generalization Theory: The Fundamental Question of Machine Learning
Overfitting and the Generalization Gap
The ultimate goal of machine learning is not to perform perfectly on training data, but to make accurate predictions on data never seen before. This leads to several key concepts: Overfitting, Population Risk, and the Generalization Gap.
Population risk refers to the expected loss of a model on the true data distribution. However, since we cannot access the complete data distribution, in practice we use the test set as a proxy for population risk. When a model has extremely low loss on the training set but significantly higher loss on the test set, the difference between the two is the generalization gap — a direct manifestation of overfitting.
From a deeper theoretical perspective, the distinction between Population Risk and Empirical Risk is a cornerstone of statistical learning theory. Population risk is defined as the expected loss of a model over the entire data-generating distribution E[L(f(x), y)], while empirical risk is the average loss computed over finite training samples. Classical Vapnik-Chervonenkis (VC) theory proves that the generalization gap can be bounded by model complexity (measured by VC dimension) and training sample size — for a fixed amount of data, the more complex the model, the larger the upper bound on the generalization gap. This theory provides solid theoretical justification for techniques that prevent overfitting, such as regularization and early stopping. Notably, the over-parameterized networks of the deep learning era seem to violate the predictions of classical VC theory yet still generalize well. This "double descent" phenomenon has reinvigorated academic interest in generalization theory.
The No Free Lunch Theorem and Inductive Biases
The series also emphasizes the importance of the No Free Lunch Theorem and Inductive Biases. The core message of this theorem is: there is no universal learning algorithm that outperforms all other algorithms on every possible problem. The effectiveness of any algorithm depends on assumptions about the problem's structure.
The No Free Lunch Theorem was formally introduced by David Wolpert and William Macready in 1997. Its mathematical formulation shows that, averaged over all possible target functions, any two optimization algorithms perform identically. In other words, if algorithm A outperforms algorithm B on one class of problems, then B necessarily outperforms A on another class. The profound implication of this theorem is that the core of algorithm design is not pursuing universality, but understanding the structure of the target problem and selecting inductive biases that match it.
This is precisely why inductive biases exist — by introducing reasonable prior assumptions, we enable models to achieve better generalization on specific tasks. For example, Convolutional Neural Networks (CNNs) have built-in inductive biases of local connectivity and weight sharing, assuming that spatial features exhibit locality and translational invariance, which makes them far superior to fully connected networks on image tasks. Recurrent Neural Networks (RNNs) assume temporal dependencies in data. The Transformer architecture introduces a bias toward global dependency modeling through self-attention mechanisms, making it better suited for long-sequence modeling tasks. Understanding this helps us make more informed model choices when facing specific problems, rather than blindly pursuing a "one-size-fits-all model."
Unsupervised Learning: From Density Estimation to Clustering
Foundational Methods for Density Estimation and Clustering
Unsupervised learning is a methodology for discovering the intrinsic structure of data without labels. The series focuses on two classic tasks: Density Estimation and Clustering.
Density estimation aims to model the probability distribution $p(x)$ of data, forming the theoretical foundation of many generative models. Clustering attempts to group similar data points together. While these two tasks seem different, they both serve the same goal — understanding how data is organized.
From a technical taxonomy perspective, density estimation methods can be divided into parametric and non-parametric categories. Non-parametric methods like Kernel Density Estimation (KDE) make no assumptions about the distribution form, estimating density by placing kernel functions (such as Gaussian kernels) at each data point. They are flexible but suffer from the curse of dimensionality in high-dimensional spaces. Parametric methods assume data follows a certain parametric distribution (such as Gaussian Mixture Models, GMM) and optimize parameters through Maximum Likelihood Estimation or the Expectation-Maximization (EM) algorithm. In recent years, deep generative models have pushed density estimation to new heights: Normalizing Flows achieve exact density computation through invertible transformations, autoregressive models decompose joint distributions into products of conditional distributions via the chain rule, and Energy-Based Models implicitly define distributions through unnormalized energy functions. These methods each involve different trade-offs between density evaluation accuracy, sampling efficiency, and scalability.
In practical applications, the quality of density estimation also leads to discussions about Density Evaluation and Sample Efficiency — how much data a model needs to accurately characterize a distribution.
VAE: The Bridge Between Deep Learning and Probabilistic Modeling
Latent Factors and the Variational Autoencoder Architecture
Variational Autoencoder (VAE) is one of the key topics in the series, with particularly detailed code implementation and architecture explanations. The core idea of VAE introduces the concept of Latent Factors — assuming that observed data is generated from a set of low-dimensional latent variables through some generative process.
The VAE architecture consists of an Encoder and a Decoder: the encoder maps inputs to a probability distribution in latent space, while the decoder reconstructs the original data from latent variables. Unlike traditional autoencoders, VAE imposes probabilistic constraints on the latent space (typically a standard normal distribution), giving the latent space continuity and the ability to be sampled. This property allows VAE not only to compress data but also to generate new samples, making it an important cornerstone in the field of deep generative models.
Key Implementation Details of VAE
From an engineering perspective, implementing VAE involves two core technical points:
-
Reparameterization Trick: Proposed by Kingma and Welling in 2013, this trick converts the random sampling operation into a deterministic computation plus noise, allowing gradients to backpropagate through the sampling step. Specifically, the transformation z = μ + σ·ε (where ε ~ N(0,1) is external noise) cleverly separates the randomness from the computational graph. Since ε is a fixed random input that doesn't require gradients, while μ and σ are network outputs that can participate normally in backpropagation. This trick not only solved the training challenge of VAE but also inspired numerous subsequent works using stochastic computational graphs, including the Gumbel-Softmax trick (for discrete latent variables) and stochastic depth networks.
-
ELBO Loss Function: Composed of two parts — reconstruction loss and KL divergence — where the former ensures generation quality and the latter constrains the regularity of the latent distribution. ELBO (Evidence Lower BOund) originates from the mathematical framework of variational inference. The objective of VAE is to maximize the marginal log-likelihood of observed data log p(x), but due to the existence of latent variables, direct computation requires integrating over all possible latent variable values, which is typically intractable in high dimensions. Variational inference introduces an approximate posterior distribution q(z|x) (i.e., the encoder) and uses Jensen's inequality to derive log p(x) ≥ E_q[log p(x|z)] - KL(q(z|x) || p(z)), where the right side is the ELBO. The gap between ELBO and the true marginal likelihood equals exactly KL(q(z|x) || p(z|x)) — the distance between the approximate posterior and the true posterior — which is also the starting point for subsequent improvements (such as Importance Weighted VAE and Hierarchical VAE).
These concepts are essential prerequisites for understanding modern generative models (such as diffusion models). In fact, diffusion models can be viewed as a special type of hierarchical VAE where the latent variable dimensions match the input, and the forward process is defined by a fixed noise scheduler.
Self-Supervised Learning: Representation Learning in the Unlabeled Era
Masked Prediction Methods and Applications
Self-Supervised Learning has become one of the mainstream paradigms in deep learning in recent years. Its core idea is to construct supervisory signals from the data itself. The series focuses on the method of Masked Predictions.
The idea behind masked prediction is elegantly simple yet powerful: mask a portion of the input data and have the model predict the masked content from the remaining parts. This idea can be traced back to the Cloze task in natural language processing, but it was BERT (Bidirectional Encoder Representations from Transformers), proposed by Google in 2018, that systematically turned it into a pre-training paradigm. BERT randomly masks 15% of input text tokens and trains the model to predict the masked words, thereby learning deep bidirectional semantic representations.
After this paradigm gave rise to milestone models like BERT in the NLP domain, it was quickly transferred to computer vision. MAE (Masked Autoencoder), proposed by Kaiming He et al. in 2021, splits images into patches, randomly masks up to 75% of them, encodes the visible patches with a Vision Transformer, and reconstructs the masked portions, demonstrating excellent pre-training results on ImageNet. Its advantage lies in learning high-quality universal representations from massive unlabeled data, dramatically reducing the dependency on manual annotation.
The success of masked prediction reveals a profound insight: rich redundant structure exists within data, and by learning to exploit these structural relationships, models naturally acquire powerful representational capabilities. The latest multimodal models like Meta's data2vec have unified this paradigm across speech, text, and image modalities, demonstrating the enormous potential of masked prediction as a universal pre-training framework.
Introduction to Reinforcement Learning: The Multi-Armed Bandit Problem
The Classic Exploration-Exploitation Trade-off
As introductory reinforcement learning content, the series presents the Multi-Armed Bandit problem. This is an excellent starting point for understanding the core tension in reinforcement learning.
The multi-armed bandit problem describes the following scenario: given multiple "bandit arms" with unknown rewards, how do you maximize cumulative returns within a limited number of attempts? This directly introduces the most fundamental Exploration vs. Exploitation trade-off in reinforcement learning — should you keep exploiting the currently known best option, or explore unknown options that might yield higher rewards?
This seemingly simple problem has spawned a series of elegant algorithmic designs. The most basic strategy is ε-greedy: with probability 1-ε, select the arm with the highest estimated reward, and with probability ε, explore randomly. More sophisticated methods include UCB (Upper Confidence Bound), which balances exploration and exploitation by adding a confidence upper bound proportional to uncertainty to each arm's estimated reward — arms selected fewer times receive a higher "exploration bonus" due to greater uncertainty. The Bayesian method Thompson Sampling maintains a posterior distribution of rewards for each arm, samples from each arm's posterior in every round, and selects the arm with the highest sampled value. It has been theoretically proven to achieve near-optimal regret bounds in many scenarios.
Among these, Regret is the core performance metric in the multi-armed bandit problem, defined as the difference between the cumulative reward of the optimal policy and the actual policy's cumulative reward over T decision rounds. Theory has proven that the regret lower bound for any policy is O(√T), and both UCB and Thompson Sampling achieve this order.
While multi-armed bandits are structurally simple, their ideas permeate the entire field of reinforcement learning — from A/B testing in recommendation systems to ad placement strategy optimization and adaptive drug allocation in clinical trials. More importantly, they lay the conceptual foundation for understanding more complex Markov Decision Processes (MDP) and deep reinforcement learning.
Summary: The Probabilistic Perspective Unifying the Machine Learning Knowledge System
The value of this coding demo lies in connecting generalization theory, unsupervised learning, generative models, self-supervised learning, and reinforcement learning — seemingly disparate topics — into a coherent knowledge system. The common underlying logic of these topics is machine learning from a probabilistic perspective — whether it's density estimation, VAE's latent distributions, or bandit expected rewards, they all fundamentally involve modeling uncertainty.
The power of this unified probabilistic perspective is that it provides a cross-domain language of thought: population risk in generalization theory measures uncertainty about the data distribution, VAE handles inference uncertainty of latent variables through variational inference, self-supervised learning implicitly models conditional distributions of data, and multi-armed bandits require decision-making under epistemic uncertainty about reward distributions. By mastering this unified perspective, learners can develop a deeper understanding of the essential connections between various methods, rather than viewing them as isolated techniques.
For learners, this "code + concepts" dual-track teaching approach is particularly effective: theory provides the "why," and code answers the "how." Interested readers can access the complete code implementation and explanations through the original video link.
Key Takeaways
Related articles

Math PhD Transitioning to AI/ML: A Complete Guide to Layered Project Roadmaps and Role Strategies
How can an applied math PhD transition to MLE, AI engineer, or applied scientist? A layered project roadmap covering diffusion models, Neural ODEs, RAG systems, and more.

Glasp Firefox Extension: A Detailed Guide to Free AI Highlighting & Smart Summarization
Glasp launches on Firefox with multi-color highlighting for web pages, PDFs, and YouTube videos, AI summaries via ChatGPT, Claude & Gemini, plus free export to Notion and Obsidian.

Wealthfolio: A Local-First Open-Source Personal Finance Tool
Wealthfolio is an open-source, local-first personal finance app for investment tracking, net worth, and expense management — with no accounts, no subscriptions, and full data privacy.