Foundations to Frontier
Tests
E. Deep Learning › E6 Early and Energy-Based Networks

Restricted Boltzmann Machines and Contrastive Divergence

A restricted Boltzmann machine (RBM) is a Boltzmann machine with one visible and one hidden layer and no connections within a layer. Given one layer, the other's units are independent, so sampling alternates whole layers, and the hidden units can be summed out in closed form (the free energy). Contrastive divergence (CD-k) trains it by replacing the intractable model statistics with those after k Gibbs steps started at the data. This page covers the architecture, the free energy, block Gibbs sampling, CD-k and persistent CD. In the exercises you verify the free energy and the factorised conditionals by brute force, then compare training rules by exact likelihood on 3 × 3 bars and stripes: CD-10 came within 0.28 nats of the best possible model, CD-1 was clearly worse, and persistent CD fell apart at too high a learning rate.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Two layers, and nothing in between

A restricted Boltzmann machine keeps only the connections between a visible layer (the data) and a hidden layer (features); units within a layer do not talk to each other. That restriction makes everything easier: given the data, all hidden units can be sampled at once, and given the hidden units, all visible units can. Training then compares the data with the network’s own reconstruction of it a few steps later, and adjusts the weights to make reconstructions look more like data.

Gibbs sampling a 4-unit Boltzmann machine
0000000100100011010001010110011110001001101010111100110111101111
KL(sample ‖ exact) = 0.015 · switches between all-down (0000) and all-up (1111): 54
Outlines: exact probabilities of the 16 states; filled bars: fraction of sweeps the chain spent in each. With weak coupling the chain mixes quickly and the histogram matches. With strong coupling at low temperature the two aligned states dominate and are separated by improbable states, so the chain rarely switches: after a few hundred sweeps it may never have visited one of them. That slow mixing is why exact Boltzmann-machine learning is impractical and why RBMs use contrastive divergence.

The demo shows why sampling general Boltzmann machines is slow; the RBM’s layered structure and contrastive divergence were the practical answer.

Why engineers care

RBMs were the building blocks of the deep belief networks that restarted deep learning in 2006, an early workhorse for collaborative filtering (the Netflix prize) and are still used for representing quantum many-body states. Their training trick, contrastive divergence, is the template for many energy-based learning methods.

📝 Quick check (Level 0)
0/3 answered
  1. 1. An RBM has connections…
  2. 2. Given the visible units, the hidden units are…
  3. 3. Contrastive divergence compares data with…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Energy: E(v, h) = −bᵀv − cᵀh − vᵀWh.
  • Conditionals: p(hⱼ = 1 | v) = σ(cⱼ + (vW)ⱼ) and p(vᵢ = 1 | h) = σ(bᵢ + (Wh)ᵢ). Example: v = (1, 0), W = [[2, 0], [0, 0]], c = 0: p(h) = (σ(2), σ(0)) ≈ (0.881, 0.5).
  • Free energy: F(v) = −bᵀv − Σⱼ log(1 + exp(cⱼ + (vW)ⱼ)), and p(v) = exp(−F(v))/Z. With all parameters zero and one hidden unit, F = −log 2.
  • CD-k update: ΔW = η(v₀ᵀp(h | v₀) − v_kᵀp(h | v_k))/batch, where v_k is reached by k steps of block Gibbs sampling from the data v₀. Example: v₀ = (1, 0), v₁ = (0, 1), both hidden probabilities 1, η = 0.5: ΔW = (0.5, −0.5)ᵀ.
  • Persistent CD (PCD): keep the chains running across updates instead of restarting them at the data.
📝 Quick check (Level 1)
0/3 answered
  1. 1. p(h₁ = 1 | v) with input 2:
  2. 2. F(v) with all parameters zero and one hidden unit:
  3. 3. PCD differs from CD by…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

RBMs in code

Exercise 1. For a 4-visible, 3-hidden RBM, the free-energy formula reproduced the brute-force marginal p(v) for all 16 visible states, and p(h | v) computed by brute force matched the factorised logistic formula.

Practice.

  • Initialise weights small (0.01 scale); use mini-batches, momentum and a small weight decay.
  • CD-1 is fast and fine for learning features; for density estimation use CD-k with larger k or PCD with a small learning rate.
  • Monitor reconstruction error only as a rough guide: it can fall while the likelihood gets worse. For small models, compute the likelihood exactly; for large ones, estimate log Z with annealed importance sampling.
ph0 = sigmoid(v0 @ W + c); v = v0
for _ in range(k): h = rng.uniform(size=ph0.shape) < sigmoid(v @ W + c); v = rng.uniform(size=v.shape) < sigmoid(h @ W.T + b)
W += lr * (v0.T @ ph0 - v.T @ sigmoid(v @ W + c)) / len(v0)
🐍 Exercise 1: free energy and factorised conditionals
Check by brute force that the RBM free energy gives the visible marginal and that p(h | v) factorises.
📝 Quick check (Level 2)
0/3 answered
  1. 1. For density estimation prefer…
  2. 2. Reconstruction error is…
  3. 3. log Z of large RBMs is estimated by…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why CD works (approximately). The exact gradient needs ⟨vhᵀ⟩ under the model, which requires a chain run to equilibrium. CD-k approximates it after k steps starting at the data. Hinton (2002) showed CD approximately follows the gradient of KL(p₀‖p_∞) − KL(p_k‖p_∞); it is biased, but the bias shrinks as k grows (Carreira-Perpiñán and Hinton, 2005).

PCD. Tieleman (2008) kept chains alive across updates, so with a small learning rate the chains stay close to the slowly changing model distribution and the gradient estimate is nearly unbiased. With a large learning rate the model changes faster than the chains mix, and the estimate degrades.

Representational power. Adding hidden units can always improve the likelihood, and an RBM with enough hidden units can represent any distribution over binary vectors (Le Roux and Bengio, 2008).

Beyond binary units. Gaussian–Bernoulli RBMs model real-valued data; conditional RBMs model time series; RBM-like networks represent quantum wavefunctions (Carleo and Troyer, 2017).

📝 Quick check (Level 3)
0/3 answered
  1. 1. CD's bias shrinks as…
  2. 2. PCD needs a learning rate that is…
  3. 3. RBMs have been used to represent…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Comparing training rules exactly

History.

  • P. Smolensky (1986) introduced the harmonium.
  • G. Hinton (2002) introduced contrastive divergence; T. Tieleman (2008) persistent CD.
  • R. Salakhutdinov, A. Mnih and G. Hinton (2007) used RBMs for collaborative filtering.
  • G. Carleo and M. Troyer (2017) used them as neural quantum states.

Exercise 2. The 14 distinct 3 × 3 bars-and-stripes images, an RBM with 8 hidden units, 3000 full-batch epochs. With 9 visible units the exact log-likelihood can be computed by enumerating all 512 images. The best possible average log-likelihood is −log 14 = −2.639; a uniform model gives −6.238. Means of three seeds:

training rule average log-likelihood
CD-1, η = 0.2 −3.302
CD-10, η = 0.2 −2.919
PCD-1, η = 0.1 −3.718
PCD-1, η = 0.2 −6.662
  • CD-10 came within 0.28 nats of the best possible model; CD-1 was 0.38 nats worse than CD-10.
  • PCD was sensitive to the learning rate: at 0.2 it ended worse than a uniform model; at 0.1 it was stable but behind CD.
  • Lesson: exact likelihoods on toy problems reveal biases and instabilities that reconstruction error hides; longer chains give better density models.
🐍 Exercise 2 (research): comparing training rules exactly
Train RBMs on 3 × 3 bars and stripes with CD-1, CD-10 and PCD at two learning rates, and compare their exact log-likelihoods.

RBMs in AI, robotics and aerospace

  • Anomaly detection: the free energy of a trained RBM scores how typical an input is; unusual sensor patterns get high free energy.
  • Feature learning for small datasets of binary or discretised sensor data.
  • Quantum simulation for materials and chemistry uses RBM-style wavefunctions.

References

  1. G. E. Hinton (2002). “Training products of experts by minimizing contrastive divergence.” Neural Computation 14(8).
  2. T. Tieleman (2008). “Training restricted Boltzmann machines using approximations to the likelihood gradient.” Proc. ICML.
  3. M. Á. Carreira-Perpiñán, G. E. Hinton (2005). “On contrastive divergence learning.” Proc. AISTATS.
  4. G. Carleo, M. Troyer (2017). “Solving the quantum many-body problem with artificial neural networks.” Science 355.