Variational Classification

Shehzaad Dhuliawala, Mrinmaya Sachan, Carl AllenarXiv 2023

TL;DR

Researchers have re-interpreted the standard neural network softmax classifier as a latent variable model, revealing that standard training often causes internal representations to "collapse" into overconfident points. By introducing Variational Classification (VC)—a training objective that aligns these internal representations with a structured probability distribution—they significantly improve model calibration, adversarial robustness, and performance in low-data regimes without losing classification accuracy.

What Problem Does This Paper Address?

Despite their success, standard softmax classifiers (trained with cross-entropy loss) suffer from several well-known "pathologies." They are often miscalibrated (overconfident in their errors), vulnerable to adversarial attacks (tiny input changes causing huge output shifts), and brittle when faced with distribution shifts (real-world data looking different from training data).

The authors argue these issues stem from a lack of probabilistic rigor in how we treat the "latent space"—the layer just before the final softmax output. Standard training encourages the model to map all samples of a single class to the same point in this space to minimize loss. This "latent collapse" discards valuable information about data variety and uncertainty, leading to the brittle behavior we see in production models.

Methods

The core contribution is a theoretical bridge between discriminative classification and generative latent variable models (LVMs).

  1. The Latent Perspective: The authors model classification as a process of reversing a generative chain: Class ($y$) $\to$ Attributes ($z$) $\to$ Observation ($x$). They show that a standard softmax classifier is actually a special, "collapsed" case of this model.
  2. The VC Objective: They derive a new training objective based on the Evidence Lower Bound (ELBO), similar to what is used for Variational Auto-Encoders (VAEs).
  3. Distribution Alignment: The "Variational Classification" objective adds terms to ensure that the empirical distribution (what the encoder actually produces) matches the anticipated distribution (what the softmax layer theoretically expects to see to produce accurate probabilities).
  4. The Density Ratio Trick: Because the empirical distribution is only accessible via sampling, the authors use an auxiliary adversarial task. They train a set of binary discriminators (one per class) to distinguish between real latent samples and samples from the desired prior. This allows them to minimize the Kullback-Leibler (KL) divergence between the two distributions without needing an analytical form for the encoder's output.

This progression is visualized in the paper's first figure, showing how adding priors and entropy "fills out" the latent space:

Empirical distributions of inputs to the output layer for different objectives. MLE (left) shows collapse, while the full VC objective (right) fits the anticipated Gaussian priors.

Datasets

The authors evaluated VC across a wide range of benchmarks:

  • Vision: MNIST, CIFAR-10, CIFAR-100, and Tiny-ImageNet using ResNet architectures.
  • Medical: 10 different MedMNIST datasets to test real-world complexity and varying sample sizes.
  • Text: AGNews for news classification.
  • Robustness/Shift: CIFAR-10-C, CIFAR-100-C, and Tiny-Imagenet-C (synthetic corruptions); SNLI/MNLI (natural language inference); and Camelyon17 (cancer detection in microscopic images).

Key Insights and Ideas

  • Softmax is a "Flipped" Bayes' Rule: The paper proves that the softmax layer implicitly assumes latent variables follow an exponential family distribution. VC makes this assumption explicit and enforces it during training.
  • Latent Collapse is Optional: While standard cross-entropy encourages mapping everything to a point (MLE), VC treats the latent space as a full distribution (Bayesian). This preservation of "volume" in the latent space is what provides the robustness.
  • Generative Benefits in a Discriminative Model: By better reversing the generative process (Figure 2), the model gains properties usually associated with generative models, like better uncertainty estimation, while remaining a fast, discriminative classifier.
Figure 2: Variational Classification, reversing the generative process: qϕ(z|x) maps data x∈X to the latent space Z, where empirical distributions qϕ(z|y) are fitted to class priors pθ(z|y); top layer computes pθ(y|z) by Bayes’ rule to give a class prediction p(y|x).

Key Results

  • Calibration: VC consistently produces more "honest" confidence scores. On CIFAR-100, the Expected Calibration Error (ECE) dropped from 11.1% for standard softmax to 4.8% for VC.
  • Low-Data Efficiency: VC shines when data is scarce. On the AGNews dataset with only 50 samples, VC achieved 66.3% accuracy, compared to 56.3% for standard cross-entropy.
  • Adversarial Robustness: Without any specific adversarial training, VC models were significantly more resistant to FGSM attacks. As shown in Figure 4, VC maintains much higher accuracy as the attack magnitude ($\epsilon$) increases.
Prediction accuracy for increasing FGSM adversarial attacks.
  • Robustness to Shift: Under increasing levels of data corruption (distribution shift), VC maintains a lower calibration error than standard models, even those using post-hoc fixes like temperature scaling (Figure 3).
Calibration under distribution shift.

Limitations and Future Work

  • Encoder as a Black Box: The authors focused on the interaction between the latent space and the output layer. They treat the rest of the neural network (the encoder) as a black box, leaving open the question of how internal layers contribute to this distribution shaping.
  • Deterministic Encoders: In this work, the encoder $q(z|x)$ is still a deterministic function (a delta distribution). A natural next step is to make the encoder itself stochastic, fully mirroring the VAE architecture.
  • Peaky Distributions: The authors note that their current optimal empirical distributions are still "peaky" approximations of the priors, suggesting that the objective function could be further refined to achieve a perfect distributional match.

Summary updated

Abstract

We present a latent variable model for classification that provides a novel probabilistic interpretation of neural network softmax classifiers. We derive a variational objective to train the model, analogous to the evidence lower bound (ELBO) used to train variational auto-encoders, that generalises the softmax cross-entropy loss. Treating inputs to the softmax layer as samples of a latent variable, our abstracted perspective reveals a potential inconsistency between their anticipated distribution, required for accurate label predictions, and their empirical distribution found in practice. We augment the variational objective to mitigate such inconsistency and induce a chosen latent distribution, instead of the implicit assumption found in a standard softmax layer. Overall, we provide new theoretical insight into the inner workings of widely-used softmax classifiers. Empirical evaluation on image and text classification datasets demonstrates that our proposed approach, variational classification, maintains classification accuracy while the reshaped latent space improves other desirable properties of a classifier, such as calibration, adversarial robustness, robustness to distribution shift and sample efficiency useful in low data settings.
BibTeX
@misc{dhuliawala2023variational,
  author = {Shehzaad Dhuliawala and Mrinmaya Sachan and Carl Allen},
  title = {{Variational Classification}},
  year = {2023},
  eprint = {2305.10406},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2305.10406},
}
Figure