# Representation Learning with Contrastive Predictive Coding

Contrastive predictive coding learns representations by predicting future latent codes

Aaron van den Oord, Yazhe Li, Oriol Vinyals · arXiv · 2018-7-10

- **Problem** Generative models must rebuild every detail of a raw signal, which wastes capacity on information the context does not share. Predicting the signal itself is hard to make useful for learning features without labels.
- **Method** CPC predicts future latent codes and scores them with a contrastive loss instead of generating signals.
- **Results** On ImageNet with a ResNet v2 101 encoder, CPC scored 48.7 top-1, compared to 39.6 for colorization.

## Why generative prediction stalls

Predicting what is missing is an old and attractive way to learn features without labels. The difficulty starts when the thing to be predicted is a raw signal such as an image or a waveform. A generative model asked to produce such a signal has to reconstruct every detail, and much of that detail is unrelated to the context. An image holds thousands of bits, while a class label carries about 10 bits for 1,024 categories, so the mismatch is large.

The authors instead target the information that a future signal x and its context c have in common, measured as mutual information. Predicting further ahead leaves less shared information, so the model has to learn longer-range structure. Such slow features, like phonemes and intonation in speech or objects in images, are often the most useful ones.

## Predicting latents with a density ratio

The key idea is to compress each signal into a latent vector first, then predict future latents from a summary of the past. The model then only has to tell whether a candidate future latent matches the context, using sampled comparison values. An encoder genc turns each observation x\_t into z\_t, possibly at a lower temporal resolution. An autoregressive model gar reads all latents up to time t and produces the context c\_t.

The model doesn't generate the future distribution directly, and instead learns a score that is proportional to a density ratio between the conditional and the marginal density. The score is an exponential of a bilinear form, \\\[ f\_k(x\_{t+k}, c\_t) = \\exp\\left(z\_{t+k}^{T} W\_k c\_t\\right) \\\] Each step k gets its own matrix W\_k, so predicting further ahead uses different weights.

Any positive score would work in principle, and the authors chose this log-bilinear form for its simplicity.

Either z\_t or c\_t can serve as the representation, depending on the task. The context c\_t helps when past information matters, as in speech recognition, while z\_t may be enough when no extra context is needed. For whole-image or whole-sequence tasks, the representations are pooled over all locations.

## Training with InfoNCE

Training uses InfoNCE over a set X of N samples, with one positive from the conditional distribution and N−1 negatives from the proposal distribution. The loss is \\\[ L\_N = -\\,\\mathbb{E}\_X\\left\[\\log \\frac{f\_k(x\_{t+k}, c\_t)}{\\sum\_{x\_j \\in X} f\_k(x\_j, c\_t)}\\right\] \\\] It is a categorical cross-entropy, so the model learns to pick out the positive sample among the candidates.

The optimal posterior that x\_i is the positive is its ratio p(x\_i\|c\_t)/p(x\_i) divided by the sum of that ratio over all N candidates. Because of this, the optimal score is proportional to the density ratio, and it doesn't depend on how many negatives are drawn.

The loss also gives a bound, I(x\_{t+k}, c\_t) ≥ log(N) − L\_N, which tightens as N grows. So minimizing L\_N maximizes a lower bound on the mutual information between c\_t and x\_{t+k}. The encoder and the autoregressive model are trained jointly, so the whole model learns end to end.

## Speech probing results

The audio experiments use a 100-hour subset of LibriSpeech, with phone labels obtained by forced alignment with Kaldi. The encoder has five strided convolutions on the 16KHz waveform, with a feature every 10ms of speech. A GRU with a 256-dimensional hidden state provides the context, and the model predicts 12 steps ahead from it. The model trains on audio windows of length 20480 until convergence, which the authors report at roughly 300,000 updates.

For probing, the GRU outputs are extracted for the whole dataset, and only a multi-class linear logistic regression is trained on them. On phone classification with 41 classes, CPC scored 64.6, compared to 39.7 for MFCC features and 27.6 for a randomly initialized model. The supervised model with the same architecture scored 74.6, so CPC closes much of the gap without labels.

On speaker identity among 251 speakers, CPC scored 97.4, compared to 17.6 for MFCC features. The supervised model scored 98.5, so the linear probe comes close to the fully supervised network. The authors read this as CPC capturing both speaker identity and speech content. A single hidden layer lifts phone accuracy from 64.6 to 72.5, so some encoded information isn't linearly accessible.

## Prediction horizon and negative sampling

The first ablation varies the number of predicted steps, and phone accuracy rises from 28.5 with 2 steps to 64.6 with 12. Accuracy then slips to 63.8 with 16 steps, so 12 steps gave the best value in this sweep. The authors conclude that predicting multiple steps matters for learning useful features.

The second ablation keeps 12 steps and changes where the negatives come from. Mixed-speaker negatives scored 64.6, and same-speaker negatives scored 65.5. When the current sequence is excluded, mixed-speaker negatives drop to 57.3, while same-speaker negatives stay at 64.6. Drawing all negatives from the current sequence gives 65.2. So the source of negatives matters most when the current sequence is excluded and negatives come from mixed speakers.

## ImageNet linear probing

The image encoder is a ResNet v2 101 trained without pretraining and without Batch-Norm. Each 256x256 image gives a 7x7 grid of 64x64 crops with 32 pixels of overlap. The third residual block output is spatially mean-pooled, so each crop becomes a 1024-dimensional vector and the image becomes a 7x7x1024 tensor.

A PixelCNN-style autoregressive model predicts up to five rows of latents from the top down. The contrastive loss is applied to each patch in the predicted rows. For evaluation, the 7x7x1024 features are pooled into one 1024-dimensional vector, and a linear layer is trained on ImageNet labels. The earlier setup in \[36\] used a 3x3x1024 representation without pooling, which gives its linear mapping more parameters.

CPC scored 48.7 top-1, compared to 39.6 for colorization, the best ResNet-V2 entry listed. Top-5 accuracy was 73.6, compared to 69.3 for the combination of four earlier methods. The authors report gains of 9% absolute in top-1 and 4% absolute in top-5 over the state of the art. The Jigsaw entry in the AlexNet block isn't directly comparable, because its architecture differs.

## Sentence transfer on BookCorpus

The text model trains on BookCorpus, using a 1D-convolution sentence encoder that maps each sentence to a 2400-dimensional vector. Evaluation follows the skip-thought protocol, with logistic regression trained on the sentence features for five benchmarks.

On TREC, CPC scored 96.8, compared to 91.4 for skip-thought vectors. On MR, CPC scored 76.9, compared to 79.5 for skip-thought with layer normalization. The authors argue that the method is about as accurate as skip-thought and trains faster, because it needs no LSTM word decoder. They also found that better children's book relationships didn't always transfer to these targets.

## Reinforcement learning with the auxiliary loss

The authors add CPC as an auxiliary loss to a batched A2C agent on five DeepMind Lab tasks, without a replay buffer. The unroll length is 100 steps, and predictions reach up to 30 steps ahead. Hyperparameters are searched randomly over entropy regularization, learning rate and RMSProp epsilon.

After 1 billion frames, four of the five tasks improve with the auxiliary loss. On lasertag\_three\_opponents\_small the loss neither helps nor hurts. The authors suspect the task doesn't need memory, so a purely reactive policy suffices there.

## Limitations

Linear probes measure only part of what the features hold, since a hidden layer raises phone accuracy from 64.6 to 72.5. The linear-probe numbers therefore understate what the features contain.

The audio model's context was capped at 20480 timesteps, slightly more than a second. The authors state that longer segments would likely give better results.

The ImageNet comparison isn't a controlled match with \[36\], because the probe pooling differs. The sentence results cover five benchmarks with BookCorpus as source, and the RL results cover five DeepMind Lab tasks. The lasertag explanation is the authors' suspicion, and the text reports no test of it.

## Paper info

- **Authors**: Aaron van den Oord, Yazhe Li, Oriol Vinyals
- **Venue**: arXiv · Preprint
- **First published**: 2018-7-10
- **Added**: 2026-11-11
- **Updated**: 2019-1-22
- **Version**: v2
- **arXiv**: 1807.03748
- **Category**: cs.LG · stat.ML
- **Original paper terms**: [arXiv non-exclusive 1.0](https://arxiv.org/licenses/nonexclusive-distrib/1.0/)
- **What we show**: Paper info and commentary

[Original paper ↗](https://arxiv.org/abs/1807.03748v2) · [PDF ↗](https://arxiv.org/pdf/1807.03748v2)

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

Aaron van den Oord, Yazhe Li, Oriol Vinyals (2018). Representation Learning with Contrastive Predictive Coding. arXiv:1807.03748v2. https://arxiv.org/abs/1807.03748v2

**BibTeX**

```
@misc{oord2018representation,
  title = {{Representation Learning with Contrastive Predictive Coding}},
  author = {Aaron van den Oord and Yazhe Li and Oriol Vinyals},
  year = {2018},
  eprint = {1807.03748},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1807.03748v2}
}
```

## Other paper commentary

- [GloVe: Global Vectors for Word Representation](https://refivu.com/en/papers/d4308808-78ba-4fc7-a70c-6e67f906e337-glove-global-vectors-for-word-representation.md)
- [Anomaly Detection on Attributed Networks via Contrastive Self-Supervised Learning](https://refivu.com/en/papers/c20bdbfe-b419-4df5-8f33-b1066c4f4576-anomaly-detection-on-attributed-networks-via-contrastive.md)
- [Self-supervised Learning for Human Activity Recognition Using 700,000 Person-days of Wearable Data](https://refivu.com/en/papers/7d06c47d-a111-423f-a4f8-68d244e1093c-self-supervised-learning-for-human-activity-recognition.md)
- [Multi-task Self-Supervised Learning for Human Activity Detection](https://refivu.com/en/papers/ae7c537d-33fb-4fec-8014-37dab4536341-multi-task-self-supervised-learning-for-human-activity.md)
- [Neural Discrete Representation Learning](https://refivu.com/en/papers/111a9c5f-9d7d-4bff-bbad-a962285094a3-neural-discrete-representation-learning.md)
- [Momentum Contrast for Unsupervised Visual Representation Learning](https://refivu.com/en/papers/4093c4bb-ce8b-4d26-bded-60ec02bc0fac-momentum-contrast-for-unsupervised-visual-representation.md)
- [YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors](https://refivu.com/en/papers/c2cb4858-244f-407a-916b-4d41a9df7d4f-yolov7-trainable-bag-of-freebies-sets-new-state.md)
- [Communication-Efficient Learning of Deep Networks from Decentralized Data](https://refivu.com/en/papers/67777aca-9be4-4537-8568-875c944fceee-communication-efficient-learning-of-deep-networks-from.md)
- [Empirical Evaluation of Gated Recurrent Neural Networks on Sequence Modeling](https://refivu.com/en/papers/136a3b1f-3eb6-469b-8a8a-7536056851d5-empirical-evaluation-of-gated-recurrent-neural-networks-on.md)
- [Convolutional Neural Networks for Sentence Classification](https://refivu.com/en/papers/e03eeb97-8544-4ef6-a8e6-47276e072373-convolutional-neural-networks-for-sentence-classification.md)
- [A Survey on Deep Learning in Medical Image Analysis](https://refivu.com/en/papers/4d173b56-9ea6-4644-98f2-af1794e63965-a-survey-on-deep-learning-in-medical-image.md)
- [The Unreasonable Effectiveness of Deep Features as a Perceptual Metric](https://refivu.com/en/papers/7a2a39ad-a72d-46f5-b96c-8392e55b66ee-the-unreasonable-effectiveness-of-deep-features-as-a.md)

## Terms in this paper

- [Top-1 accuracy](https://refivu.com/en/terms/653c0b9eaa58-top-1-accuracy)
- [Adam optimizer](https://refivu.com/en/terms/aff9e901333e-adam-optimizer)
- [t-SNE](https://refivu.com/en/terms/488ecf7dbbcf-t-sne)
- [top-5 accuracy](https://refivu.com/en/terms/6de0752ac469-top-5-accuracy)
- [GRU](https://refivu.com/en/terms/911a73dfeb1f-gru)
- [mutual information](https://refivu.com/en/terms/31de24f17a5e-mutual-information)
- [Negative sampling](https://refivu.com/en/terms/1dbdea971d8c-negative-sampling)
- [InfoNCE](https://refivu.com/en/terms/9909993ddc7f-infonce)
- [LibriSpeech](https://refivu.com/en/terms/0144d6cc5a9d-librispeech)
- [ACC](https://refivu.com/en/terms/e6cad80f6e60-acc)
- [TREC](https://refivu.com/en/terms/aaff5299ce84-trec)
- [MR](https://refivu.com/en/terms/8b735ad0001e-mr)
- [DeepMind Lab](https://refivu.com/en/terms/1d226c83e6ae-deepmind-lab)
- [linear classifier](https://refivu.com/en/terms/1781c7b77353-linear-classifier)
- [A2C](https://refivu.com/en/terms/473baa3a4fc6-a2c)
- [Skip-thought vectors](https://refivu.com/en/terms/9fd372ffab2e-skip-thought-vectors)

---

AI-written text can contain mistakes. [Refivu](https://refivu.com/en/papers/cd05bbc6-eff6-4b02-b9ef-45070ef6704a-representation-learning-with-contrastive-predictive-coding)
