Learning Data Representations with Joint Diffusion Models

Kamil Deja, Tomasz Trzcinski, Jakub M. Tomczak

TL;DR

This paper introduces a novel joint diffusion model that shares internal data representations between a generative diffusion process and a discriminative classifier. By training end-to-end without the need for unstable MCMC sampling, the model achieves state-of-the-art results in both image generation and classification, while naturally enabling seamless visual counterfactual explanations.

What Problem Does This Paper Address?

A long-standing goal in machine learning is to build "hybrid" or "joint" models capable of both generating data (synthesis) and labeling it (classification). In theory, learning how to generate an object should make a model a better classifier, and vice versa.

However, existing joint models face a harsh trade-off. Energy-based models (like JEM) suffer from severe training instability and rely on computationally expensive Markov Chain Monte Carlo (MCMC) sampling. Other frameworks, like Variational Autoencoders (VAEs) or Normalizing Flows, require altering the log-likelihood objective function, forcing practitioners to prioritize either generation quality or classification accuracy at the expense of the other.

The authors address this gap by leveraging Diffusion-based Deep Generative Models (DDGMs). They explore whether the highly effective internal representations naturally built by diffusion models can be stably shared with an integrated classifier to benefit both tasks simultaneously.

Methods

The authors propose a Joint Diffusion Model that shares the encoder architecture of a standard UNet denoising model with a classifier.

  1. Feature Pooling: Features from different depth levels of the UNet encoder ($Z_t$) are average-pooled and concatenated into a single vector ($z_t$) representing the image at a given diffusion timestep $t$.
  2. Joint Training: The model is optimized end-to-end using a single optimizer. The objective combines a standard cross-entropy loss for the classifier (evaluated on clean images $x_0$) and a simplified diffusion loss (predicting the added noise).
  3. Noisy Classifier Training: To boost robustness, the authors introduce an alternative training scheme where the classifier is also trained on intermediate noisy images ($x_t$ for $t > 0$). This acts as a structured form of data augmentation.
  4. Conditional Sampling: Instead of using an external, independently trained classifier to guide generation (the standard approach in diffusion models), they optimize the internal representations ($Z_t$) directly using gradients from the joint classifier. This steers the backward diffusion process cleanly toward a target class.
Fig. 1: Data representation zt in a UNet-based diffusion model.

Datasets

The model was evaluated across several standard image benchmarks and a real-world medical dataset:

  • FashionMNIST: Grayscale clothing images.
  • SVHN: Street View House Numbers.
  • CIFAR-10 & CIFAR-100: Standard natural image classification benchmarks.
  • CelebA: Large-scale face attributes dataset (used to analyze feature granularity and counterfactuals).
  • MALARIA: A real-world medical dataset containing 27,558 cell images (infected vs. uninfected) used to validate visual counterfactual explanations.

Key Insights and Ideas

  • Feature Granularity vs. Timestep: A key discovery is that diffusion models learn features at different levels of abstraction depending on the noise level. High-grained features (like hair color) emerge at late diffusion steps closer to pure noise, whereas low-grained features (like glasses or necklaces) only appear during early steps when the image is mostly intact.
  • Mutual Optimization Benefit: The shared encoder benefits from a dual force: the generative task forces the classifier to be highly robust to noise, while the classification task drives the generative latent features toward distinct class modes, improving generation precision.
  • Inherent Counterfactuals: Because the generator and classifier share the exact same representation space, you can easily ask the model: "What is the minimal change needed to flip this classification label?" The diffusion decoder can then cleanly synthesize that exact visual change (e.g., adding or removing a malaria parasite from a cell image).
Fig. 2: (a) The test-set accuracy of a stand-alone classifier compared to a classifier trained on top of data representations from a pre-trained diffusion model extracted from original images x0. (b) The area under the ROC curve (AUC) for logistic regression models fit on data representations extracted with a pre-trained diffusion model at ten different diffusion timesteps. High-grained features are already distinguishable at late diffusion steps (closer to random noise), while low-grained features are only represented at the earlier stage of the forward diffusion.

Key Results

  • Classification Accuracy: The joint model consistently outperformed standalone classifiers and previous state-of-the-art hybrid models, achieving 97.4% on SVHN, 96.4% on CIFAR-10, and 77.6% on CIFAR-100.
  • Generation Quality: The model shattered previous hybrid model generation benchmarks. On CIFAR-10, it achieved a Fréchet Inception Distance (FID) score of 6.4 (with conditional sampling), compared to 20.2 for GenViT and 30.5 for VERA.
  • Data Efficiency: In semi-supervised setups with only 5% labeled data, the joint model performed nearly as well as a standalone classifier trained on 100% of the data. It also demonstrated excellent unsupervised domain adaptation capabilities (e.g., adapting from SVHN to MNIST without target labels).

Limitations and Future Work

  • Loss of Spatial Information: Average pooling across the UNet layers collapses spatial dimensions. This means the classifier loses explicit information about where certain features are located in the image, which might limit performance on more complex, multi-object scenes.
  • Basic Domain Adaptation Setup: For the domain adaptation experiments, the authors used a very simple setup—fine-tuning solely on the target domain's diffusion loss. They note that incorporating advanced techniques like adversarial training (e.g., Domain-Adversarial Neural Networks) could further improve feature alignment between different domains.

Summary updated

BibTeX
@inproceedings{deja2023learning,
  author = {Kamil Deja and Tomasz Trzcinski and Jakub M. Tomczak},
  title = {{Learning Data Representations with Joint Diffusion Models}},
  booktitle = {ECML/PKDD},
  year = {2023},
  url = {https://openreview.net/forum?id=QaZa3805LR},
}
Figure