Bayesian Prompt Learning for Image-Language Model Generalization

Mohammad Mahdi Derakhshani, Enrique Sanchez, Adrian Bulat, Victor Guilherme Turrisi da Costa, Cees G. M. Snoek, Georgios Tzimiropoulos, Brais Martinez

This paper, titled "Bayesian Prompt Learning for Image-Language Model Generalization," presents a probabilistic approach to adapting foundational models like CLIP to new tasks. By moving from deterministic "soft prompts" to a Bayesian framework, the authors significantly improve how these models handle unseen data and domain shifts.

TL;DR

The authors reformulate prompt learning as a variational inference problem, treating prompts as probability distributions rather than fixed vectors. This Bayesian approach acts as a powerful regularizer, preventing the model from overfitting to specific training examples and leading to superior generalization across 15 different benchmarks.

What Problem Does This Paper Address?

Foundational vision-language models (VLMs) like CLIP are typically adapted to downstream tasks via prompt learning. Instead of manual templates (e.g., "a photo of a {}"), researchers use "soft prompts"—learnable vectors optimized through backpropagation.

However, standard prompt learning relies on Empirical Risk Minimization (ERM). In few-shot settings (where only a few images per class are available), ERM is prone to overfitting. The model learns "spurious features" specific to the training images rather than generalizable concepts. Consequently, a prompt that works perfectly for "seen" classes often fails when applied to "unseen" classes or when the image domain changes (e.g., moving from photos to sketches).

Methods

The core innovation is framing prompt learning through the lens of Variational Inference.

  1. Probabilistic Prompts: Instead of learning a single fixed prompt vector, the model learns a distribution over prompts. They model the prompt as a fixed set of learnable tokens plus a conditional residual $r$ that follows a Gaussian distribution: $r \sim \mathcal{N}(\mu(x), \Sigma(x))$.
  2. Posterior Network: The authors introduce a small neural network, $\pi_\phi(x)$, that takes image features and predicts the parameters ($\mu$ and $\Sigma$) of this distribution.
  3. Variational Lower Bound: The training objective is modified to include a KL-divergence term. This forces the learned distribution to stay close to a simple prior, effectively regularizing the prompt space and preventing the model from "memorizing" the training set.
  4. Variational Ensemble: At inference time, the model doesn't just use one prompt. It samples $K$ different residuals from the learned distribution, generates $K$ separate classifier weights, and averages the results. This "ensemble" approach is much more robust to noise and distribution shifts.
Figure 1: We present a Bayesian perspective on prompt learning by formulating it as a variational inference problem (right column). Our framework models the prompt space as an a priori distribution which makes our proposal compat- ible with common prompt learning approaches that are un- conditional (top) or conditional on the image (bottom).

Datasets

The authors evaluated their method on a massive suite of 15 benchmarks:

  • Generalization to Unseen Classes: 11 datasets including ImageNet, Caltech101, OxfordPets, StanfordCars, Flowers102, Food101, FGVCAircraft, SUN397, UCF101, DTD, and EuroSAT.
  • Cross-Domain Generalization: Training on ImageNet and testing on robust variants: ImageNetV2, ImageNet-Sketch, ImageNet-A, and ImageNet-R.

Key Insights and Ideas

  • Regularization through Uncertainty: By treating prompts as distributions, the model is forced to learn a "region" in the embedding space that works for a class, rather than a single point. This naturally ignores high-frequency, spurious features that don't generalize.
  • The Power of Sampling: The "aha moment" is that sampling multiple prompts from the distribution and averaging them (the variational ensemble) consistently outperforms using just the mean prompt. As shown in the paper's analysis, as you ensemble more samples, the cosine similarity between the text and image encodings increases.
  • Capturing Factors of Variation: The Bayesian approach allows the model to capture different visual styles within a single class. For example, in a flower dataset, different modes of the distribution might correspond to different sub-species or lighting conditions.
Figure 2: Variational distribution interpretation on the EuroSAT dataset. The text encoding of the mean prompt pµ(x) ( ) is the most similar to the image encoding. As we move further away from the mean prompt, the cosine similarity scores between the text encoding and image encoding decrease further ( ). When we ensemble the text encoding of different prompts the cosine similarity increases ( ), where the maximum similarity is obtained when all text encodings are combined.

Key Results

  • Superior Generalization: On the task of generalizing to unseen classes, the Bayesian method outperformed the previous state-of-the-art (CoCoOp) by 3.25% on average across 11 datasets.
  • Robustness to Domain Shift: When trained on ImageNet and tested on out-of-distribution data (like sketches or adversarial examples), the method showed consistent gains, improving accuracy on ImageNet-R by 1.79% over standard CoOp.
  • Efficiency: While sampling adds some overhead, the authors found that just 20 Monte Carlo samples provide a near-optimal trade-off between accuracy and inference speed.

Limitations and Future Work

  • In-Domain Trade-off: The authors note a slight "generalization-specialization" trade-off. While their model is much better at new tasks, it can slightly underperform deterministic models on the exact training data (in-domain) because the regularization prevents it from perfectly memorizing those specific samples.
  • Inference Latency: Sampling $K$ times is computationally more expensive than a single forward pass, which might be a concern for real-time applications on edge devices.
  • Future Directions: The authors suggest exploring more complex distribution models beyond Gaussians, such as Normalizing Flows or Diffusion Models, to represent the prompt space even more accurately.

Summary updated

BibTeX
@inproceedings{derakhshani2022bayesian,
  author = {Mohammad Mahdi Derakhshani and Enrique Sanchez and Adrian Bulat and Victor Guilherme Turrisi da Costa and Cees G. M. Snoek and Georgios Tzimiropoulos and Brais Martinez},
  title = {{Bayesian Prompt Learning for Image-Language Model Generalization}},
  booktitle = {UvA-DARE},
  year = {2022},
}
Figure