Ru00y.
Jun 22, 2026 · 17 min

Noise Contrastive Estimation

How turning 100,000-way classification into binary classification makes language model training tractable and sparked modern contrastive learning.

1. Why Normalization Hurts#

Given a context hh and model parameters θ\theta, predicting the next word ww uses the softmax:

Pθh(w)=exp(sθ(w,h))Zθh=uθ(w,h)ZθhP_\theta^h(w) = \frac{\exp(s_\theta(w, h))}{Z_\theta^h} = \frac{u_\theta(w, h)}{Z_\theta^h}

where sθ(w,h)s_\theta(w, h) is the logit (score) for word ww, uθ(w,h)=exp(sθ(w,h))u_\theta(w, h) = \exp(s_\theta(w, h)) is the unnormalized score, and Zθh=wVexp(sθ(w,h))Z_\theta^h = \sum_{w' \in V} \exp(s_\theta(w', h)) is the partition function that sums over every word in the vocabulary VV.

When V|V| ranges from 10510^5 to 10610^6, each training step must enumerate the entire vocabulary just to compute ZθhZ_\theta^h. The MLE gradient reveals the bottleneck:

θEwPdh[logPθh(w)]=EwPdh[sθ(w,h)θ]positive contribution from real wordsEwPθh[sθ(w,h)θ]must sum over all words!\frac{\partial}{\partial \theta} \mathbb{E}_{w \sim P_d^h}[\log P_\theta^h(w)] = \underbrace{\mathbb{E}_{w \sim P_d^h}\Big[\frac{\partial s_\theta(w,h)}{\partial\theta}\Big]}_{\text{positive contribution from real words}} - \underbrace{\mathbb{E}_{w \sim P_\theta^h}\Big[\frac{\partial s_\theta(w,h)}{\partial\theta}\Big]}_{\text{must sum over all words!}}

The core problem: MLE demands the gradient of ZθhZ_\theta^h at every step, and that gradient is an expectation over the model’s own distribution PθhP_\theta^h, which covers the entire vocabulary. Millions of training steps times a 10510^5-word sum is infeasible.

To see exactly how this gradient breaks down, start from the MLE objective: maximize the expected log-probability EwPdh(w)[logPθh(w)]\mathbb{E}_{w \sim P_d^h(w)}[\log P_\theta^h(w)]. Write the log-probability as logPθh(w)=loguθ(w,h)logZθh\log P_\theta^h(w) = \log u_\theta(w,h) - \log Z_\theta^h. The second term does not depend on which ww was drawn from the data, so it sits outside the expectation over ww. Taking the derivative: θEwPdh[logPθh(w)]=EwPdh[θloguθ(w,h)]θlogZθh\frac{\partial}{\partial\theta} \mathbb{E}_{w \sim P_d^h}[\log P_\theta^h(w)] = \mathbb{E}_{w \sim P_d^h}[\frac{\partial}{\partial\theta} \log u_\theta(w,h)] - \frac{\partial}{\partial\theta} \log Z_\theta^h.

The first term only touches words that actually appear in the training batch — cheap. The second term is the problem. Expanding it step by step: θlogZθh=1ZθhZθhθ\frac{\partial}{\partial\theta} \log Z_\theta^h = \frac{1}{Z_\theta^h} \cdot \frac{\partial Z_\theta^h}{\partial\theta} (chain rule on ddxlogf=f/f\frac{d}{dx}\log f = f'/f), then =1Zθhθwexp(sθ(w,h))= \frac{1}{Z_\theta^h} \cdot \frac{\partial}{\partial\theta} \sum_{w'} \exp(s_\theta(w', h)) (expand ZθhZ_\theta^h‘s definition), then =1Zθhwθexp(sθ(w,h))= \frac{1}{Z_\theta^h} \cdot \sum_{w'} \frac{\partial}{\partial\theta} \exp(s_\theta(w', h)) (swap sum and derivative), then =1Zθhwexp(sθ(w,h))sθ(w,h)θ= \frac{1}{Z_\theta^h} \cdot \sum_{w'} \exp(s_\theta(w', h)) \cdot \frac{\partial s_\theta(w', h)}{\partial\theta} (chain rule: ddxef=eff\frac{d}{dx}e^f = e^f \cdot f'), then =wexp(sθ(w,h))Zθhsθ(w,h)θ= \sum_{w'} \frac{\exp(s_\theta(w', h))}{Z_\theta^h} \cdot \frac{\partial s_\theta(w', h)}{\partial\theta} (push 1/Z1/Z back into the sum), which is =EwPθh[sθ(w,h)θ]= \mathbb{E}_{w' \sim P_\theta^h}[\frac{\partial s_\theta(w', h)}{\partial\theta}].

That last line is the killer: EwPθh\mathbb{E}_{w' \sim P_\theta^h} means every word in the vocabulary contributes to the gradient, weighted by the model’s own softmax probability. Expanding it explicitly: EwPθh[sθθ]=Pθh(cat)sθ(cat,h)θ+Pθh(dog)sθ(dog,h)θ+Pθh(the)sθ(the,h)θ+\mathbb{E}_{w' \sim P_\theta^h}[\frac{\partial s_\theta}{\partial\theta}] = P_\theta^h(\text{cat}) \cdot \frac{\partial s_\theta(\text{cat}, h)}{\partial\theta} + P_\theta^h(\text{dog}) \cdot \frac{\partial s_\theta(\text{dog}, h)}{\partial\theta} + P_\theta^h(\text{the}) \cdot \frac{\partial s_\theta(\text{the}, h)}{\partial\theta} + \cdots. Each word needs one forward pass (computing Pθh(w)P_\theta^h(w')) times one backward pass (computing sθθ\frac{\partial s_\theta}{\partial\theta}). For a 100,000-word vocabulary, each gradient descent step costs 100,000 forward-backward passes.

In plain terms: MLE pushes the score of the correct word up, but to prevent the model from cheating — giving every word an infinite score — it also pushes all word scores down, weighted by how likely the model currently thinks each word is. That “push all scores down” step runs over the whole vocabulary. NCE sidesteps this by reformulating the problem entirely.

Notation and Probability Quick-Start#

If the equations feel unfamiliar, the issue is likely probability notation, not NCE itself. Here is a minimal primer.

SymbolHow to read itMeaningExample
P(w)P(w)”probability of wwProbability a random variable takes the value wwP(cat)=0.3P(\text{cat}) = 0.3 means cat appears 30% of the time
P(wh)P(w \mid h)”probability of ww given hhConditional probability: ww‘s probability once we know hhGiven the previous word is “the”, how likely is the next word “cat”?
wPw \sim Pww follows distribution PPww is sampled from the probability distribution PPwPdw \sim P_d means ww comes from the data distribution
EwP[f(w)]\mathbb{E}_{w \sim P}[f(w)]”expectation of f(w)f(w) under PPWeighted average, using PP as the weightsFor details see the explanation below
w\sum_w”sum over all wwAdd up over every possible valuewV\sum_{w \in V} means summing over the whole vocabulary
Pdh(w)P_d^h(w)data distributionThe true frequency of word ww given context hh in training dataHow often “cat” really follows “the” in human text
Pθh(w)P_\theta^h(w)model distributionThe model’s predicted probability of ww given hh (with parameters θ\theta)The softmax output
Pn(w)P_n(w)noise distributionAn arbitrary distribution we choose ourselvesA unigram frequency distribution (common words get higher probability)
θ\frac{\partial}{\partial\theta}“partial derivative with respect to θ\thetaGradient: how the function changes when θ\theta movesLθ\frac{\partial L}{\partial\theta} is the gradient of the loss

The expectation E\mathbb{E} is simply a weighted average: EwP[f(w)]=wP(w)f(w)\mathbb{E}_{w \sim P}[f(w)] = \sum_{w} P(w) \cdot f(w). Take each possible value of f(w)f(w), multiply it by how often it occurs under P(w)P(w), and sum. If your vocabulary has three words and your model assigns P(cat)=0.5P(\text{cat})=0.5, P(dog)=0.3P(\text{dog})=0.3, P(the)=0.2P(\text{the})=0.2, with scores s(cat)=10s(\text{cat})=10, s(dog)=5s(\text{dog})=5, s(the)=2s(\text{the})=2, then EwP[s(w)]=0.5×10+0.3×5+0.2×2=6.9\mathbb{E}_{w \sim P}[s(w)] = 0.5 \times 10 + 0.3 \times 5 + 0.2 \times 2 = 6.9. That is all there is to it.

The vertical bar in P(wh)P(w \mid h) means “under the condition that.” P(rain)=0.3P(\text{rain}) = 0.3 is the baseline; P(raindark clouds)=0.8P(\text{rain} \mid \text{dark clouds}) = 0.8 is the updated belief once you have seen clouds. In language modeling, P(catthe)P(\text{cat} \mid \text{the}) is the probability of “cat” given that the preceding word was “the.”

Bayes’ rule is the central tool in NCE’s derivation. Its essence is flipping conditions around: P(AB)=P(A,B)P(B)P(A \mid B) = \frac{P(A, B)}{P(B)}, which says: the probability of A once you know B is the fraction of times A and B occur together, divided by how often B occurs at all. If “cat and from real data” happens 5 times out of 100, and “cat” appears 20 times total (real or noise), then a cat-token is from real data with probability 0.05/0.20=0.250.05 / 0.20 = 0.25. NCE applies exactly this logic: given a word ww, work backwards to decide whether it came from real data or from noise.

2. Core Idea: Classification, Not Counting#

NCE introduces a noise distribution Pn(w)P_n(w) (a simple, fast-to-sample distribution like unigram frequency) and defines a binary classification problem:

LabelMeaningSampling ratio
D=1D = 1The word came from real data Pdh(w)P_d^h(w)1k+1\frac{1}{k+1}
D=0D = 0The word came from noise Pn(w)P_n(w)kk+1\frac{k}{k+1}

where kk is the theoretical ratio of noise samples to real samples.

The derivation proceeds through Bayes’ rule. The joint probability of drawing a word ww with label D=1D=1 is Ph(D=1,w)=1k+1Pdh(w)P^h(D=1, w) = \frac{1}{k+1} P_d^h(w): first pick the “real data” source (probability 1/(k+1)1/(k+1)), then sample ww from the data distribution. Similarly, Ph(D=0,w)=kk+1Pn(w)P^h(D=0, w) = \frac{k}{k+1} P_n(w). The marginal probability of seeing word ww at all (from either source) is ph(w)=Ph(D=1,w)+Ph(D=0,w)=1k+1[Pdh(w)+kPn(w)]p^h(w) = P^h(D=1, w) + P^h(D=0, w) = \frac{1}{k+1}[P_d^h(w) + kP_n(w)].

Given a word ww, what is the posterior probability that it came from real data?

Ph(D=1w,θ)=Ph(D=1,w)ph(w)=1k+1Pdh(w)1k+1[Pdh(w)+kPn(w)]=Pdh(w)Pdh(w)+kPn(w)P^h(D=1 \mid w, \theta) = \frac{P^h(D=1, w)}{p^h(w)} = \frac{\frac{1}{k+1} P_d^h(w)}{\frac{1}{k+1}[P_d^h(w) + kP_n(w)]} = \frac{P_d^h(w)}{P_d^h(w) + kP_n(w)}

We do not know Pdh(w)P_d^h(w) — that is the distribution we want to learn. So we substitute the model’s prediction Pθh(w)P_\theta^h(w) in its place:

Ph(D=1w,θ)=Pθh(w)Pθh(w)+kPn(w)P^h(D=1 \mid w, \theta) = \frac{P_\theta^h(w)}{P_\theta^h(w) + kP_n(w)}

Ph(D=0w,θ)=kPn(w)Pθh(w)+kPn(w)P^h(D=0 \mid w, \theta) = \frac{kP_n(w)}{P_\theta^h(w) + kP_n(w)}

The intuition: when Pθh(w)P_\theta^h(w) dominates kPn(w)kP_n(w), the model believes this word is real; when the noise term dominates, the model leans toward fake. NCE trains the model to make Pθh(w)P_\theta^h(w) converge to the true Pdh(w)P_d^h(w) through this proxy classification task.

To turn this into a loss function, start from the standard binary log loss J(θ)=E(x,y)Pd[ylogσ(hθ(x))+(1y)log(1σ(hθ(x)))]J(\theta) = \mathbb{E}_{(x,y) \sim P_d}[y\log\sigma(h_\theta(x)) + (1-y)\log(1-\sigma(h_\theta(x)))]. NCE substitutes Ph(Dw,θ)P^h(D|w,\theta) as the classifier’s output and DD as the label: when D=1D=1, the loss is logPh(D=1w,θ)-\log P^h(D=1|w,\theta); when D=0D=0, the loss is logPh(D=0w,θ)-\log P^h(D=0|w,\theta). The total loss weights every sample according to its source.

Expanding the expectation over the full mixture distribution ph(w)p^h(w) and substituting the two conditional probabilities:

Jh(θ)=EwPdh[logPθh(w)Pθh(w)+kPn(w)]+kEwPn[logkPn(w)Pθh(w)+kPn(w)]J^h(\theta) = \mathbb{E}_{w \sim P_d^h}\Big[\log \frac{P_\theta^h(w)}{P_\theta^h(w) + kP_n(w)}\Big] + k \cdot \mathbb{E}_{w \sim P_n}\Big[\log \frac{kP_n(w)}{P_\theta^h(w) + kP_n(w)}\Big]

The constant factor 1/(k+1)1/(k+1) is dropped (it does not affect optimization). The first term encourages the model to assign high scores to real words; the second term, weighted by kk because there are kk times as many noise samples, encourages the model to assign low scores to noise words. Together they form a weighted binary classification log loss.

3. The Critical Result: NCE Approaches MLE When kk \to \infty#

In NCE we do not compute ZθhZ_\theta^h explicitly. Instead we introduce a learnable parameter chc^h as a per-context scaling factor: Pθh(w)=Pθ0h(w)exp(ch)P_\theta^h(w) = P_{\theta^0}^h(w) \cdot \exp(c^h), where Pθ0h(w)=exp(sθ0(w,h))P_{\theta^0}^h(w) = \exp(s_{\theta^0}(w, h)) is the unnormalized score from the neural network θ0\theta^0, and exp(ch)\exp(c^h) approximates the normalization constant. The full parameter set is θ={θ0,ch}\theta = \{\theta^0, c^h\}.

And yet, the most remarkable property of NCE is that in practice you can simply fix exp(ch)=1\exp(c^h) = 1 and not learn it at all. The model will learn to self-normalize: the score magnitudes will automatically adjust so that wexp(sθ(w,h))1\sum_w \exp(s_\theta(w,h)) \approx 1. This sounds like it should not work. If we are not learning the normalization factor, what stops the network from pushing all scores to infinity?

The answer is in the structure of the binary classification loss. Suppose the network goes haywire and assigns enormous Pθh(w)P_\theta^h(w) to every word, real or fake. In every term’s denominator Pθh(w)+kPn(w)P_\theta^h(w) + kP_n(w), the PθhP_\theta^h term dominates and the noise term kPnkP_n is washed out. For noise words, the loss becomes logkPnPθh+kPnlogtinyhuge\log\frac{kP_n}{P_\theta^h + kP_n} \approx \log\frac{\text{tiny}}{\text{huge}} \to -\infty: the loss explodes. For real words, the loss becomes logPθhPθh+kPnloghugehugelog1=0\log\frac{P_\theta^h}{P_\theta^h + kP_n} \approx \log\frac{\text{huge}}{\text{huge}} \approx \log 1 = 0: loss is zero, which cannot offset the explosion from the noise terms. Conversely, if the network pushes all scores toward zero, the real-word loss explodes while the noise-word loss sits at zero. Either extreme is punished. The only equilibrium is wexp(sθ)1\sum_w \exp(s_\theta) \approx 1. The binary classification loss is a natural braking mechanism: push too high and the noise terms penalize you; push too low and the real terms penalize you. The math itself enforces normalization, no explicit partition function required.

Gradient Derivation#

Computing θJh(θ)\frac{\partial}{\partial\theta} J^h(\theta) gives the core insight. Using Leibniz rule to move the derivative inside the expectations:

Jhθ=EwPdh[θlogPθhPθh+kPn]+kEwPn[θlogkPnPθh+kPn]\frac{\partial J^h}{\partial\theta} = \mathbb{E}_{w \sim P_d^h}\Big[\frac{\partial}{\partial\theta} \log \frac{P_\theta^h}{P_\theta^h + kP_n}\Big] + k \cdot \mathbb{E}_{w \sim P_n}\Big[\frac{\partial}{\partial\theta} \log \frac{kP_n}{P_\theta^h + kP_n}\Big]

For the first term, instead of attacking the fraction directly, rewrite it to avoid the quotient rule:

θlogPθhPθh+kPn=θlog(1+kPnPθh)=11+kPnPθh(kPn(Pθh)2Pθhθ)\frac{\partial}{\partial\theta} \log \frac{P_\theta^h}{P_\theta^h + kP_n} = -\frac{\partial}{\partial\theta} \log\Big(1 + \frac{kP_n}{P_\theta^h}\Big) = -\frac{1}{1 + \frac{kP_n}{P_\theta^h}} \cdot \Big(-\frac{kP_n}{(P_\theta^h)^2} \cdot \frac{\partial P_\theta^h}{\partial\theta}\Big)

After algebraic simplification: =kPnPθh+kPn1PθhPθhθ=kPnPθh+kPnθlogPθh= \frac{kP_n}{P_\theta^h + kP_n} \cdot \frac{1}{P_\theta^h} \cdot \frac{\partial P_\theta^h}{\partial\theta} = \frac{kP_n}{P_\theta^h + kP_n} \cdot \frac{\partial}{\partial\theta} \log P_\theta^h.

The second term follows similarly: θlogkPnPθh+kPn=θlog(1+PθhkPn)=PθhPθh+kPnθlogPθh\frac{\partial}{\partial\theta} \log \frac{kP_n}{P_\theta^h + kP_n} = -\frac{\partial}{\partial\theta} \log(1 + \frac{P_\theta^h}{kP_n}) = -\frac{P_\theta^h}{P_\theta^h + kP_n} \cdot \frac{\partial}{\partial\theta} \log P_\theta^h.

Combining both expectations:

Jhθ=EwPdh[kPnPθh+kPnlogPθhθ]kEwPn[PθhPθh+kPnlogPθhθ]\frac{\partial J^h}{\partial\theta} = \mathbb{E}_{w \sim P_d^h}\Big[\frac{kP_n}{P_\theta^h + kP_n} \cdot \frac{\partial \log P_\theta^h}{\partial\theta}\Big] - k \cdot \mathbb{E}_{w \sim P_n}\Big[\frac{P_\theta^h}{P_\theta^h + kP_n} \cdot \frac{\partial \log P_\theta^h}{\partial\theta}\Big]

Writing these expectations as discrete sums reveals the structure more clearly:

Jhθ=wkPnPθh+kPn(Pdh(w)Pθh(w))logPθhθ\frac{\partial J^h}{\partial\theta} = \sum_w \frac{kP_n}{P_\theta^h + kP_n} \cdot \big(P_d^h(w) - P_\theta^h(w)\big) \cdot \frac{\partial \log P_\theta^h}{\partial\theta}

Now take the derivative with respect to θ0\theta^0 (the network parameters, excluding the normalization parameter chc^h). Since logPθh(w)=logPθ0h(w)+ch\log P_\theta^h(w) = \log P_{\theta^0}^h(w) + c^h and chc^h does not depend on θ0\theta^0, the logPθhθ0\frac{\partial \log P_\theta^h}{\partial\theta^0} term simplifies to logPθ0h(w)θ0\frac{\partial \log P_{\theta^0}^h(w)}{\partial\theta^0}:

Jhθ0=wkPnPθh+kPn(Pdh(w)Pθh(w))logPθ0h(w)θ0\frac{\partial J^h}{\partial\theta^0} = \sum_w \frac{kP_n}{P_\theta^h + kP_n} \cdot \big(P_d^h(w) - P_\theta^h(w)\big) \cdot \frac{\partial \log P_{\theta^0}^h(w)}{\partial\theta^0}

Now the critical step: take kk \to \infty. The weighting factor kPnPθh+kPn1\frac{kP_n}{P_\theta^h + kP_n} \to 1 because kPnkP_n dominates PθhP_\theta^h in both numerator and denominator. The gradient becomes:

Jhθ0kw(Pdh(w)Pθh(w))logPθ0h(w)θ0\frac{\partial J^h}{\partial\theta^0} \xrightarrow{k \to \infty} \sum_w \big(P_d^h(w) - P_\theta^h(w)\big) \cdot \frac{\partial \log P_{\theta^0}^h(w)}{\partial\theta^0}

This is exactly the MLE gradient. Recall from Section 1: the MLE gradient was w(PdhPθh)sθθ\sum_w (P_d^h - P_\theta^h) \cdot \frac{\partial s_\theta}{\partial\theta}, and here logPθ0hθ0=sθ0θ0\frac{\partial \log P_{\theta^0}^h}{\partial\theta^0} = \frac{\partial s_{\theta^0}}{\partial\theta^0}. The two expressions are identical.

So NCE gradientkMLE gradient\text{NCE gradient} \xrightarrow{k \to \infty} \text{MLE gradient}. The crucial difference: in MLE, the sum over ww runs over the entire vocabulary (because Pθh(w)P_\theta^h(w) is the full softmax). In NCE, the sum only runs over the positive sample plus kk noise samples. The complexity drops from O(V)O(|V|) to O(k)O(k).

4. NCE in Practice: Sigmoid + Log Loss#

The posterior probability Ph(D=1w,θ)=Pθh(w)Pθh(w)+kPn(w)P^h(D=1 \mid w, \theta) = \frac{P_\theta^h(w)}{P_\theta^h(w) + kP_n(w)} can be rewritten in a much more practical form. Divide numerator and denominator by Pθh(w)P_\theta^h(w) to get 11+kPn(w)Pθh(w)\frac{1}{1 + \frac{kP_n(w)}{P_\theta^h(w)}}, then rewrite the fraction inside using exp(log)\exp(\log \cdot): 11+exp(logkPn(w)Pθh(w))=11+exp(log(kPn(w))logPθh(w))\frac{1}{1 + \exp(\log \frac{kP_n(w)}{P_\theta^h(w)})} = \frac{1}{1 + \exp(\log(kP_n(w)) - \log P_\theta^h(w))}. Pulling out a negative sign gives 11+exp([logPθh(w)log(kPn(w))])\frac{1}{1 + \exp(-[\log P_\theta^h(w) - \log(kP_n(w))])}, which is exactly the sigmoid function σ(x)=11+ex\sigma(x) = \frac{1}{1 + e^{-x}} with input x=logPθh(w)log(kPn(w))x = \log P_\theta^h(w) - \log(kP_n(w)).

In practice we set exp(ch)=1\exp(c^h) = 1 (self-normalization), so Pθh(w)=exp(sθ0(w,h))P_\theta^h(w) = \exp(s_{\theta^0}(w,h)) and logPθh(w)=sθ0(w,h)\log P_\theta^h(w) = s_{\theta^0}(w,h). This yields:

Ph(D=1w,θ)=σ(sθ0(w,h)log(kPn(w)))P^h(D=1 \mid w, \theta) = \sigma\big(s_{\theta^0}(w, h) - \log(kP_n(w))\big)

Define Δs=sθ0(w,h)log(kPn(w))\Delta s = s_{\theta^0}(w, h) - \log(kP_n(w)). This quantity asks a natural question: how much higher is the model’s score for this word compared to its expected score if it were random noise? The larger the gap, the more likely the word is real.

The empirical loss function (following the original paper’s recommendation of m=1m=1 real sample and n=kn=k noise samples) becomes:

Jh^(θ)=logσ(sθ(w0,h)log(kPn(w0)))+j=1klog(1σ(sθ(wj,h)log(kPn(wj))))\widehat{J^h}(\theta) = \log \sigma\big(s_\theta(w_0, h) - \log(kP_n(w_0))\big) + \sum_{j=1}^{k} \log\big(1 - \sigma(s_\theta(w_j, h) - \log(kP_n(w_j)))\big)

The computation flow: take one real word w0w_0 (label = 1) and kk noise words (label = 0); for each word compute Δs=sθ(w,h)log(kPn(w))\Delta s = s_\theta(w, h) - \log(kP_n(w)); pass through sigmoid; compute binary cross-entropy; backpropagate.

5. A Common Confusion: kk vs nn#

Nearly every blog post and even TensorFlow’s tf.nn.nce_loss gets this wrong. The two parameters are conceptually distinct.

kk is the theoretical ratio of noise samples to real samples. A larger kk brings NCE closer to MLE in the limit. nn is the actual number of noise samples drawn from PnP_n for Monte Carlo estimation of the noise expectation. A larger nn gives a more accurate estimate of the expectation. Confusing them does not break anything in practice when nn is large enough, but conceptually they are independent design choices.

6. Limitations#

NCE only speeds up training, not inference. At test time you still need to enumerate the vocabulary if you want to generate the next word — NCE does not bypass the partition function at inference. The noise distribution Pn(w)P_n(w) matters: if it is too far from the true data distribution, the classifier never sees difficult negative examples and the learned model degrades. And NCE does not guarantee a properly normalized probability distribution (though self-normalization works well in practice, the output is not a strict probability).

7. From NCE to InfoNCE#

NCE is the mathematical foundation of InfoNCE loss. InfoNCE generalizes the idea from language modeling to representation learning, making essentially one change: instead of k+1k+1 independent binary classifications, it poses a single (k+1)(k+1)-way classification.

In NCE, each negative sample is judged independently: “are you real or fake?” In InfoNCE, the question becomes “among these k+1k+1 candidates, which one is the real positive?” The negatives compete with each other, and only one can win.

NCE’s loss for a positive sample plus kk negatives: Jh=logσ(Δs0)+j=1klog(1σ(Δsj))J^h = \log\sigma(\Delta s_0) + \sum_{j=1}^k \log(1-\sigma(\Delta s_j)). InfoNCE transforms this into a softmax over all k+1k+1 samples: LInfoNCE=logexp(sθ(x,x+))exp(sθ(x,x+))+j=1kexp(sθ(x,xj))\mathcal{L}_{\text{InfoNCE}} = -\log \frac{\exp(s_\theta(x, x^+))}{\exp(s_\theta(x, x^+)) + \sum_{j=1}^{k} \exp(s_\theta(x, x_j^-))}.

NCEInfoNCE
ProblemLanguage modeling (predict the next word)Representation learning (classify positive vs. negative pairs)
Positive(h,w)(h, w) from training dataA positive pair (x,x+)(x, x^+)
NegativesSampled from Pn(w)P_n(w)Other samples in the batch or a memory bank
Classificationk+1k+1 independent binary (Sigmoid)One (k+1)(k+1)-way classification (Softmax)
NormalizationNot needed (each Sigmoid is independent)Required — denominator sums j=0kexp(sj)\sum_{j=0}^k \exp(s_j)
Negative relationshipIndependentCompetitive

Why “Info” NCE: From Loss to Mutual Information#

This is InfoNCE’s deepest theoretical contribution. Many sources give the conclusion without the derivation; here is the full chain.

Start with the InfoNCE loss: LN=EX[logfθ(x,x+)j=1Nfθ(x,xj)]\mathcal{L}_N = -\mathbb{E}_X [\log \frac{f_\theta(x, x^+)}{\sum_{j=1}^{N} f_\theta(x, x_j)}], where x+x^+ is the positive sample for xx and x2,...,xNx_2, ..., x_N are N1N-1 negatives drawn independently from the marginal p(x+)p(x^+). The scoring function fθf_\theta is what the model learns — for instance, exp(sim/τ)\exp(\text{sim}/\tau) in SimCLR.

At optimality, the scoring function converges to a density ratio: f(x,x+)p(x+x)p(x+)f^*(x, x^+) \propto \frac{p(x^+ \mid x)}{p(x^+)}. The model’s score estimates how much more likely x+x^+ is to appear when paired with xx, compared to appearing randomly in the dataset. A high ratio means a strong association.

Substituting this optimal form back into the loss and noting that negative samples are drawn independently from p(x+)p(x^+) (so p(xjx)p(xj)=p(xj)p(xj)=1\frac{p(x_j \mid x)}{p(x_j)} = \frac{p(x_j)}{p(x_j)} = 1), we get:

LNopt=EX[logp(x+x)p(x+)p(x+x)p(x+)+(N1)]EX[logp(x+x)p(x+)]+logN\mathcal{L}_N^{\text{opt}} = -\mathbb{E}_X \Bigg[ \log \frac{ \frac{p(x^+|x)}{p(x^+)} }{ \frac{p(x^+|x)}{p(x^+)} + (N-1) } \Bigg] \approx -\mathbb{E}_X \Bigg[ \log \frac{p(x^+|x)}{p(x^+)} \Bigg] + \log N

where the approximation holds when NN is large and the density ratio is small relative to NN. Now recall the definition of mutual information: I(x;x+)=E(x,x+)p(x,x+)[logp(x,x+)p(x)p(x+)]=EX[logp(x+x)p(x+)]I(x; x^+) = \mathbb{E}_{(x, x^+) \sim p(x, x^+)}[\log \frac{p(x, x^+)}{p(x)p(x^+)}] = \mathbb{E}_X[\log \frac{p(x^+ \mid x)}{p(x^+)}]. Mutual information measures how much uncertainty about x+x^+ is removed by knowing xx.

This yields LNoptI(x;x+)+logN\mathcal{L}_N^{\text{opt}} \approx -I(x; x^+) + \log N, and since any trained loss is at least the optimal loss, LNlog(N1)I(x;x+)\mathcal{L}_N \geq \log(N-1) - I(x; x^+). Rearranging gives the useful bound:

I(x;x+)log(N1)LNI(x; x^+) \geq \log(N-1) - \mathcal{L}_N

The quantity log(N1)LN\log(N-1) - \mathcal{L}_N is a lower bound on the mutual information II. Minimizing the InfoNCE loss pushes this lower bound upward: the model is forced to preserve more shared information between positive pairs. A large NN tightens the bound (the log(N1)\log(N-1) term grows), which is why contrastive learning craves large numbers of negatives.

The narrative flow is worth tracing in full. InfoNCE sets up a game: among NN candidates, find the real positive. This game has a theoretical ceiling — the best any classifier can do is determined by the mutual information shared between positive pairs. When the model plays well (low loss), its representations retain a lot of mutual information. When it cannot tell positives from negatives (high loss), the representations have discarded most of that shared structure. Minimizing InfoNCE is not an arbitrary trick; it is mathematically equivalent to maximizing a lower bound on mutual information.

In SimCLR, a batch of NN images, each with two augmented views, produces 2N2N representations. For each positive pair (zi,zj)(z_i, z_j), the loss takes the form i,j=logexp(sim(zi,zj)/τ)k=12N1[ki]exp(sim(zi,zk)/τ)\ell_{i,j} = -\log \frac{\exp(\text{sim}(z_i, z_j) / \tau)}{\sum_{k=1}^{2N} \mathbb{1}_{[k \neq i]} \exp(\text{sim}(z_i, z_k) / \tau)}, where sim(zi,zj)\text{sim}(z_i, z_j) is typically cosine similarity and τ\tau is a temperature parameter controlling the sharpness of the distribution. This is InfoNCE (also called NT-Xent loss): the denominator sums over all 2N12N-1 other samples as negatives. InfoNCE has been adopted by SimCLR, MoCo, CPC, BYOL, and essentially all major contrastive learning methods.

Why InfoNCE Outperforms NCE in Representation Learning#

NCE was built for language modeling; InfoNCE was built for representation learning. When compared within the representation learning setting, InfoNCE has several structural advantages.

The key difference is negative competition. In NCE, each negative is judged independently — even a hard negative (one very similar to the positive) gets the same treatment as an easy one: just another “fake” label. In InfoNCE, all negatives share the same denominator. To win the softmax, the positive must not only score highly but must outrank every negative. A strong hard negative steals probability mass from the positive, producing a stronger gradient.

The gradient itself reveals this property. For each negative xjx_j^-, Ls(x,xj)=exp(s(x,xj)/τ)kexp(s(x,xk)/τ)\frac{\partial \mathcal{L}}{\partial s(x, x_j^-)} = \frac{\exp(s(x, x_j^-) / \tau)}{\sum_{k} \exp(s(x, x_k^-) / \tau)}. The penalty on a negative is proportional to the probability the model mistakenly assigns to it being the positive. The more a negative resembles the positive, the higher its softmax probability, and the harder the gradient hits it. The loss is doing automatic hard negative mining — no manual filtering needed. NCE’s sigmoid has no equivalent mechanism: all negatives receive equal gradient weight regardless of difficulty.

InfoNCE also removes the need for an external noise distribution. NCE requires choosing Pn(w)P_n(w) by hand; a poor choice degrades performance. InfoNCE uses the batch itself as the negative pool, with zero overhead. MoCo extends this further with a momentum encoder maintaining a large negative queue for when the batch alone is too small.

The temperature parameter τ\tau in the softmax provides a tunable difficulty knob: τ0\tau \to 0 concentrates all gradient signal on the hardest negative; τ\tau \to \infty treats all negatives equally. SimCLR’s τ=0.1\tau = 0.1 was found through tuning. NCE’s sigmoid has no comparable control.

And as shown in the derivation above, InfoNCE provides an information-theoretic guarantee: LInfoNCElog(k)I(x;x+)\mathcal{L}_{\text{InfoNCE}} \geq \log(k) - I(x; x^+). Minimizing the loss is mathematically equivalent to maximizing the mutual information between positive pairs while minimizing it for negative pairs. NCE cannot make this claim.

In short, replacing independent sigmoids with a shared softmax denominator triggers a cascade of consequences — negative competition, automatic hard negative mining, mutual information interpretation, and temperature control. NCE’s binary classifier is solving an easier problem, and in representation learning, that produces a less tightly organized representation space.

  • Contrastive Predictive Coding — the paper that proposed InfoNCE and extended NCE to representation learning
  • SimCLR — InfoNCE applied to visual contrastive learning
  • MoCo — momentum queue for scaling negative samples
  • BYOL — contrastive learning without negative samples

References#

  • Lei Mao’s Blog: Noise Contrastive Estimation
  • Gutmann & Hyvarinen (2012). Noise-Contrastive Estimation of Unnormalized Statistical Models
  • Mnih & Teh (2012). A Fast and Simple Algorithm for Training Neural Probabilistic Language Models
  • Ma & Collins (2018). Noise Contrastive Estimation and Negative Sampling for Conditional Models: Consistency and Statistical Efficiency
Noise Contrastive Estimation
https://ru00ys-lab.com/blog/noise-contrastive-estimation
Author Ru00y
Published at June 22, 2026
Series: Contrastive Learning Part 1 of 6

Enjoyed this post? Never miss out on future posts

Loading comments...

Best in the dark?

This site looks its best in dark mode. Give it a try!