From counts to vectors: the neural probabilistic language model
Bengio's neural language model replaced count tables with learned word vectors and a small network. See how it predicts, why shared vectors generalize to unseen sequences, and what its full softmax costs.
What runs in CodeSplash
- LNG002Neural probabilistic language model (Bengio) Explained here
You will be able to
- Trace one prediction of a neural language model from word vectors to a softmax
- Explain why shared word vectors generalize where counts can't
- Say why the softmax over the whole vocabulary is the expensive part
The problem with counting
ReadAn n-gram model learns nothing from "the cat sat on the mat" about "the dog sat on the rug". Every word is a separate row in its table, so similar words share nothing, and longer histories need exponentially many counts.
The neural probabilistic language modelNeural probabilistic language modelPredicts the next word from the learned vectors of the previous few words, through a hidden layer and a softmax over the vocabulary. (Bengio and colleagues, 2003) fixed both problems at once. Each word gets a learned vector. A small network reads the vectors of the previous few words and outputs a probability for every word in the vocabulary.
One prediction, step by step
ReadLook up the vectors of the previous words, join them end to end, pass them through a tanh layer, then a softmaxSoftmaxTurns a vector of scores into probabilities: exponentiate each, divide by the total. Subtracting the max first keeps it stable. over the vocabulary. Training minimizes the negative log-probability of the word that actually came.
The reference program pushes a two-number input through a tanh layer and a three-word softmax. The probabilities are 0.408, 0.282 and 0.310; if the first word is the true one, the loss is 0.896. The weights are made up, so the numbers check arithmetic, not a model.
The expensive part
ReadThe final softmax touches every word in the vocabulary for every prediction: tens of thousands of dot products, only to normalize.
Much of what followed is about this cost. Word2vec replaced the full softmax with a few sampled "noise" words. Recurrent networks replaced the fixed window with a state carried along the text. Transformers replaced the state with attention over the whole context. All of them kept the core idea: words as learned vectors, and a network that predicts what comes next.
Where it stands in CodeSplash
Read| Entry | In CodeSplash |
|---|---|
| Neural probabilistic language model (LNG002) | Explained here; its descendants run in Word vectors, Text encoder and the GPT-2 scorer |
Check: neural language models
CheckpointCheck yourself
What does Bengio's model feed its hidden layer?
Show the answer
Answer: a
Each previous word is looked up in an embedding table, and the vectors are concatenated. The window is still fixed, like an n-gram's, but the inputs are learned vectors instead of table rows.
Check yourself
Why can the neural model score a word sequence it never saw better than a smoothed n-gram model?
Show the answer
Answer: b
Smoothing spreads probability evenly over unseen pairs; shared vectors spread it toward plausible ones.
Check yourself
Which step dominates the cost of each prediction in the original model, with a vocabulary of 50,000 words?
Show the answer
Answer: c
Normalizing needs a score for every word. Sampled objectives such as negative sampling, used by word2vec, avoid computing all of them during training.
Recap
Recap- The neural language model looks up vectors for the previous words, passes them through a tanh layer and a softmax over the vocabulary.
- Shared, learned vectors let evidence flow between similar words, which counts can't do.
- The full softmax is costly; word2vec, RNNs and Transformers each answer part of that problem.
Concepts in this lesson