Alleviating Adversarial Attacks on Variational Autoencoders with MCMC

Anna Kuzina, Max Welling, Jakub Mikolaj Tomczak

Variational Autoencoders (VAEs) are powerful generative models, but they harbor a significant security flaw: they are notoriously fragile to adversarial attacks. A tiny, imperceptible perturbation to an input image can trick the encoder into mapping the image to a completely incorrect, low-probability region of the latent space. This results in nonsensical reconstructions and catastrophic failures in downstream tasks like classification.

Previous attempts to fix this vulnerability usually involve modifying the training objective or adding architectural constraints, which often introduces a "robustness-accuracy trade-off"—the model becomes harder to fool but performs worse on clean data. To address this, researchers have proposed a "plug-and-play" defense mechanism using Markov Chain Monte Carlo (MCMC) during the inference step, significantly improving robustness without requiring any changes to the training process.

The MCMC "Pull-Back" Defense

The core insight of this approach is probabilistic: an adversarial attack works by moving the latent code $z$ to a region where the true posterior $p(z|x)$ has very low density. To counteract this, the authors introduce a defense mechanism based on Hamiltonian Monte Carlo (HMC).

Schematic of the MCMC defense mechanism pulling attacked latent representations back to high-probability regions.
  1. Standard Encoding: The attacked input $x^a$ is passed through the encoder to get an initial corrupted latent code $z^a$.
  2. MCMC Refinement: Instead of using $z^a$ directly, it serves as a starting point for an MCMC chain. The system runs several steps of HMC to sample from the target distribution $\pi(z) = p_\theta(z|x^a) \propto p_\theta(x^a|z)p(z)$.
  3. The "Pull-Back": Because MCMC naturally moves toward regions of higher probability, it effectively "corrects" the adversarial perturbation. It steers the latent code back toward the "typical set" of the true posterior.

Crucially, this method is model-agnostic. It can be applied to pre-trained VAEs, $\beta$-VAEs, or complex hierarchical models like NVAE. Theoretical analysis proves that these MCMC steps reduce the Total Variation distance between the corrupted and clean latent distributions.

Key Insights and Empirical Results

The researchers validated their approach across several standard benchmarks, including MNIST, Fashion MNIST, Color MNIST, and CelebA.

Restoring the Posterior Density Attacks significantly lower the posterior density of the latent code. By calculating the "log posterior ratio," the authors demonstrate empirically that HMC consistently moves the code back to "safer," high-density regions.

Histogram showing the log posterior ratio shifting back toward the original distribution after applying HMC.

Robustness to Adaptive Attacks Even if an attacker knows the defender is using MCMC, creating a successful attack becomes much harder. The stochastic nature of MCMC and the requirement to fool the refined posterior make the optimization landscape significantly more difficult for the attacker.

Synergy with Hierarchical Models The defense is particularly effective for hierarchical VAEs (like NVAE). By updating all levels of latent variables simultaneously during the HMC steps, the model can recover complex structural information lost during the attack.

Performance Gains The results show a consistent and significant boost in robustness across datasets:

  • Reconstruction Similarity: On MNIST, under an attack of radius 0.1, a standard VAE's reconstruction similarity (MSSSIM) was 0.70. Adding HMC boosted this to 0.88.
  • Classification Accuracy: In adversarial settings on MNIST, the accuracy of a downstream classifier dropped to a measly 8% for a standard VAE. With the MCMC defense, it recovered to 25% (a 3x improvement).
  • MSE Reduction: On Fashion MNIST, the Mean Squared Error (MSE) of reconstructions under attack dropped from 814.2 to 764.2 when using the defense.

Limitations and Future Directions

The primary limitation of this approach is computational overhead. Running multiple steps of HMC (up to 1000 steps in testing) is significantly slower than a single forward pass through an encoder. This creates a trade-off between the level of robustness and inference speed, potentially limiting its use in real-time applications.

Future work may explore more sophisticated adaptive attacks and investigate how this MCMC-based approach might be integrated with other defense strategies to provide even stronger guarantees for generative models.

Summary updated

BibTeX
@inproceedings{kuzina2022alleviating,
  author = {Anna Kuzina and Max Welling and Jakub Mikolaj Tomczak},
  title = {{Alleviating Adversarial Attacks on Variational Autoencoders with MCMC}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2022},
  url = {https://openreview.net/forum?id=458a8dN8L6},
}
Figure