Word2Vec Training › Comparing Gensim and PyTorch Results

Michael BrenndoerferApril 7, 20253 min read
Language AI HandbookAll chapters
Part 4 of 67Word Embeddings
Chapter 23Word2Vec Training › Comparing Gensim and PyTorch Results
Reading section 10 of 14 · Comparing Gensim and PyTorch Results · 3 minSection 10 of 14 · 3 min

Book contents

Section 10 of 14 · 3 min read

Word2Vec Training › Comparing Gensim and PyTorch ResultsOne section · Sections in this chapter10 / 14 ↓
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

Both implementations are training the same underlying model, but they differ in how they handle subsampling, the exact negative sampling implementation, and initialization. Comparing the similarity structures they learn reveals how sensitive embeddings are to implementation details.

This sensitivity is an important practical insight. Two runs of Word2Vec on the same corpus with different random seeds will produce embeddings that are rotations of each other in embedding space (word similarities will match, but the actual vector coordinates will differ). Two runs with different hyperparameters or implementation details will produce embeddings with different similarity structures. There is no single "ground truth" set of word embeddings: the geometry depends on the training choices.

This is not a bug but a feature. The training objective defines what we want to be similar (words that appear in similar contexts), but it does not define a unique solution. Many configurations of the embedding space satisfy the training objective equally well. SGD with random initialization finds one of these configurations, and different initializations or slightly different training procedures find different ones. For most downstream applications, any well-trained configuration works equally well because the relative structure (which words are near each other) is preserved even if the absolute coordinates differ.

Out[34]:
Visualization
Heatmap of Gensim Word2Vec cosine similarities with nearly all word pairs shown in dark red, indicating uniformly high similarity.
Cosine similarity matrix for shared vocabulary words learned by Gensim Word2Vec. Most off-diagonal scores are uniformly high on this tiny corpus, showing how limited data can produce a weakly differentiated embedding space.
Heatmap of PyTorch Word2Vec cosine similarities with a red diagonal and varied pale red, white, and blue off-diagonal cells.
Cosine similarity matrix for the same words learned by the custom PyTorch Skip-gram implementation. Its more varied positive and negative scores contrast with the Gensim matrix, illustrating how initialization and training details can dominate results when the corpus is very small.