Semi-Supervised Learning with Deep Generative Models
- Publication
- NeurIPS 2014Conference
- First submitted to arXiv
- arXiv:1406.5298
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@misc{imagegenerationsemisupervisedvae2014,
title = {{Semi-Supervised Learning with Deep Generative Models}},
author = {Diederik P. Kingma and Danilo J. Rezende and Shakir Mohamed and Max Welling},
year = {2014},
eprint = {1406.5298},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/1406.5298}
}
Fifty thousand handwritten digits, and only a hundred of them come with answers, ten for each digit.
Trained on just those hundred, a nearest-neighbor classifier gets 25.81% wrong on the MNIST test set; AtlasRBF, the best semi-supervised method of the time in the paper's table, gets 8.10% wrong. This paper's best result is 3.33%.
There are four authors. Kingma and Welling had finished the VAE only half a year earlier; Rezende and Mohamed of DeepMind had, almost simultaneously and independently, proposed stochastic backpropagation, built on the same idea. This time the two pairs wrote a single paper together and took on a different question. Can a generative model do work for a classifier?
Defining the problem
In semi-supervised classification, there is a lot of data on hand, and only a small part of it is labeled. Image search, genomics, and speech are all like this; data are easy to come by, while labeling is expensive and slow. The question is whether the unlabeled data can help the classifier draw a better decision boundary than it would with the labeled data alone.
The paper's introduction surveys the methods of the time:
- Self-training: take the model's own most confident predictions as new labels and keep expanding the training set. It is a heuristic, and wrong predictions get reinforced right along with the rest.
- Transductive SVM (TSVM): maximize the margin while keeping unlabeled points away from the margin as much as possible. It becomes hard to optimize once there is a lot of unlabeled data.
- Graph-based methods: connect similar samples into a graph and let the labels propagate along it. The results are sensitive to the structure of the graph, and they require an eigenanalysis of the graph Laplacian, which limits their scale.
- Neural network methods: add an unsupervised penalty, such as an autoencoder, on top of the classifier. Among them, the Manifold Tangent Classifier (MTC) first learns the data manifold with a contractive autoencoder, then makes the classifier invariant to small perturbations along the manifold; AtlasRBF combines manifold learning with kernel methods and was among the best-performing methods of the time.
Generative approaches to semi-supervised learning had in fact been around for a long time. They treat the missing labels as missing values and fill them in with a density model of the data. The difficulty was that the models were too weak; Gaussian mixture models or HMMs needed many components to be good enough, and nonparametric models such as trees or Gaussian processes couldn't keep up in scalability or inference accuracy.
The VAE had just shown that a deep network used as a density model can be trained with minibatch gradients, with approximate inference handed off to an encoder. What this paper wants to know is whether, once labels are put into such a model, the 49,900 images without answers can really help the hundred that have them.
Solving the problem
The paper proposes three models, shown together here:

M1: features first, then classify
M1 is just an ordinary VAE, with the generative model of Eq. (1):
is a likelihood function whose parameters are given by a deep network; for MNIST it is Bernoulli. Training ignores the labels entirely and uses all fifty thousand images to maximize the VAE bound (Eq. 5). After training, each image goes through the encoder , the inferred serves as its features, and the features of the hundred labeled images are then used to train a classifier; the paper uses a TSVM.
In the MNIST experiments, has 50 dimensions, and the encoder and decoder each have two layers of 600 softplus units. Going from 784 pixel dimensions down to 50 leaves the classifier a much smaller space to deal with; the authors' argument is that these low-dimensional representations come from Gaussian posteriors produced by a chain of nonlinear transformations, so the classes should be easier to separate.
M1's limitation is just as clear. Feature learning knows nothing about the classification task, and the labels show up only once, in the final classifier.
M2: labels as a latent variable
M2 writes the class directly into the generative model. An image is produced by two latent variables, the class and a continuous (Eq. 2):
is the categorical distribution, and is fed into the decoder as a one-hot vector. and are independent in the prior, so to generate a "7," you can fix , draw a from to decide the writing style, such as stroke thickness and slant, and the decoder combines the two into an image.
For labeled data, is observed; for unlabeled data, is a latent variable just like and has to be inferred. Classification thus becomes part of the inference problem, since guessing an image's is computing its posterior to begin with.
The inference model has two parts (Eq. 4):
is an MLP that outputs probabilities for 10 classes, and once trained, it is the classifier we're after; infers the image's writing style given its class. The official code feeds the image and the one-hot together into the encoder's first hidden layer, and the decoder likewise feeds and together into its first layer. In the MNIST experiments, M2 uses a 50-dimensional and networks with one layer of 500 softplus units each.
The labeled bound
With a label, both and are observed, and only remains to be inferred. Taking apart the way the VAE derivation does gives Eq. (6). Below, the and subscripts are dropped:
. The paper defines as the negative of the bound, that is, the loss to be minimized. Compared with the VAE, there is just one extra term, ; when the prior is uniform (which is how the official code sets it), it is the constant and doesn't affect the gradient. The expectation of is the negative of the VAE's Gaussian KL and uses the same closed form; the reconstruction term is sampled with the reparameterization, as before.
The unlabeled bound
Without a label, has to be inferred as well. Treat and as one set of latent variables and factor the approximate posterior as ; the bound becomes:
The second step takes the expectation over first and then sums over . The in the denominator splits into ; in the expectation over , the part is exactly the labeled , while doesn't depend on and stays as it is. The third step writes as the entropy of . This is Eq. (7), written .
This expression says that an unlabeled image has to try all 10 classes. Compute the labeled bound once for each , weight the results by the probabilities the classifier gives, and finally add the entropy.
Where does this push the classifier? Let's work it out for one unlabeled image. Suppose the decoder has already learned something, and this image's bound is for , for , and for , with the other classes much lower.
- If the classifier puts all its probability on 7, , and the entropy is 0.
- If the classifier gives 0.5 each to 7 and 9, .
The best allocation can be computed directly. Let ; the maximum of over is , reached at , that is, a softmax over . Plugging in this example, the best classifier gives about 0.9975 to 7 and about 0.0025 to 9, with .
So maximizing the unlabeled bound pushes toward "whichever class's generative model best explains this image." is a lower bound on , so amounts to approximating the true class posterior .
There's a chicken-and-egg problem here. The decoder has to know "what a 7 looks like" before the unlabeled data can help the classifier; and which dimension of corresponds to the digit 7 is something only the hundred labeled images can tell.
Adding the classification term
Adding the two kinds of bound together gives the objective for the whole dataset (Eq. 8):
and are the empirical distributions of the labeled and unlabeled data.
The authors point out a flaw in this objective. appears only in , so the labeled data never train the classifier directly, because with a label the true is used and isn't needed at all. So in Eq. (9) they add a classification loss:
All experiments use . With at fifty thousand, is 5,000; the expectation after it averages over the hundred labeled images, so spread over each image, the cross-entropy carries a weight of 50. The first two terms are sums of fifty thousand bounds, each on the order of a hundred nats or more; with a weight of just 1, the classification term would be drowned out.
The paper also gives this term a second origin. Put a symmetric Dirichlet prior on the parameters of the categorical distribution and do variational inference on them, and an objective of the same form comes out.
How much does this term matter? The official Pyro tutorial implements M2 following this paper and compares three versions:
| Labels | Sample | Sum | Sum + class. term | Labeled only |
|---|---|---|---|---|
| 100 | 20.1 | 22.5 | 93.2 | 77.1 |
| 3000 | 19.8 | 89.3 | 95.8 | 91.1 |
Compiled for this note from the table in Pyro's "The Semi-Supervised VAE"; test accuracy in %, mean over five random samplings.
"Sample " draws one class from at each step, and "Sum " computes all 10 classes one by one as in Eq. (7); neither has the classification term. With Eq. (8) alone, accuracy with 100 labels is only about 20%; adding the classification term is what brings it to 93.2%, 16 percentage points above a classifier trained on the hundred labeled images alone. The tutorial says the third version reproduces the paper's results. Its setup isn't quite the same as the paper's; the images aren't binarized, and the weight of the classification term is chosen on a validation set.
Training and cost
Just as in the VAE, every term in Eqs. (6) through (9) can have its gradient estimated with the reparameterization, and and are updated together. Algorithm 2 is M2's training loop; each iteration draws a minibatch, infers and for the unlabeled data, computes the gradient of , and updates the parameters.
The pseudocode of Algorithm 2 says to draw one from ; Eq. (7) and the official code both compute all 10 classes one by one and then weight them. When implementing it, follow Eq. (7); the Pyro table above already shows the difference.
The price is computation. Section 3.3 puts the cost of each M1 update at (minibatch size, number of samples, and the cost of a forward pass through the network), and M2 at times that, where is the number of classes. With only 10 classes, MNIST is manageable, but with many classes it gets expensive, and the authors list this as the main limitation.
M1+M2: stacking two layers
The last model stacks the two. First train M1 to get a 50-dimensional representation for each image; then move M2 wholesale onto , so that M2 generates instead of generating pixels directly. The generative model now has two layers of random variables:
plays the role of in the original M2, and is the representation learned by M1; in the official code, is a Gaussian. M2 operates in a space of at most 50 dimensions, which costs less than working on 784-dimensional pixels.
The paper doesn't say much about why stacking works. M1's features alone already cut the TSVM's error rate from 16.81% to 11.82% (with 100 labels), and with M2 stacked on top, both the classifier and the generative model work in a space that is easier to separate; this is this note's inference.
The official code also has a step the paper doesn't mention. Before M1's features go into M2, the code takes a thousand unlabeled images, computes the standard deviation of the encoder mean in each dimension, and keeps only the dimensions whose standard deviation exceeds 0.1. Some latent dimensions in a VAE collapse entirely back onto the prior and record no information, and this step weeds them out up front. In addition, the that M2 sees in each minibatch is freshly sampled from , not a fixed mean.
Discussion
Experimental setup
MNIST's fifty thousand training images are split into a labeled part and an unlabeled part, with 100, 600, 1000, or 3000 labels and the same number of labels for each class; each label count is redrawn several times, and the mean and standard deviation are reported.
Section 4.4 initializes the parameters from with the biases set to 0, and optimizes with "a variant of RMSProp with momentum and initialization bias correction," using a learning rate of 0.0003, a first-moment decay of 0.1, and a second-moment decay of 0.001. MNIST pixels are first normalized to between 0 and 1 and then treated as Bernoulli probabilities, with a fresh binary image sampled for each minibatch, so that the Bernoulli likelihood lives up to its name. M2 also adds weight decay, corresponding to an prior on the parameters.
Converted, that "RMSProp variant" has and . In December of the same year, Kingma and Ba put it on arXiv under the name Adam.
The header of the official adam.py also says that this initialization bias correction is "not yet published at the time of writing." The update line, however, uses self.alpha, and the corrected step size alpha_t, computed on the line just above, is never used (issue #4 asked about exactly this in June 2015; replies so far: zero). By the calculation in the Adam note, with and and no correction, the first step is 3.16 times the configured value.
Main MNIST results
First, the baselines of the time; the numbers are test error rates:
| Labels | NN | TSVM | CAE | MTC | AtlasRBF |
|---|---|---|---|---|---|
| 100 | 25.81 | 16.81 | 13.47 | 12.03 | 8.10 |
| 600 | 11.44 | 6.16 | 6.3 | 5.13 | – |
| 1000 | 10.7 | 5.38 | 4.77 | 3.64 | 3.68 |
| 3000 | 6.04 | 3.45 | 3.22 | 2.57 | – |
Compiled from Table 1 of the paper; test error rate in %. The original table also has a CNN column (22.98, 7.68, 6.45, 3.35).
Then the paper's three models:
| Labels | M1+TSVM | M2 | M1+M2 |
|---|---|---|---|
| 100 | 11.82 | 11.97 | 3.33 |
| 600 | 5.72 | 4.94 | 2.59 |
| 1000 | 4.24 | 3.60 | 2.40 |
| 3000 | 3.49 | 3.92 | 2.18 |
Compiled from Table 1 of the paper; test error rate in %.
- M1+TSVM ties MTC. With 100 labels, it is 11.82 versus 12.03. The same TSVM gets 16.81% wrong on raw pixels and 11.82% on M1's features.
- M2 alone is about as good as M1, but unstable. With 100 labels it gets 11.97%, with a standard deviation of 1.71; with 3000 labels it gets 3.92%, which is actually worse than the 3.60% with 1000 labels, with a standard deviation of 0.63. No other model in the table gets a higher error rate from more labels.
- Stacking is what counts. M1+M2 is the lowest at all four label counts; with 100 labels it gets 3.33% (standard deviation 0.14), less than half of AtlasRBF's 8.10%. The more labels, the smaller the gap; with 3000 labels it is 2.18 versus MTC's 2.57.
The authors also trained M1+M2 with all the labels and got 0.96% error, among the best published results at the time for permutation-invariant MNIST classification, which uses no prior knowledge of image structure.
Class and style, separated

M2 keeps class and style in two separate variables, and this can be plotted directly. The authors train an M2 whose has only 2 dimensions, fix at 2, 3, and 4, and step across a grid from −5 to 5.
Characters at the same position in the three grids are written alike. The paper describes the left region as "upright writing styles" and the right as "slanted styles," independent of the class. Compare the VAE's 2D latent space in the previous note, where digit class and handwriting were mixed together in one map; here the class is handed to , and is left with only the style.

The second experiment uses a high-dimensional . Take a test image, pass it through the inference network to get its , fix , and generate one image for each from 0 to 9.
In the MNIST panel on the left, the ten digits in each row all carry the handwriting of the leftmost image; in the thick-stroked row every digit is thick, and in the right-slanting row every digit slants. On the right, SVHN consists of house numbers photographed in street scenes; each row keeps the original's background, font, and colors and swaps only the digit in the middle. Look closely, and the neighboring digits cut off at the edges are kept as well. The authors say these were the best simulations a generative model had achieved on these two datasets at the time.
SVHN and NORB
SVHN and NORB had no existing semi-supervised results to compare against, so the authors ran nearest neighbors (KNN) and a TSVM with an RBF kernel themselves, comparing each on raw features and on M1 features. Both datasets use only 1000 labels and go through PCA before entering the model.
| Dataset | KNN | TSVM | M1+KNN | M1+TSVM | M1+M2 |
|---|---|---|---|---|---|
| SVHN | 77.93 | 66.55 | 65.63 | 54.33 | 36.02 |
| NORB | 78.71 | 26.00 | 65.39 | 18.79 | – |
The SVHN row is compiled from Table 2 of the paper and the NORB row from Table 3; test error rate in %.
M1's features improve the TSVM on both datasets, from 66.55% to 54.33% on SVHN and from 26.00% to 18.79% on NORB. On SVHN, M1+M2 gets 36.02% wrong, far below the other columns, but that is still more than one wrong in every three; the NORB row has no M1+M2 result.
The official code
The authors put nips14-ssl, written in Theano, on GitHub, with the last push in January 2015. For the M1+M2 experiments, the README says "Each experiment will run for 3000 epochs; since this code is not GPU-optimized, running many epochs might take a few days to complete."
Compared with the paper, a few settings differ:
- The paper says M1 uses two layers of 600 units, but
learn_yz_x_ss.pyloads an M1 with two layers of 500 units; - The paper says M2 uses 500 units, but
run_2layer_ssl.pyswitches to 300 with 100 labels; - The step mentioned earlier, dropping the dimensions with a standard deviation below 0.1, isn't in the paper;
- The bias correction in
adam.pyis computed but never used.
None of these are big, but anyone trying to reproduce the 3.33% from the paper should go by the code.
What came later
That number didn't stand for long. In 2015, the Ladder Network reached 1.06% error with 100 labels on the same permutation-invariant MNIST; in its comparison table, this paper is listed above it under the name "DGN," at 3.33%.
This paper itself lists a few directions in Section 5, namely variational inference over the parameters as well, convolutional networks, and dealing with the linear slowdown as the number of classes grows.
Conclusion
M2 writes the label as a latent variable in the generative model, treated as an observation when the label is there and summed over all classes with weights when it isn't, which makes the classifier part of the inference model. On its own it is about as good as M1 and not very stable; stacked on the representation learned by M1, it brings the MNIST error rate with 100 labels down to 3.33%, far ahead of everything in the comparison table of the time.
When implementing M2, compute the bound once for every class on unlabeled data and weight the results, rather than drawing one class from and calling it done; always add the classification term, with a weight on the same scale as the whole bound (the paper uses ); and take a look at whether your optimizer's bias correction is actually in effect.
The version in the Pyro tutorial that follows only Eq. (8) and draws one class per step gets about 20% accuracy with 100 labels. Guessing at random among ten classes gets 10%.