Techniqueadvanced
Noise-contrastive estimation
Also called NCE.
Fit an unnormalized model by logistic regression that tells data samples from noise samples. It avoids the full softmax over a large vocabulary.
Noise-contrastive estimation (NCE) fits a model whose probabilities are known only up to a normalizing constant. A language model with a softmax over a vocabulary of words is such a model: the score of one word is cheap, but its probability needs the sum of exponentials, the partition function, at every training step. NCE turns the fit into a binary classification. Mix the training data with times as many samples from a known noise distribution, and train the model, by logistic regression, to tell which samples are data. Gutmann and Hyvärinen (2010, 2012) proved that the best classifier gives the data density itself, normalized, with no sum over the space. Mnih and Teh (2012) used it to train neural language models over 10 times faster with the same perplexity.
In plain words
You want a model that scores words, but turning scores into chances needs a sum over every word, which is slow.
So play a different game. Take a real next word from the text, and also draw a few fake words at random from a simple known list of chances, such as how common each word is. Ask the model to tell the real word from the fakes. It does this by comparing its own score of each word with how likely the word is as a fake.
A model that plays this game as well as possible must give each word its true chance in the text. And the game needs only the few words in hand, not the whole vocabulary.
When it applies
Use it when a model has a cheap unnormalized score and an expensive normalizer, and you can sample from a noise distribution whose density you can evaluate. The noise must be positive wherever the data density is. The main uses are language models with a large output vocabulary, where the normalizer is a sum over words for each context, and models of continuous data whose normalizer is an integral with no closed form. When the output space is small, the plain softmax with maximum likelihood is simpler and as fast.
The template
Let be a data sample, from the unknown density , and let noise samples be drawn for each data sample from . The model is , an unnormalized log density; it may include a learned constant in place of the normalizer, . The posterior probability that a sample came from the data, if the model were right, is
with the logistic function . Gutmann and Hyvärinen write with . NCE maximizes the log-likelihood of the right labels,
NCE-TRAIN(score s_θ, data, noise p_n, k, rate η)- 1
repeat until the held-out loss stops falling: - 2
draw a data example u_0, with its context if the model has one - 3
draw k noise samples u_1, ..., u_k from p_n ▷ hole: the noise distribution, positive where the data is - 4
Δ_j ← s_θ(u_j) - ln(k · p_n(u_j)) for j = 0, ..., k ▷ hole: the model score, with or without a learned constant - 5
J ← ln σ(Δ_0) + Σ_{j=1..k} ln(1 - σ(Δ_j)) - 6
θ ← θ + η ∇_θ J ▷ hole: the optimizer
Line L4 is the log odds of "data" against "noise" for each sample. Line L5 needs scores and no sum over the space. The gradient of line L5 is
Why the optimum is the data density. Treat as free at each point, and write . The objective at is
Its derivative in is , which is positive for and negative for when . So the unique maximum is : the best model is the data density, and it is normalized although nothing forced it to be. This is Theorem 1 of Gutmann and Hyvärinen (2012). For a parametric family that contains , they prove that the estimate is consistent and asymptotically normal (Theorems 2 and 3).
A small check. Let on four symbols, uniform and . Gradient ascent on the exact objective with four free values ends at , which sums to 1. Stochastic steps as in the template, with 200,000 data samples and seed 1, end near , with sum 1.001.
The cost. For a language model with a context of words of dimension , Mnih and Teh count about operations per word for the softmax and for NCE, so the speedup is . With , , and this is about 45.
Classic examples
- Neural probabilistic language models. Mnih and Teh trained log-bilinear language models with NCE, with a unigram noise distribution, and fixed the normalizer of every context to 1 instead of learning one constant per context; this did not hurt. On the Penn Treebank, maximum likelihood gave a test perplexity of 163.5 in 21 hours. NCE gave 192.5, 172.6, 163.1 and 159.1 with , 5, 25 and 100, in about 1.5 hours each, about 14 times faster. Uniform noise was much worse than unigram noise. They also trained models on the 47 million word, 80,000 word vocabulary data of the MSR Sentence Completion Challenge.
- Negative sampling for the skip-gram model. Mikolov and colleagues (2013) simplified NCE for word vectors: they use in place of . The objective no longer gives probabilities, but it is enough to learn good word embeddings.
- Unnormalized density models. Gutmann and Hyvärinen applied NCE to models of natural images, such as independent component analysis models and their extensions, where the normalizer is an integral with no closed form; the normalizing constant is one more parameter.
- Recurrent language models. The output softmax of a recurrent model has the same cost, and NCE with a unigram noise distribution trains it with output scores per word.
Variations
- The noise distribution. The unigram distribution is the usual choice for words; a noise closer to the data gives a harder task and a better estimate per sample. Uniform noise is easy to sample but needs many more samples.
- The number of noise samples. As , the covariance of the estimate no longer depends on (Corollary 5 of Gutmann and Hyvärinen), and for a normalized model the estimate tends to the efficiency of maximum likelihood (Corollary 6). With the covariance is times that of maximum likelihood (Corollary 7): 2 times for and about 1.1 times for .
- The normalizer. Learn one constant for a single model, as Gutmann and Hyvärinen do. For a conditional model with millions of contexts, fix it to 0, as Mnih and Teh do; the model learns to be nearly self-normalized.
- Negative sampling. Dropping from line L4 gives the objective of word2vec. Its optimum is , a scaled ratio of densities, not .
- Importance sampling. Bengio and Senécal estimated the gradient of the normalizer from samples, which needs the samples to be weighted and was found to be unstable by Mnih and Teh. NCE does not estimate the normalizer at all.
- Ranking forms. A softmax over the data sample and the noise samples, which picks the data sample among , is the basis of later contrastive objectives. It is a different loss with a different optimum.
- Contrastive divergence. It also avoids the normalizer, but samples from the model by a short Markov chain instead of from a fixed noise distribution.
Pitfalls
- Noise with gaps. If where , the model is never told the right value there, and Theorem 1 fails. Give the noise full support, for example by smoothing the unigram.
- Too few noise samples. With the language model of Mnih and Teh was much worse than maximum likelihood (192.5 against 163.5). Choose on held-out data; 25 already matched it.
- Reporting perplexity without normalizing. The trained model is only close to normalized. For a test perplexity, normalize each distribution over explicitly, which is affordable at test time.
- Mixing up NCE and negative sampling. Negative sampling has no term and does not estimate probabilities. Do not use its scores as a language model.
- The wrong posterior. The factor belongs in line L4. With alone, the optimum is , which is off by a constant factor.
Exercises
- Show that the maximum of over unnormalized densities is when wherever . Hint: the derivative at each point is given in the template.
- Show that the negative sampling objective is maximized at . Answer: the same computation with replaced by 1 gives terms and the stationary point .
- Show that for a fixed context, as , the expected NCE gradient tends to the gradient of the log-likelihood of a normalized model. Hint: for the data sample, and .
- With , how large must be for the covariance to be within 5 percent of that of maximum likelihood? Answer: , so .
References
- Gutmann and Hyvärinen 2010, the estimation principle, with the normalizing constant as a parameter
- Gutmann and Hyvärinen 2012, Section 2.1 (the objective, equations 3 to 7, with r_nu(u) = 1/(1 + nu exp(-u))), Section 2.2 (unnormalized models, ln p_m = ln p_m^0 + c), Section 2.3 (Theorem 1, the nonparametric optimum; Theorems 2 and 3, consistency and asymptotic normality), Section 2.4 (Corollaries 5 to 7 on the variance as nu grows and for p_n = p_d)
- Mnih and Teh 2012, Section 3 (NCE for the log-bilinear language model; Section 3.1, the normalizers fixed to 1; Section 3.2, the speedup (cd + V)/(cd + k)), Tables 1 and 2 (Penn Treebank results for k = 1 to 100 and for unigram against uniform noise), and the MSR Sentence Completion Challenge
- Mikolov, Sutskever, Chen, Corrado and Dean 2013, negative sampling, introduced as a simplification of NCE for the skip-gram model
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag CAP0E5 Edit this page History Report an error Markdown