Analyzing and Improving Representations with the Soft Nearest Neighbor Loss

Nicholas Frosst, Nicolas Papernot, Geoffrey E. Hinton

Deep learning models are notoriously overconfident and frequently struggle to generalize or identify out-of-distribution data. While standard training objectives, like cross-entropy, focus on pushing different classes apart to achieve linear separability at the final layer, we have a limited understanding of the similarity structure within a network's hidden layers.

This paper explores the Soft Nearest Neighbor Loss (SNNL) as a tool to measure and manipulate "entanglement"—how closely representations of different classes are mixed in a neural network's hidden layers. Surprisingly, the authors find that deliberately maximizing this entanglement during training acts as a powerful regularizer, leading to better generalization and significantly improved uncertainty estimates.

Measuring and Maximizing Entanglement

The core of the methodology relies on SNNL. Intuitively, for a given point $i$, the SNNL is the negative log-probability that a randomly sampled neighbor $j$ (based on distance) belongs to the same class as $i$.

Key technical contributions include:

  • Temperature Scaling: A temperature parameter ($T$) is introduced to control the "perplexity" or effective neighborhood size. Low temperatures focus strictly on nearest neighbors, while high temperatures consider the global distribution.
  • Entanglement as an Objective: Instead of merely using SNNL as an analytical tool, the authors add it to the standard cross-entropy loss. Crucially, they apply a negative coefficient ($\alpha < 0$) to maximize entanglement in hidden layers during training.
  • Deep k-Nearest Neighbors (DkNN): To measure uncertainty, the authors utilize DkNN, which examines the labels of a test point's neighbors across all hidden layers. In an entangled representation space, an out-of-distribution input will have fewer neighbors from the predicted class, making it easier to flag.

Key Insights into Representation Geometry

The authors evaluate their approach on standard computer vision benchmarks (MNIST, Fashion-MNIST, SVHN, CIFAR-10) and uncover several counter-intuitive insights about how neural networks learn.

Hidden Layers Should be Entangled: While the final layer must be discriminative and disentangled to classify data, hidden layers actually benefit from being entangled. This forces the network to identify similarity structures and features that are shared across classes, preventing it from relying on class-specific memorization shortcuts.

The Evolution of Learning: By tracking SNNL during training, the authors demonstrate that lower layers naturally tend to entangle more over time as they discover shared features. Only the final layers consistently disentangle to satisfy the primary classification objective.

Entanglement of layers over time. Lower layers (darker lines) increase in entanglement as training progresses.

SNNL vs. Triplet Loss: Unlike the popular Triplet Loss, which only evaluates one positive and one negative example at a time, SNNL considers the entire batch. This results in representations that are more "spread out" and capture a richer global structure.

Comparison of SNNL and Triplet Loss. SNNL (right) results in more spread-out representations when maximized.

Results and Applications

Maximizing entanglement yields tangible benefits for model robustness and performance:

  • Improved Generalization: Models trained to maximize entanglement consistently outperformed baselines. For instance, an "Entangled" ResNet achieved a test accuracy of 91.22% on CIFAR-10, compared to the baseline's 90.78%.
  • Robust Uncertainty: Entangled models project out-of-distribution data into "no man's land"—areas of the representation space lacking neighbors from any training class. In adversarial settings (e.g., FGSM or BIM attacks), these models provided credibility scores that correlated much more closely with actual accuracy, meaning the model "knows" when it is uncertain.
  • Tracking Generative Progress: SNNL can also be used to track the progress of Generative Adversarial Networks (GANs). As a GAN improves, its synthetic data becomes increasingly entangled with real data in the input space, which is reflected by a rising SNNL score.

Limitations and Future Directions

While the improvements in test accuracy are consistent, they remain relatively marginal; the primary advantage of this method lies in representation geometry and uncertainty calibration. Additionally, the current formulation of SNNL relies on Euclidean distance. While effective in hidden representation spaces, Euclidean distance struggles in raw pixel space for complex datasets like CIFAR-10, where pixel distance fails to capture semantic similarity. Future work may explore entanglement in learned spaces for generative models to overcome these pixel-level limitations.

Summary updated

BibTeX
@inproceedings{frosst2019analyzing,
  author = {Nicholas Frosst and Nicolas Papernot and Geoffrey E. Hinton},
  title = {{Analyzing and Improving Representations with the Soft Nearest Neighbor Loss}},
  booktitle = {International Conference on Machine Learning},
  year = {2019},
  url = {https://openreview.net/forum?id=H1EPT5WdZH},
}
Figure