# Embedding Dimension of Contrastive Learning and $k$-Nearest Neighbors

Arboricity of comparison graphs bounds the embedding dimension for contrastive and k-NN data

Avdiukhin, Dmitrii, Chatziafratis, Vaggos, Fischer, Orr, Yaroslavtsev, Grigory · Advances in Neural Information Processing Systems · 2024-12-16

- **Problem** Before this work, no theory gave the embedding dimension needed for contrastive triplets or k-NN lists.
- **Method** The authors bound the needed dimension by the arboricity of each comparison's constraint graph, which counts how many forests cover its edges.
- **Results** The authors show an ℓ2 dimension of order √m suffices for m contrastive triplets and is necessary.

## Why embedding dimension matters for distance comparisons

Embedding dimension is a key design choice when a deep learning architecture is picked. The question here is the smallest dimension d of an ℓp-space that can hold a given set of distance comparisons. Two kinds of comparison data are studied: contrastive triplets and k-NN lists.

Each contrastive triplet says the positive example must sit closer to the anchor than the negative one, and the embedding must respect every such inequality. Before this work there was no theoretical study of the embedding dimension for these settings.

For k-NN, each item comes with its ordered list of nearest neighbors, which is a dense set of comparisons. Read as triplets, the k-NN information amounts to n(n − 1) constraints, far more than a bound linear in n would suggest.

## Turning comparisons into a constraint graph

Each contrastive sample adds edges from the anchor to its positive and negative examples, skipping edges that already exist. In the k-NN setting, each point gets an edge to each of its k nearest neighbors.

Arboricity is the least number of forests whose union covers all edges, so it stays small for sparse graphs and grows when a dense subgraph appears. Fact 7 bounds the arboricity of a graph with m edges by roughly the square root of m, so m samples give arboricity around √m at most.

An orientation of the edges gives an ordering in which each point has only a few earlier neighbors, and the later constructions rely on this ordering. For the k-NN graph the arboricity is at most k + 1.

## An ℓ2 embedding driven by arboricity

Using the bound on arboricity for m samples gives the contrastive ℓ2 result, a dimension of order m^{1/2}.

The same theorem applied to the k-NN graph gives an ℓ2 dimension of order k that preserves the ordering of each list. This embedding preserves the order of each list but can move points so that a non-neighbor becomes a neighbor.

## Extending the bounds to ℓp and ℓ∞

For integer p ≥ 1 the construction reduces to a partial semimetric and embeds it in dimension at most m + 1. Table 1 lists an upper bound of O(m) for integer p, with lower bounds that differ between even and odd p.

For ℓ∞ the points split into high-degree and low-degree sets, with the high-degree set of size O(m^{2/3}). Low-degree points are handled with graph colorings and high-degree points with a Fréchet-like embedding, which gives dimension O(m^{2/3}).

## Preserving the order of k-NN lists

The k-NN constraint graph has arboricity at most k + 1, which makes the arboricity bound applicable. Theorem 6 gives an ℓ2 embedding of dimension O(k) that keeps the k-NN ordering.

For ℓp with p ≥ 1, Theorem 10 gives dimension O(r^2 log^3 n), so with r at most k + 1 the ordering needs roughly k^2 up to logarithmic factors.

## Keeping the k-NN sets as nearest neighbors

The embedding of Theorem 10 keeps the order but can change which points are nearest, so an extension is needed. The extension gives each point a random choice among design vectors, so neighbors pick the same vector more often than non-neighbors.

Sharing a vector adds nothing to the distance between two points, while different vectors add at least (2 − α)R. Lemma 21 then shows that the gap between neighbor distances and non-neighbor distances exceeds the spread among neighbors.

The construction uses L = Θ(r^4 log^4 n) parts, and the final dimension is O(k^10 log^10 n) for constant p ≥ 1. A footnote notes that later versions improved the analysis to Õ(k^3).

## Hardness and partial satisfaction

With d = 1, finding an ℓ2 embedding that satisfies given contrastive constraints is NP-hard, by reduction from betweenness. So no polynomial algorithm exists in the realizable case unless P = NP.

If only an α fraction of constraints must hold and α is at most 1/2, one random dimension already satisfies half of them in expectation. Above the threshold α\* ≈ 0.77, satisfying the constraints needs dimension of order √m for even p.

## CIFAR experiments on contrastive triplets

The authors build triplets from ground-truth distances of a pretrained ResNet-18 and train new ResNet-18 models from scratch with triplet loss for 50 epochs. The output dimension is set by the last fully-connected layer, and results on CIFAR-10 and CIFAR-100 are averaged over 5 runs with embedding dimension 128 in the sample sweep.

When d is at least √m, the learned embedding satisfies at least 99% of the triplets. The authors read this as consistent with Theorem 1.

Moving from 64 to 256 adds 2% accuracy, while moving from 256 to 1024 adds none.

## Limitations

The experiments use only CIFAR-10 and CIFAR-100 with ResNet-18.

The partial-satisfaction result leaves a gap between α = 1/2 and α\* ≈ 0.77, which the authors expect may be improvable. The authors note that the dependence on k and log n in the k-NN bound may be improvable, and that data-dependent bounds are not captured.

## Paper info

- **Authors**: Avdiukhin, Dmitrii, Chatziafratis, Vaggos, Fischer, Orr, Yaroslavtsev, Grigory
- **Venue**: Advances in Neural Information Processing Systems · Published
- **First published**: 2024-12-16
- **Added**: 2026-11-25
- **DOI**: 10.52202/079017-1309
- **Original paper terms**: [Check in the original ↗](https://papers.nips.cc/paper_files/paper/2024/hash/487c9d6ef55e73aa9dfd4b48fe3713a6-Abstract-Conference.html)
- **What we show**: Paper info and commentary

[Original paper ↗](https://papers.nips.cc/paper_files/paper/2024/hash/487c9d6ef55e73aa9dfd4b48fe3713a6-Abstract-Conference.html)

AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.

## Quote from the paper

Avdiukhin, Dmitrii, Chatziafratis, Vaggos, Fischer, Orr, Yaroslavtsev, Grigory (2024). Embedding Dimension of Contrastive Learning and $k$-Nearest Neighbors. Advances in Neural Information Processing Systems. https://doi.org/10.52202/079017-1309

**BibTeX**

```
@article{dmitrii2024embedding,
  title = {{Embedding Dimension of Contrastive Learning and $k$-Nearest Neighbors}},
  author = {Avdiukhin, Dmitrii and Chatziafratis, Vaggos and Fischer, Orr and Yaroslavtsev, Grigory},
  year = {2024},
  journal = {Advances in Neural Information Processing Systems},
  doi = {10.52202/079017-1309},
  url = {https://papers.nips.cc/paper_files/paper/2024/hash/487c9d6ef55e73aa9dfd4b48fe3713a6-Abstract-Conference.html}
}
```

## Other paper commentary

- [Open-fMIND: A Large-Scale Open-Access fMRI Corpus for Pretraining Brain Foundation Models](https://refivu.com/en/papers/6f4ae9bb-4461-4449-a68a-a651a7b86a71-open-fmind-a-large-scale-open-access-fmri.md)
- [Breaking feature collapse in self-supervised time series encoders](https://refivu.com/en/papers/819f640a-5ad8-4d90-8850-3c039f21f3d1-breaking-feature-collapse-in-self-supervised-time-series.md)
- [Cracks in Embeddings: Robustness and Stability of JEPA-Based Chest X-Ray Representations](https://refivu.com/en/papers/daee5a77-a7ae-4a85-aa91-3c0350658e14-cracks-in-embeddings-robustness-and-stability-of-jepa.md)
- [Self-supervised Graph Learning for Recommendation](https://refivu.com/en/papers/101a7b08-1d43-4cb8-8a03-8ebbce6be79e-self-supervised-graph-learning-for-recommendation.md)
- [Representation Engineering: A Top-Down Approach to AI Transparency](https://refivu.com/en/papers/f6b6fa1e-01cf-4bad-a9dc-8b83f1107927-representation-engineering-a-top-down-approach-to-ai.md)
- [Text Embeddings by Weakly-Supervised Contrastive Pre-training](https://refivu.com/en/papers/d1351af6-6e66-4a03-9f02-44cc61903f8b-text-embeddings-by-weakly-supervised-contrastive-pre.md)
- [Unlocking Noise-Resistant Vision: Key Architectural Secrets for Robust Models Against Gaussian Noise](https://refivu.com/en/papers/9fe8e470-936c-4fac-810d-ab4f5fb89aeb-unlocking-noise-resistant-vision-key-architectural-secrets.md)
- [ProMiSE: Protein Multi-State Evaluation Benchmark in Biological Contexts](https://refivu.com/en/papers/e163e455-fb04-4bd6-8669-b48b49fec9e9-promise-protein-multi-state-evaluation-benchmark-in.md)
- [SeeA\*: Efficient Exploration-Enhanced A\* Search by Selective Sampling](https://refivu.com/en/papers/80e3d55e-cbd4-4f3f-88b9-118fcd1bb5c9-seea-efficient-exploration-enhanced-a-search-by-selective.md)
- [Unitary Convolutions for Message-passing and Positional Encodings on Directed Graphs](https://refivu.com/en/papers/cf7ea629-41a3-4349-add7-79a6b104140f-unitary-convolutions-for-message-passing-and-positional.md)
- [Principled SVD-based Delta Compression via Quantization Error Minimization](https://refivu.com/en/papers/007f6db5-bad8-41e3-a36e-3c5ad8cf77a4-principled-svd-based-delta-compression-via-quantization.md)
- [Understanding the Parameter Space Volume of Transformers Encoding Boolean Functions](https://refivu.com/en/papers/f4dbf6ce-3357-44a3-b4ed-38147e384f67-understanding-the-parameter-space-volume-of-transformers.md)

## Terms in this paper

- [CIFAR-100](https://refivu.com/en/terms/5afa3b877d18-cifar-100)
- [Triplet loss](https://refivu.com/en/terms/6b3cf5362b16-triplet-loss)
- [contrastive learning](https://refivu.com/en/terms/180d2c06550d-contrastive-learning)
- [ResNet-18](https://refivu.com/en/terms/1cdf53b7347d-resnet-18)
- [k-NN](https://refivu.com/en/terms/f56ac38408a9-k-nn)
- [Chernoff bound](https://refivu.com/en/terms/2bd82b5d0473-chernoff-bound)

---

AI-written text can contain mistakes. [Refivu](https://refivu.com/en/papers/9df2424e-149a-4953-912c-2bdfc9b778bc-embedding-dimension-of-contrastive-learning-and-k-nearest)
