Sub topic 8
The Cost of the Full Softmax
Prerequisites
This chapter assumes you know vectors, dot products, norms, conditional probability, softmax, gradient descent, and basic tokenization. We focus on why the original word2vec training objective becomes expensive for a large vocabulary.
From a training pair to a prediction
Suppose a word2vec model receives a center word such as cat and must predict a context word such as mat. The model turns cat into a hidden vector h. For every vocabulary word, it computes a score:
score(w) = u_w · h
Here, u_w is the output vector for word w. If the vocabulary has V words, the model computes V scores. It then applies softmax:
P(w | cat) = exp(score(w)) / sum(exp(score(v)))
The denominator sums over every vocabulary word v. Training uses cross-entropy loss, which penalizes the model when the correct context word receives a low probability.
The model must update the output vector for every word, not only the correct word. Increasing the correct word's probability depends on comparing it with all other candidates.
Why the full softmax is expensive
Let d be the embedding dimension. Computing one dot product costs about d operations. Computing all vocabulary scores therefore costs about V × d operations for one training pair.
The normalization and gradient also involve all V words. The output weight matrix contains V × d values, so it can require substantial memory as well.
| Component | Approximate cost for one pair |
|---|---|
| Score every vocabulary word | O(Vd) |
| Softmax normalization | O(V) |
| Output-gradient updates | O(Vd) |
| Output weight storage | O(Vd) |
For a vocabulary of 1,000,000 words and vectors of 300 dimensions, one prediction touches roughly 300 million score components before accounting for other work. A corpus supplies millions or billions of training pairs, so this repeated calculation dominates training time.
The following small example shows the full calculation. It is practical for a tiny vocabulary, but the same approach does not scale well.
import numpy as np
h = np.array([0.8, -0.2])
output_vectors = np.array([
[0.5, 0.1], # "cat"
[0.2, 0.7], # "mat"
[-0.3, 0.4], # "car"
])
target_index = 1
scores = output_vectors @ h
scores -= scores.max() # improves numerical stability
probabilities = np.exp(scores)
probabilities /= probabilities.sum()
loss = -np.log(probabilities[target_index])
print("probabilities:", probabilities)
print("loss:", loss)What replaces the full calculation?
Word2vec commonly avoids the full softmax with negative sampling. Instead of scoring every vocabulary word, it scores the observed pair as a positive example and samples a small number of incorrect words as negative examples. The cost becomes roughly O((k + 1)d), where k is the number of negative samples.
Hierarchical softmax is another option. It stores vocabulary words in a binary tree and predicts a word through a path of about log2(V) decisions. This reduces the number of operations, but it has different implementation and quality trade-offs.
Exercise
If the vocabulary grows from 50,000 to 500,000 words while the embedding size stays fixed, what happens to the approximate full-softmax work per training pair?
Show answer
It grows by about 10 times because the dominant O(Vd) term is linear in the vocabulary size.