Semi-supervised Variational Autoencoder

Machine Learning
Introduced by
Kingma, D. P.; Mohamed, S.; Rezende, D. J.; Wierstra, D.
Year
2014
Type
Generative probabilistic model (semi-supervised)
Family
Deep learning / NLP / CV

What does it do?

The semi-supervised VAE (M2 model) is a deep generative method that learns a latent representation of inputs and a classifier jointly, using both labeled and unlabeled examples within a probabilistic framework.

It was introduced by Kingma et al. in 2014. Its main use is classification when labels are scarce, since the generative objective on unlabeled inputs can inform the classifier.

The intuition behind the method

Labeling is usually expensive, so datasets often contain many more unlabeled examples than labeled ones, and a standard classifier trained on the labeled subset ignores the rest. The semi-supervised VAE instead treats the class label of an unlabeled point as a latent variable to be inferred, and trains a generative model (decoder) and a recognition network (encoder plus classifier) together. Because every input must be reconstructed whether or not it has a label, unlabeled examples also shape the learned representation, which is how they can improve classification when labels are few. Whether this helps on a given dataset depends on how well the unlabeled data matches the labeled data.

History

The variational autoencoder was introduced by Kingma and Welling in 2014 as a framework for deep generative modeling with amortized variational inference. In the same year, Kingma, Mohamed, Rezende, and Wierstra extended the framework to semi-supervised learning with the M1 and M2 models, which is the model described on this page.

When to use it

Choose the semi-supervised VAE when a large pool of unlabeled examples exists, labels cover only a small part of it, and classification or structured prediction is the goal. It is most relevant where labeling is expensive, such as medical imaging, genomics, or specialized text corpora. Compared with a purely supervised model it is worth considering when the labeled set is small, and compared with an unsupervised VAE it is the natural choice once any labels are available. Do not use it when ample labeled data exists, since the added objective complexity may not pay off, or when the computational budget is very tight. A simpler model, such as logistic regression on pretrained embeddings, may be sufficient when it already captures the data well.

Limitations and considerations

  • Training is more complex than a standard classifier: the reconstruction-based ELBO and the cross-entropy term must be balanced through the alpha hyperparameter.
  • Requires tuning of the KL-term weight and of alpha; poor settings can lead to posterior collapse, where the latent code carries little information, or to the classifier making little use of the labeled signal.
  • Computational cost grows with the number of classes, because each unlabeled point must be evaluated across all class values.
  • Gains over simpler semi-supervised baselines may shrink when strong self-supervised pretraining, such as contrastive learning or masked autoencoders, is available.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Uses unlabeled examples to shape the learned latent representation, because every input contributes through the reconstruction term.
  • Trains a generative and a discriminative objective in one probabilistic framework rather than fitting a classifier alone.
  • Because the decoder is conditioned on the class label, the latent code is not required to carry all class information, which can support generation and interpolation alongside classification.
  • Can give reasonable classification accuracy at low label rates compared with purely supervised baselines; the size of this advantage depends on the dataset and is not quantified on this page.
  • The variational posterior gives a probabilistic latent representation, although it is an approximation and this page does not establish how well calibrated its uncertainty is.

Applications

  • Medical image classification with limited expert annotations.
  • Text categorization on specialized corpora where annotation is costly.
  • Learning latent representations for downstream tasks that have few labels.
  • Possible uses where labeled assay data is scarce, such as drug discovery or genomics, although these are plausible rather than established applications of this model.

How it works

How it works

1

An encoder network maps each input x (together with the label y when it is known or being considered) to a posterior distribution over a continuous latent variable z, parameterized by a mean vector and a diagonal covariance. The latent code is typically much lower-dimensional than x, which limits how much information about x it can carry.

Formula
q_{\phi}(z|x) = \mathcal{N}(\mu_{\phi}(x),\,\sigma^{2}_{\phi}(x))
2

A classification head in the recognition network models the posterior over the discrete class label y given x. For labeled examples the true label is used and a cross-entropy term, weighted by alpha, trains the classifier. For unlabeled examples y is unknown, so the bound is computed for each possible class and weighted by the predicted class probability q(y|x), and the entropy of q(y|x) is added; this marginalization is what allows unlabeled data to contribute.

Formula
q_{\phi}(y|x) = \text{Cat}(\pi_{\phi}(x))
3

To allow gradient-based training, the sample z is written as a deterministic function of the encoder outputs (mean and standard deviation) plus noise epsilon drawn from a standard normal distribution. This makes the sampling step differentiable, so gradients can flow back to the encoder parameters through z.

Formula
z = \mu + \sigma \odot \epsilon,\quad \epsilon \sim \mathcal{N}(0,I)
4

A decoder network reconstructs x from the sampled z and the class label y, which is known for labeled data and inferred for unlabeled data. Supplying the class label to the decoder directly means the latent code does not have to carry all class information and can encode other variation in the data.

Formula
p_{\theta}(x|y,z)
5

The displayed formula is the bound for a labeled point: a reconstruction term, a KL term that keeps the posterior close to the prior over z, and a classification term weighted by alpha. In the full model the labeled bound is L(x,y) = E_q(z|x,y)[log p(x|y,z)] + log p(y) - KL(q(z|x,y) || p(z)). For an unlabeled point the bound is U(x) = sum over y of q(y|x) (-L(x,y)) + H(q(y|x)), which marginalizes over the unknown label. The total objective sums L(x,y) over labeled points, adds alpha times the classification cross-entropy on labeled points, and sums U(x) over unlabeled points. Maximizing it trains encoder, decoder, and classifier together.

Formula
\mathcal{L} = \mathbb{E}_{q}[\log p_{\theta}(x|y,z)] - D_{\mathrm{KL}}(q_{\phi}||p) + \alpha \log q_{\phi}(y|x)

Common pitfalls

  • Setting alpha too low so the classifier barely benefits from the labeled signal, leading to poor discrimination despite good reconstruction.
  • Ignoring posterior collapse: if the KL weight is too high, the encoder ignores the input and the latent code carries no useful information.
  • Evaluating only reconstruction loss or generative quality without measuring classification accuracy on a held-out labeled test set.
  • Treating all unlabeled examples as equally informative without checking for distribution shift between labeled and unlabeled subsets.
  • Skipping any mitigation for early training instability. KL annealing or warm-up is a common way to reduce instability and posterior collapse, though it is not always necessary.

Frequently asked questions

How does the semi-supervised VAE differ from a plain VAE?

A plain VAE is purely generative and has no explicit classifier. The semi-supervised VAE adds a recognition network for the class label y, treats unlabeled labels as latent variables, and includes a supervised cross-entropy term in the objective, making it jointly generative and discriminative.

What is the alpha hyperparameter?

Alpha scales the supervised classification term relative to the unsupervised ELBO. A higher alpha gives more weight to classification on labeled examples, and a lower alpha lets the generative objective dominate. This page does not give a formula or value for choosing alpha; it is typically tuned against accuracy on held-out labeled data.

What happens if the labeled fraction is very small, say under 1%?

At very low label rates the generative term still provides a training signal from unlabeled data, but performance is sensitive to the choice of prior and to the balance between objectives. Results should be checked on a held-out labeled set, because a very small labeled fraction makes accuracy estimates noisy.

Can the method handle high-dimensional inputs like full-resolution images?

Yes, in principle. Convolutional encoder and decoder layers are commonly used for image data. Because unlabeled points are evaluated across all classes, training cost grows with the number of classes, so very large label spaces increase training time noticeably.

Is posterior collapse a serious concern here?

Posterior collapse, where the encoder ignores the input and the KL term vanishes, is a known failure mode for VAEs. Applying KL annealing (warming up the KL weight from zero), using free bits, or adopting a more expressive posterior can mitigate it.

Related methods

Sources

  1. Kingma, D. P., Mohamed, S., Rezende, D. J., & Wierstra, D. (2014). Semi-supervised learning with deep generative models. Advances in Neural Information Processing Systems (NeurIPS), 27, 3581–3589.

    Open publication reference
  2. Kingma, D. P., & Welling, M. (2014). Auto-Encoding Variational Bayes. International Conference on Learning Representations (ICLR 2014).

    Open publication reference