1. What if the count table has no match?
We now have two building blocks: N-gram models, which estimate the next token from counts, and word vectors, which give words numeric representations. This lesson brings them together with a neural network.
First, recall the difficulty with an exact count table. Suppose a vocabulary represents “black hole” as a single item, and we want to continue:
“Scientists discovered an object orbiting a black hole. The object is a ___.”
For the miniature calculation below, we simplify the context to the two vocabulary items “around” + “black hole”. A trigram uses two context tokens and one predicted token. If this pair has never been observed with a continuation, its unsmoothed conditional estimate has no denominator.
Backoff can consult shorter contexts; smoothing and interpolation can supply probabilities from other evidence. These are useful methods, not necessarily random guesses. But a basic symbol-based count table does not automatically connect “black hole” with “star”. Evidence after “around star” is stored under a different key.
A neural model can instead share parameters across contexts. If related words have useful numeric representations, a new context can resemble an observed one even when the exact symbols differ.
2. Look up vectors, concatenate them, predict a word
A feedforward neural language model uses a fixed number of recent tokens. Let that context length be ; unlike N-gram order , counts only the input tokens. Matching an N-gram's window gives .
The basic procedure is:
- Look up each context token's embedding in a shared table.
- Concatenate those vectors in their original order.
- Apply a feedforward network and output a probability for each candidate next token.
If each embedding has coordinates, the concatenated input has coordinates:
Concatenation means joining the vectors end to end, not adding or averaging them. Each position has its own block in the input, so reversing two tokens generally changes the prediction.
The network uses familiar parts
For a hidden layer of width and a vocabulary of size , a simple architecture is:
These are the affine layers, nonlinear activations and Softmax from Chapter II. With observed next token , the training loss is:
Backpropagation computes gradients for the network weights and the looked-up embeddings. The model can learn them jointly; embeddings do not have to come from a separately trained Word2Vec model.
Neural probabilistic language models using learned distributed representations were developed before Word2Vec. A notable 2003 model used this approach to share statistical information across similar word sequences. We are following the same central idea with a simplified architecture, rather than reproducing every detail of that model.
3. Shrink the network until we can calculate it
For this teaching example, use two context tokens, two coordinates per vector, one hidden neuron and just two output candidates: “planet” and “umbrella”.
The embeddings and weights are chosen by hand. The first coordinate of the second token acts as an illustrative astronomy-related feature; this is not a claim that learned embeddings normally have a coordinate with that name.
Concatenate them:
Choose hidden-layer weights and bias . The zeros make this neuron use only the third input coordinate:
Now set the output scores to:
Softmax gives:
So this tiny network assigns about 77.8% to “planet”. It has converted a four-number input into a two-candidate distribution.
A complete forward pass with two context tokens
Context: “around” + “star”
Concatenated input: [0.10, 0.20, 0.90, 0.15]
Pre-activation: 0.5500 → tanh hidden value: 0.5005
Output scores: planet 1.2513 · umbrella 0
Hand-chosen embeddings and weights. “Black hole” is treated as one vocabulary item in this toy example. The first vector and the fourth coordinate have zero weight; there is no training or real astronomy knowledge behind this calculation.
Start with “star” and follow every displayed number. Then choose “black hole”, “rocket” and “banana”. All values are recomputed using the same fixed network; intermediate results are not rounded before the next calculation.
The two candidates make the arithmetic inspectable, not realistic. A model choosing “umbrella” after “banana” is not showing sensible language understanding: it is selecting between the only two available outputs.
4. Replace “star” with “black hole”
Suppose the exact context “around black hole” was absent from training, while both vocabulary items already had useful embeddings. In our hand-built illustration:
Substituting the latter into the same network gives:
The probabilities are about 77.8% versus 75.1%. The inputs are related, and this particular network responds similarly without needing an exact count-table entry for each pair.
| Second token | First coordinate | Hidden value | Planet probability |
|---|---|---|---|
| star | 0.90 | 0.5005 | 77.8% |
| black hole | 0.85 | 0.4422 | 75.1% |
| banana | 0.10 | -0.5717 | 19.3% |
For “banana”, the first coordinate is :
Its different numeric input produces a different prediction.
Why parameter sharing helps
The same weights transform every context. Learning from one sequence can therefore affect predictions for others, rather than merely incrementing an isolated phrase count.
With finite weights, the tanh and Softmax functions are continuous. Nearby inputs can consequently yield nearby outputs, but continuity does not guarantee a useful degree of similarity for every network: large weights can amplify differences. Even similar outputs can both be wrong.
Our chosen parameters demonstrate the mechanism. They do not prove that a trained model understands astronomy or that a specific real embedding will place these words close. Unknown vocabulary items and domain changes still need appropriate handling and evaluation.
5. Compare the models—and the remaining limitation
| Property | N-gram model | Feedforward model |
|---|---|---|
| Representation | Separate token identifiers and counts | Dense embeddings + shared parameters |
| Unseen context | Smoothing, backoff or interpolation | Apply the same network to a new input |
| Storage | Observed N-grams and statistics | Embedding table and network weights |
| Training | Count token windows; estimate probabilities | Optimise a prediction loss with gradients |
| Context | N−1 preceding tokens | K preceding tokens |
The feedforward model stores an embedding table and shared weights rather than only separate continuation counts. This helps address sparsity through generalisation, but does not remove the need for training data or guarantee better predictions in every setting.
Storage is also configuration-dependent. Ignoring optional connections, the simple architecture above has:
parameters: embeddings, hidden weights and biases, then output weights and biases. Increasing , , or increases memory or computation; neural models are not automatically small.
The context window is still fixed
Consider two stories whose last three tokens are both “the dog felt”. One earlier described a dog being rescued; the other described a dog frightened by fireworks.
With , this model receives the same input for both stories. Their earlier information is outside its window, so it must give the same next-token distribution despite the different situations.
A larger window can supply more context, but concatenation also increases the input size. The hidden weight matrix has entries, growing linearly with when the other dimensions stay fixed. No particular window size is a universal requirement.
6. What you have discovered
A feedforward language model combines embeddings with a shared prediction function.
- Look up → concatenate → predict: context vectors of dimension become an input with coordinates.
- Familiar network parts: affine layers, a nonlinear activation, Softmax and cross-entropy form the model and its training objective.
- Joint learning: backpropagation can update both the network parameters and the embedding table.
- Generalisation: related inputs can produce related predictions for combinations not observed exactly in training.
- A fixed window remains: information before the final tokens is not supplied to this model.
The next lesson introduces recurrent neural networks and a hidden state that is updated as the sequence is read.