Skip to content

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.

Applied ~11 min7 steps

What runs in CodeSplash

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

Read first

01

The problem with counting

Read

An 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.

02

One prediction, step by step

Read
x=[ e(wt−n+1); …; e(wt−1) ],h=tanh⁡(Hx+d),p(wt∣⋅)=softmax(Uh+b)x = [\,e(w_{t-n+1});\,\ldots;\,e(w_{t-1})\,], \quad h = \tanh(Hx + d), \quad p(w_t \mid \cdot) = \mathrm{softmax}(Uh + b)

Look 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.

03

Why vectors generalize

Predict

Predict first

Training text often has "the cat sat" and "a dog ran", never "the dog sat". After training, what does the neural model do with "the dog sat"?

It has no information about the new sentenceIt gives it a reasonable probability, because similar words have similar vectorsIt refuses to score it
Reveal

It gives it a reasonable probability. "cat" and "dog" appear in similar contexts, so training pushes their vectors close together, and the network's output for one carries over to the other. A count table would have nothing to go on.

04

The expensive part

Read

The 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.

05

Where it stands in CodeSplash

Read
06

Check: neural language models

Checkpoint

Check 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.

07

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