Sub topic 11
Subsampling Frequent Words
Assume you know tokenization, relative word frequency, skip-gram and CBOW, and the basic idea of gradient descent. This chapter explains why word2vec removes some frequent tokens before creating training examples.
Why frequent words are a problem
Words such as “the”, “of”, and “and” appear in almost every document. They create many training pairs, but those pairs often provide little information about a word’s meaning. For example, learning that “volcano” occurs near “the” is less useful than learning that it occurs near “lava”.
Frequent words also dominate computation. If a corpus contains millions of occurrences of “the”, processing every occurrence spends much of the training budget on nearly identical, low-information examples.
Word2vec uses subsampling, which randomly removes some occurrences of very frequent words. The word remains in the vocabulary, but individual tokens may be dropped from a training sentence.
The keep probability
Let f(w) be the word’s relative frequency in the corpus, and let t be a small threshold such as 1e-5. A common word2vec keep probability is:
p_{\text{keep}}(w)
=
\min\left(1,\left(\sqrt{\frac{f(w)}{t}}+1\right)\frac{t}{f(w)}\right)The word is retained with probability \(p_{\text{keep}}\). Otherwise, it is discarded for that training pass.
For a word with f(w) = 0.01 and t = 1e-5:
p_{\text{keep}} \approx \sqrt{10^{-3}} + 10^{-3} \approx 0.0326Only about 3.3% of its occurrences are retained. For a rare word whose frequency is below the threshold, the probability is capped at 1.
import math
import random
def keep_probability(frequency, threshold=1e-5):
if frequency <= 0:
raise ValueError("frequency must be positive")
probability = (math.sqrt(frequency / threshold) + 1) * (
threshold / frequency
)
return min(1.0, probability)
random.seed(7)
frequency = 0.01
p = keep_probability(frequency)
kept = sum(random.random() < p for _ in range(10000))
print(f"keep probability: {p:.4f}")
print(f"observed fraction: {kept / 10000:.4f}")Effect on context windows
Subsampling usually happens before the model creates context pairs. Removing a token can therefore change which words fall inside a window. This reduces both the number of examples and the number of uninformative contexts.
Choosing the threshold
A smaller threshold removes more frequent-word occurrences and reduces training cost further. An excessively aggressive threshold can remove useful grammatical signals or leave too little context in a small corpus. Compare validation results and training speed rather than assuming the default is optimal.
Exercise
Why does subsampling usually help word2vec even though it throws away training tokens?
Show answer
It removes many redundant examples dominated by frequent function words, allowing the model to spend computation on more informative contexts.