Negative Sampling › How Many Negative Samples? The Hyperparameter

Michael BrenndoerferApril 4, 20254 min read
Language AI HandbookAll chapters
Part 4 of 67Word Embeddings
Chapter 21Negative Sampling › How Many Negative Samples? The Hyperparameter
Reading section 6 of 15 · How Many Negative Samples? The Hyperparameter · 4 minSection 6 of 15 · 4 min

Book contents

Section 6 of 15 · 4 min read

Negative Sampling › How Many Negative Samples? The HyperparameterOne section · Sections in this chapter6 / 15 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The number of negative samples kk is a central hyperparameter that controls the trade-off between training signal quality and computational cost. More negatives provide stronger gradient information per positive pair but increase the work per training step linearly.

Choosing k

Typical values for kk range from 5 to 20. Smaller values (5 to 10) work well for large datasets where each word pair is seen many times. Larger values (15 to 25) help with smaller datasets or less frequent words. The original Word2Vec paper recommends 5 to 10 for large corpora and 15 to 25 for smaller ones.

The intuition behind these recommendations is straightforward. With a large corpus, each positive word pair appears many times throughout training. This provides ample signal to position that pair correctly. A modest number of negatives is sufficient to push away distractors. With a smaller corpus, each positive pair appears less frequently, so more negatives per step compensate by providing stronger repulsive signal that helps separate unrelated words even with limited data.

In[17]:
Code
def estimate_training_cost(vocab_size, num_negatives, num_training_pairs):
    """
    Compare computational cost of full softmax vs negative sampling.

    Cost is measured in dot product operations per training step.
    """
    softmax_cost = num_training_pairs * vocab_size
    neg_sampling_cost = num_training_pairs * (1 + num_negatives)
    speedup = softmax_cost / neg_sampling_cost
    return softmax_cost, neg_sampling_cost, speedup


vocab_size = 100000
num_negatives = 10
num_training_pairs = 1_000_000_000  # 1 billion pairs

softmax_cost, neg_samp_cost, speedup = estimate_training_cost(
    vocab_size, num_negatives, num_training_pairs
)
Out[18]:
Console
Computational Cost Comparison:
-------------------------------------------------------
Vocabulary size:              100,000
Training pairs:         1,000,000,000
Negative samples k:                10

Full softmax cost:    100,000,000,000,000 dot products
Neg sampling cost:     11,000,000,000 dot products

Speedup factor:                 9,091x

This is why negative sampling enables practical training!

For a 100,000-word vocabulary with 10 negative samples, negative sampling performs approximately 9,000 times fewer operations per training step than full softmax. What would take months with softmax can be completed in days with negative sampling.

Out[19]:
Visualization
Log-log scale line plot showing softmax cost increasing steeply with vocabulary while negative sampling remains flat.
Operations per training step for full softmax and negative sampling (k=10) across vocabulary sizes on a log-log scale. Softmax operations grow proportionally with vocabulary size while negative sampling requires a constant 11 operations regardless of vocabulary size.
Bar chart showing speedup factor increasing from hundreds to tens of thousands as vocabulary size grows.
Operations per training step for full softmax and negative sampling (k=10) across vocabulary sizes on a log-log scale. Softmax operations grow proportionally with vocabulary size while negative sampling requires a constant 11 operations regardless of vocabulary size.

The trade-off between the number of negative samples and computational cost is linear: doubling kk doubles the work per training step. But the relationship between kk and embedding quality is sublinear, with diminishing returns as kk increases. The sweet spot at k=5k = 5 to 1515 captures most of the quality gains while keeping training times manageable.