Deep Learning

RBM and generative models

This lecture shows how generative models learn the data distribution and create new samples. It then builds the Restricted Boltzmann Machine from the full Boltzmann Machine. After this page you can compute RBM energies, hidden probabilities and one contrastive-divergence update by hand.

Generative modelsSearch on YouTube

A generative model learns the underlying distribution of the data. After training, it creates new samples that resemble the training data. The slides name four examples: GANs, autoencoders, variational encoders and RBMs. Typical uses are realistic images, text generation, music or speech synthesis, data augmentation and image-to-image translation.

A discriminative model learns the boundary between classes. It maps an input to a label. A generative model learns how the data is distributed. An RBM is generative. It learns a probability distribution over the inputs. Then it reconstructs inputs instead of predicting a label.

A judge sorts photos into boxes. That is discriminative. An artist studies photos and draws new ones. That is generative.

TypeLearnsAnswersExample
Generativedistribution of the datawhat a new real sample looks likeRBM, GAN, variational encoder
Discriminativeboundary between classeswhich label fits this inputfeedforward classifier

Boltzmann machinesSearch on YouTube

A Boltzmann Machine (BM) is a stochastic neural network. Stochastic means probabilistic: units fire with a probability. It learns complex probability distributions over its inputs. It has visible units vv and hidden units hh. Visible units hold the data. Hidden units capture latent variables.

Every unit connects to every other unit. There are no self-connections. All connections are symmetric. The network gives low energy to training patterns. Low energy means high probability. Other patterns get high energy and low probability.

Why full Boltzmann machines failSearch on YouTube

Training a full BM is computationally expensive. Every neuron connects to every other neuron. Gradients need sums over all states of visible and hidden units. The state count grows exponentially. With nn binary units there are 2n2^n states. So 10 units need 1024 sums, while 100 units need 21002^{100} sums. Full BMs are impractical for real datasets. This problem led to the Restricted Boltzmann Machine.

Restricted Boltzmann machinesSearch on YouTube

An RBM is a generative stochastic network with two layers. The first layer is the visible (input) layer. The second layer is the hidden layer. RBMs are shallow two-layer nets. They form the building blocks of deep-belief networks.

Each node is a locus of computation. It processes its input and makes a stochastic decision about transmission. The restriction is strict. No two nodes of the same layer share a link. There is no intra-layer communication. Inputs from all visible nodes reach all hidden nodes. So an RBM is a symmetric bipartite graph. Symmetric means each visible node links to each hidden node. Bipartite means the graph has two parts (layers). The same weight wijw_{ij} works in both directions.

RBMs help with dimensionality reduction, classification, regression, collaborative filtering, feature learning and topic modelling.

Images hide information that sits outside the pixels. A blue-heavy photo may come from a clear daytime sky. The sky is hidden, yet it explains the pixels. Hidden units model such latent variables. Visible units hold the observed pixel intensities. Joint modelling finds latent features for reconstruction, generation and feature extraction.

h1 h2 v1 v2 v3 hidden layer visible layer
RBM as a symmetric bipartite graph. Every visible unit links to every hidden unit. No link sits inside one layer.

B-S-G: Bipartite, Symmetric, Generative

Bipartite: two layers with no intra-layer links. Symmetric: one weight wijw_{ij} serves both directions. Generative: the net models the joint distribution and reconstructs inputs.

Do not add links inside one layer. Intra-layer links destroy the conditional independence. Without it, Gibbs sampling turns hard.

Forward and backward passesSearch on YouTube

The forward pass finds the hidden activation pattern. Each hidden node combines several inputs. Each input is multiplied by a separate weight. The products are summed and added to a bias. An activation function turns the sum into the output.

The backward pass reconstructs the input. It starts from a hidden pattern and rebuilds a visible pattern. Symmetric weights carry the signal back.

aj=cj+∑ivi Wij,pj=σ(aj)a_j = c_j + \sum_i v_i\,W_{ij}, \qquad p_j = \sigma(a_j)

aja_j is the total input to hidden unit jj. cjc_j is the hidden bias. viv_i is visible unit ii. WijW_{ij} is the weight between them. σ\sigma is the sigmoid activation function. pjp_j is the activation probability of hidden unit jj.

Random initial weights make early reconstructions poor. The gap between the reconstruction rr and the input vv is the training signal. Each iteration pushes the weights against this gap. The error falls until it reaches a minimum.

error=1n∑i=1n(vi−ri)2\text{error} = \frac{1}{n}\sum_{i=1}^{n}(v_i - r_i)^2

viv_i is input unit ii. rir_i is its reconstruction. nn is the count of visible units. Training repeats until this reconstruction loss reaches a minimum.

Energy and joint probabilitySearch on YouTube

Notation first. Visible units are v=(v1,v2,…,vn)v = (v_1, v_2, \dots, v_n). Hidden units are h=(h1,h2,…,hm)h = (h_1, h_2, \dots, h_m). Weights WW link the layers, and WijW_{ij} links viv_i with hjh_j. Visible bias bib_i sits on viv_i. Hidden bias cjc_j sits on hjh_j.

Picture a ball on a hilly surface. Valleys trap the ball, peaks repel it. Training digs valleys (low energy) where real data sits.

E(v,h)=−∑ibivi−∑jcjhj−∑i∑jvi Wij hjE(v,h) = -\sum_i b_i v_i - \sum_j c_j h_j - \sum_i\sum_j v_i\,W_{ij}\,h_j

E(v,h)E(v,h) is the energy of the joint state. Aligned visible and hidden units lower it through −viWijhj-v_i W_{ij} h_j. Matching biases lower it further. Low energy means high probability.

P(v,h)=exp⁡(−E(v,h))Z,Z=∑v∑hexp⁡(−E(v,h))P(v,h) = \frac{\exp(-E(v,h))}{Z}, \qquad Z = \sum_v\sum_h \exp(-E(v,h))

P(v,h)P(v,h) is the joint probability of the state. ZZ is the partition function. It sums over all visible and hidden states. It normalises the distribution. Its size is why exact training is expensive.

Worked example. One tiny RBM: two visible units, one hidden unit. Weights W11=0.6W_{11} = 0.6 and W21=−0.4W_{21} = -0.4. Biases b=(0.2,−0.2)b = (0.2, -0.2) and c1=−0.1c_1 = -0.1. Compare a data-like state AA with v=(1,0)v = (1,0), h=(1)h = (1) against an unlikely state BB with v=(0,1)v = (0,1), h=(1)h = (1).

Energy of state A.

EA=−(0.2⋅1+(−0.2)⋅0)−(−0.1⋅1)−(1⋅0.6⋅1+0⋅(−0.4)⋅1)=−0.2+0.1−0.6=−0.7\begin{aligned} E_A &= -(0.2\cdot1 + (-0.2)\cdot0) - (-0.1\cdot1) - (1\cdot0.6\cdot1 + 0\cdot(-0.4)\cdot1)\\ &= -0.2 + 0.1 - 0.6\\ &= \mathbf{-0.7} \end{aligned}

Energy of state B.

EB=−(0.2⋅0+(−0.2)⋅1)−(−0.1⋅1)−(0⋅0.6⋅1+1⋅(−0.4)⋅1)=0.2+0.1+0.4=0.7\begin{aligned} E_B &= -(0.2\cdot0 + (-0.2)\cdot1) - (-0.1\cdot1) - (0\cdot0.6\cdot1 + 1\cdot(-0.4)\cdot1)\\ &= 0.2 + 0.1 + 0.4\\ &= \mathbf{0.7} \end{aligned}

Probability ratio. ZZ cancels in the ratio.

PAPB=exp⁡(0.7)exp⁡(−0.7)=exp⁡(1.4)≈4.06\begin{aligned} \frac{P_A}{P_B} &= \frac{\exp(0.7)}{\exp(-0.7)} = \exp(1.4)\\ &\approx \mathbf{4.06} \end{aligned}

State AA carries low energy and state BB carries high energy. So the data-like state is about four times more likely.

Conditional probabilitiesSearch on YouTube

With no intra-layer links, hidden units turn independent given the visible layer. The reverse holds too. Each conditional is a sigmoid of the weighted sum. These two formulas drive both passes and all sampling.

P(hj=1∣v)=σ(cj+∑iWij vi),P(vi=1∣h)=σ(bi+∑jWij hj)P(h_j = 1 \vert v) = \sigma\left(c_j + \sum_i W_{ij}\,v_i\right), \qquad P(v_i = 1 \vert h) = \sigma\left(b_i + \sum_j W_{ij}\,h_j\right)

The hidden conditional uses the visible state vv. The visible conditional uses the hidden state hh. Both share the same weights WijW_{ij}.

σ(x)=11+exp⁡(−x)\sigma(x) = \frac{1}{1 + \exp(-x)}

σ\sigma is the sigmoid activation function. It maps any total input to a probability between 0 and 1.

Compute the weighted sum once per direction. The forward pass reads the sum bottom-up. The backward pass reads the same weights top-down.

Worked example. Same tiny RBM: W11=0.6W_{11} = 0.6, W21=−0.4W_{21} = -0.4, b=(0.2,−0.2)b = (0.2, -0.2), c1=−0.1c_1 = -0.1. Input v=(1,0)v = (1, 0). First find the hidden probability, then reconstruct the visible layer from a sampled h1=1h_1 = 1.

10

Forward pass. Total input to the hidden unit:

a1=−0.1+0.6⋅1+(−0.4)⋅0=0.5P(h1=1∣v)=σ(0.5)=11+exp⁡(−0.5)=11+0.6065=11.6065≈0.622\begin{aligned} a_1 &= -0.1 + 0.6\cdot1 + (-0.4)\cdot0 = 0.5\\ P(h_1 = 1 \vert v) &= \sigma(0.5) = \frac{1}{1 + \exp(-0.5)}\\ &= \frac{1}{1 + 0.6065} = \frac{1}{1.6065}\\ &\approx \mathbf{0.622} \end{aligned}

Backward pass. Clamp h1=1h_1 = 1 and rebuild each visible unit:

P(v1=1∣h)=σ(0.2+0.6⋅1)=σ(0.8)=11+0.4493≈0.690P(v2=1∣h)=σ(−0.2+(−0.4)⋅1)=σ(−0.6)=1−σ(0.6)=1−0.6457≈0.354\begin{aligned} P(v_1 = 1 \vert h) &= \sigma(0.2 + 0.6\cdot1) = \sigma(0.8)\\ &= \frac{1}{1 + 0.4493} \approx \mathbf{0.690}\\ P(v_2 = 1 \vert h) &= \sigma(-0.2 + (-0.4)\cdot1) = \sigma(-0.6)\\ &= 1 - \sigma(0.6) = 1 - 0.6457 \approx \mathbf{0.354} \end{aligned}

The reconstruction (0.690,0.354)(0.690, 0.354) resembles the input (1,0)(1, 0). The first unit is likely on and the second is likely off.

Positive and negative phasesSearch on YouTube

The log-likelihood gradient has two parts. The positive phase raises the probability of observed data. The model notes which visible and hidden units fire together on real data. It strengthens weights between co-active patterns. The negative phase lowers the probability of false configurations. These are patterns the model likes but the data lacks. It weakens their weights.

ΔWij=η (⟨vihj⟩data−⟨vihj⟩model)\Delta W_{ij} = \eta\,\left(\langle v_i h_j\rangle_{\text{data}} - \langle v_i h_j\rangle_{\text{model}}\right)

η\eta is the learning rate. ⟨vihj⟩data\langle v_i h_j\rangle_{\text{data}} is the co-activation measured on real data. ⟨vihj⟩model\langle v_i h_j\rangle_{\text{model}} is the co-activation measured on reconstructions. Weights move toward the data and away from fantasy.

Worked example. Same tiny RBM with learning rate η=0.1\eta = 0.1. Training vector v=(1,0)v = (1, 0). The hidden sample from the data is h1=1h_1 = 1 (drawn with probability 0.622). One Gibbs round gives reconstruction v′=(1,0)v' = (1, 0) and a fresh hidden sample h1′=0h'_1 = 0 (possible since 1−0.622=0.3781 - 0.622 = 0.378).

Data association (positive phase).

⟨v1h1⟩data=1⋅1=1⟨v2h1⟩data=0⋅1=0\begin{aligned} \langle v_1 h_1\rangle_{\text{data}} &= 1\cdot1 = \mathbf{1}\\ \langle v_2 h_1\rangle_{\text{data}} &= 0\cdot1 = \mathbf{0} \end{aligned}

Model association (negative phase).

⟨v1h1⟩model=1⋅0=0⟨v2h1⟩model=0⋅0=0\begin{aligned} \langle v_1 h_1\rangle_{\text{model}} &= 1\cdot0 = \mathbf{0}\\ \langle v_2 h_1\rangle_{\text{model}} &= 0\cdot0 = \mathbf{0} \end{aligned}

Weight update.

ΔW11=0.1⋅(1−0)=0.1,W11:0.6→0.7ΔW21=0.1⋅(0−0)=0,W21 stays at −0.4\begin{aligned} \Delta W_{11} &= 0.1\cdot(1 - 0) = \mathbf{0.1}, &\quad W_{11}: 0.6 \to \mathbf{0.7}\\ \Delta W_{21} &= 0.1\cdot(0 - 0) = \mathbf{0}, &\quad W_{21} \text{ stays at } \mathbf{-0.4} \end{aligned}

Check: with W11=0.7W_{11} = 0.7 the forward total is −0.1+0.7=0.6-0.1 + 0.7 = 0.6. Then σ(0.6)≈0.646\sigma(0.6) \approx 0.646, above 0.622. The data state gains probability, as wanted.

Contrastive divergence and Gibbs samplingSearch on YouTube

Exact gradients need sums over all states. Large models make the negative phase too complex. Contrastive divergence (CD) approximates the log-likelihood gradient. It uses Markov Chain Monte Carlo (MCMC) sampling. Gibbs sampling is the MCMC method used here. It samples a complex joint through its conditionals. Direct sampling is hard, but one-sided conditionals are easy. Once trained, the net generates data by sampling the learned distribution.

Follow these steps for CD with kk Gibbs rounds (CD-kk):

  1. Clamp a training vector on the visible layer.
  2. Sample the hidden layer from P(h∣v)P(h \vert v).
  3. Reconstruct the visible layer from P(v∣h)P(v \vert h), then resample hidden. Repeat kk times.
  4. Update weights with data association minus model association.

D-R-U: Data, Reconstruction, Update

Clamp the Data and read hidden activations. Reconstruct by Gibbs sampling. Update with data association minus model association.

CD with one Gibbs round (CD-1) already trains well in practice. Start chains from the data, not from noise. Few rounds keep each update cheap.

Do not stop at a large initial error. Random weights always start far from the input. The reconstruction loss falls over iterations.

RBM versus feedforward networksSearch on YouTube

AspectRBMFeedforward network
Graphbipartite and undirectedlayered and feedforward
Signal flowboth directions (vv to hh and hh to vv)one direction (input to output)
Goalmodel the joint P(v,h)P(v,h)map input to output
Typegenerativediscriminative
Learnsreconstruct inputspredict labels
Trainingcontrastive divergence with Gibbs samplinggradient descent with backpropagation

Stacked RBMs and pretrainingSearch on YouTube

Two RBM layers can sit inside a deeper network. Hidden layer 1 outputs feed hidden layer 2. The chain continues to a final classifying layer. Each RBM learns one layer at a time:

  1. Train RBM1 on the raw input with contrastive divergence.
  2. Freeze the weights of RBM1.
  3. Pass the data through RBM1 and read hidden activations.
  4. Train RBM2 on these activations as its input.
  5. Repeat for each deeper layer.

Freezing matters. It locks learned features before the next layer trains. Each stage then models its own input distribution. Stacked RBMs pretrain deep-belief networks this way.

Quick self-testSearch on YouTube

1. Visible vector v=(1,1)v = (1, 1), weights (0.4,0.4)(0.4, 0.4), hidden bias 00. Find P(h=1∣v)P(h = 1 \vert v).

Total a=0+0.4+0.4=0.8a = 0 + 0.4 + 0.4 = 0.8. Then σ(0.8)=1/(1+0.4493)≈0.690\sigma(0.8) = 1/(1 + 0.4493) \approx \mathbf{0.690}.

2. Hidden state h=1h = 1, weight 0.40.4, visible bias 0.10.1. Find P(v1=1∣h)P(v_1 = 1 \vert h).

Total a=0.1+0.4=0.5a = 0.1 + 0.4 = 0.5. Then σ(0.5)=1/(1+0.6065)≈0.622\sigma(0.5) = 1/(1 + 0.6065) \approx \mathbf{0.622}.

3. State A has energy −0.7-0.7 and state B has energy 0.70.7. How much more likely is A than B?

The ratio is exp⁡(0.7)/exp⁡(−0.7)=exp⁡(1.4)≈4.06\exp(0.7)/\exp(-0.7) = \exp(1.4) \approx \mathbf{4.06}. State A is about four times more likely. ZZ cancels in the ratio.

4. Learning rate η=0.5\eta = 0.5, data association 11, model association 0.250.25, old weight 0.60.6. Find the update and the new weight.

ΔW=0.5⋅(1−0.25)=0.5⋅0.75=0.375\Delta W = 0.5 \cdot (1 - 0.25) = 0.5 \cdot 0.75 = \mathbf{0.375}. New weight =0.6+0.375=0.975= 0.6 + 0.375 = \mathbf{0.975}.

5. Differentiate generative and discriminative models.

A generative model learns the data distribution and creates new samples. A discriminative model learns class boundaries and maps inputs to labels. An RBM is generative: it reconstructs inputs.

6. Differentiate a Boltzmann Machine and a Restricted Boltzmann Machine.

A BM links every unit to every other unit, including links inside a layer. An RBM links only across layers, with no intra-layer links. The RBM is a symmetric bipartite graph, so training stays tractable.

7. Why does training a full Boltzmann Machine fail on real datasets?

Gradients need sums over all visible and hidden states. The count grows as 2n2^n for nn binary units. Full BMs are therefore impractical beyond tiny sizes.

8. Why does training need both a positive and a negative phase?

The positive phase raises the probability of real data. The negative phase lowers the probability of false configurations. Without the negative phase, the model would spread high probability over unreal states too.

9. Differentiate the forward and backward passes of an RBM.

The forward pass maps visible input to a hidden activation pattern. The backward pass rebuilds visible input from a hidden pattern. Both use the same symmetric weights.

10. State the stacked RBM pretraining order. Why freeze each RBM?

Train RBM1 on raw input, freeze it, pass data through it, then train RBM2 on the activations. Repeat deeper. Freezing locks learned features, so each layer models a fixed input distribution.

  • A generative model learns the data distribution and creates new samples. A discriminative model maps inputs to labels.
  • A BM has visible units (data) and hidden units (latent variables) with symmetric all-to-all links and no self-connections.
  • Full BMs fail since gradients sum over 2n2^n states, which grows exponentially.
  • An RBM is a symmetric bipartite graph: no intra-layer links, one weight wijw_{ij} per direction pair.
  • The forward pass finds hidden activations; the backward pass reconstructs inputs with the same weights.
  • Energy E(v,h)=−∑bivi−∑cjhj−∑∑viWijhjE(v,h) = -\sum b_i v_i - \sum c_j h_j - \sum\sum v_i W_{ij} h_j; joint P(v,h)=exp⁡(−E)/ZP(v,h) = \exp(-E)/Z.
  • Conditionals P(hj=1∣v)P(h_j = 1 \vert v) and P(vi=1∣h)P(v_i = 1 \vert h) are sigmoids of the weighted sums.
  • CD update: ΔWij=η(⟨vihj⟩data−⟨vihj⟩model)\Delta W_{ij} = \eta(\langle v_i h_j\rangle_{\text{data}} - \langle v_i h_j\rangle_{\text{model}}).
  • Gibbs sampling alternates the two conditionals; CD-kk runs kk rounds from the data.
  • Stacked RBMs pretrain layer by layer: train, freeze, feed activations upward.