Building Intuition for Word Embeddings - From Math to Meaning

How does Word2Vec turn language into geometry? Walk this galaxy of word relationships

July 18, 2025
12 min read
0 views
#NLP Foundations#CoreToCode#Word2Vec#Embeddings

In the world of natural language processing, one of the most revolutionary breakthroughs came from a deceptively simple idea: what if we could represent words as mathematical vectors? This concept, materialized through Word2Vec, fundamentally changed how machines understand human language and opened new possibilities for semantic analysis that continue to influence modern AI systems today.

The Magic Behind Word Embeddings

Earlier statistical language models like N-grams relied heavily on counting word co-occurrences and suffered from data sparsity and poor generalization. These models couldn't effectively handle unseen word combinations or capture deeper semantic relationships.

Word2Vec, introduced by Mikolov et al. in 2013, changed how we represent word meanings. Instead of treating words as separate symbols, it maps them into a vector space. In this space, words with similar meanings are placed close together - making relationships easier to capture mathematically.

Word2Vec Galaxy interface showing 3D word visualization

Figure 1: Two-dimensional PCA projection of the 1000-dimensional Skip-gram vectors of countries and their capital cities.

This adopts a distributed representation approach, where each word is embedded as a dense vector in a continuous space. These embeddings allow the model to capture multiple layers of similarity - whether it’s based on meaning, verb tense, or even morphological structure.

What makes this especially powerful is how Word2Vec is designed to preserve linear regularities in the vector space. This means relationships between words - like king - man + woman ≈ queen - emerge naturally as vector arithmetic, making complex linguistic patterns surprisingly simple to compute.

Two Architectures, One Goal

They present two distinct neural network architectures each considered a log-linear model, meaning the logarithm of the predicted probability is a linear function of the input features. With this infrastructure, they trained two model variants each bringing a unique approach to capturing the meaning and relationships of words in vector space.

To scale this training across massive corpora, the authors implemented parallelized training using DistBelief frameworks such as Downpour SGD and SandBlaster L-BFGS, for fast, multi-threaded learning across machines. Additionally, they used the Adagrad optimization algorithm a method ideal for datasets with high variance.

AdaGrad Optimization

Word2Vec uses AdaGrad as its optimizer, which dynamically adjusts the learning rate for each parameter during training. This method is particularly effective when paired with mini-batch asynchronous gradient descent, as it eliminates the need to manually tune individual learning rates per parameter.

Vanilla Stochastic Gradient Descent (SGD), uses a fixed global learning rate ηη that is manually selected. AdaGrad introduces a smarter update strategy. For each parameter θi\theta_i it maintains a cumulative sum of the squares of all past gradients up to time step tt, denoted as Gt,iiG_{t,ii}. The current gradient is represented as gt,ig_{t,i}.

The update rule becomes:

θt+1,i=θt,iηGt,ii+ϵgt,i\theta_{t+1,i} = \theta_{t,i} - \frac{\eta}{\sqrt{G_{t,ii} + \epsilon}} \cdot g_{t,i}

The core idea is this: parameters with larger accumulated gradients Gt,iiG_{t,ii} receive smaller updates, since the denominator becomes larger - effectively shrinking the step size. Conversely, for parameters with smaller accumulated gradients, the learning rate remains relatively high, allowing them to continue adapting.

1. Continuous Bag of Words (CBOW)

CBOW predicts a target word based on its surrounding context words. Given the context "Hope can [?] you free" the model learns to predict "set" by analyzing the surrounding words. This architecture is better with frequent words and generally trains faster on large datasets.

Continuous Bag of Words model

Figure 2: Continuous Bag of Words model

2. Skip-gram

Skip-gram works in reverse – given a target word, it predicts the surrounding context. From "set" it learns to predict words like "Hope", "can", "you" and "free". This approach proves particularly effective for rare words and captures more nuanced semantic relationships.

Skip-gram model

Figure 3: Skip-gram model

The Skip-gram model maximizes the average log probability of words wt+jw_{t+j} within a context window cc around each target word wtw_t. This encourages the model to learn representations where similar words have similar vector representations, as they tend to appear in similar contexts.

1Tt=1Tcjc,j0logp(wt+jwt)\frac{1}{T} \sum_{t=1}^{T} \sum_{-c \leq j \leq c, j \neq 0} \log p(w_{t+j}|w_t)

Where the conditional probability logp(wt+jwt)\log p(w_{t+j}|w_t) is defined using the softmax function:

p(wOwI)=exp(vwOTvwI)w=1Wexp(vwTvwI)p(w_O|w_I) = \frac{\exp(v_{w_O}^T v_{w_I})}{\sum_{w=1}^{W} \exp(v_w^T v_{w_I})}

Here, vwOTv_{w_O}^T is the input vector of the center word, and vwIv_{w_I} is the output vector of a possible context word. Their dot product vwOT.vwIv_{w_O}^T .v_{w_I} captures how similar the two words are in the embedding space. The exponential in the numerator transforms this similarity score into a positive value, while the denominator w=1Wexp(vwTvwI)\sum_{w=1}^{W} \exp(v_w^T v_{w_I}) sums these exponentials over all words in the vocabulary to normalize the result into a valid probability. This allows the model to assign higher probabilities to more semantically relevant context words.

This softmax-based formulation, while mathematically sound, becomes extremely inefficient in practice. The denominator sums over the entire vocabulary to normalize the probabilities, which means that for every training step, the model must compute scores for all possible words, even though only one target word is needed.

Clever approximation strategies like hierarchical softmax and negative sampling were introduced to reduce the computational burden without compromising too much on performance.

The Mathematics of Meaning

The famous analogy v[king] - v[man] + v[woman] = v[queen] isn't just a clever demonstration; it reveals fundamental algebraic structures in human language.

Vector arithmetic demonstration from the original paper Figure 4: Vector arithmetic visualization showing semantic relationships as geometric transformations in vector space.

These vector operations work because Word2Vec learns to encode semantic dimensions implicitly. The vector difference kingman\vec{king} - \vec{man} captures the concept of royalty, while woman\vec{woman} provides the gender dimension, resulting in queen\vec{queen} – the female royal counterpart.

The mathematical relationship can be expressed as: queenkingman+woman\vec{queen} \approx \vec{king} - \vec{man} + \vec{woman}

This works because Word2Vec organizes semantically similar words in geometric clusters, where consistent relationships (like gender or royal status) become linear transformations in the vector space.

A Critical Implementation Detail for Vector Arithmetic:

If you include "king" in the search, it often ends up being the closest vector to the result because:

  • It contributes heavily to the final expression (was the starting point)
  • "king" is already close to "queen" in embedding space
  • So cosine similarity might prefer "king" over "queen" by a small margin

Exclude the original words (king, man, woman) from the nearest neighbor search. This avoids the model just echoing what it already knows and forces it to find true analogical matches like "queen".

The Galaxy Visualization

Understanding high-dimensional word vectors is a significant challenge for human understanding. The Word2Vec Galaxy application solves this by reducing 300-dimensional vectors to interactive 3D visualizations using Principal Component Analysis (PCA), getting the principal components with highest variance.

3D scatter plot showing semantically related words clustered together Figure 5: 3D clustering of semantically similar words – notice how related concepts group together in transformed space

The visualization shows fascinating semantic neighborhoods: animals cluster with other animals, colors group with colors, and verbs associate with verbs. This spatial organization emerges naturally from the training process, demonstrating how embeddings captures the underlying structure of human language.

Optimization Techniques in Word2Vec

The efficiency stems from several key optimization techniques that make training on large vocabularies computationally feasible:

i) Huffman Binary Tree

The Huffman binary tree provides an better way to represent the vocabulary hierarchy in Word2Vec. More frequent words are assigned shorter binary codes, reducing the computational complexity during training. This tree structure allows the model to process common words faster while still maintaining representation quality for less frequent terms.

ii) Hierarchical Softmax

Hierarchical softmax is a technique used to avoid computing the full softmax over a large vocabulary. Instead of treating it as a giant multi-class classification (e.g. 1,000-way), it organizes classes into a binary tree, typically a Huffman tree. So, for V=1000V = 1000 classes, it would require only log2100010\log_2 1000 \approx 10 binary decisions.

  • This reduces complexity from O(V)O(V) to O(logV)O(log V)
  • The output is a series of binary log-probabilities (not a single large softmax)
  • However, it assumes dependency among classes (via the tree structure), so predictions are correlated, not independent.

The paper mentions hierarchical softmax, but it was not used in the final model. Although it's efficient, the authors opted for negative sampling instead.

iii) Negative Sampling

Negative sampling is a cornerstone of self-supervised learning and was used instead of hierarchical softmax, drawing from Noise Contrastive Estimation (NCE).

Rather than performing full vocabulary softmax, it reduces the task to a k+1-way binary classification:

  • For each training pair (center word + context word), the model also samples kk random words (negative samples) from the vocabulary.
  • These "noise" words are assumed to not be contextually related.
  • The model:
    • Maximizes the dot product for the true context word
    • Minimizes it for the negative samples

It doesn't exactly try to predict the correct word out of all words, but rather separates correct (positive) vs incorrect (negative) words in vector space.

iv) Subsampling of Frequent Words

Frequent words like "the", "and", or "of" can occur hundreds of millions of times, but contribute little semantic information.

To improve both training efficiency and embedding quality, the model discards frequent words probabilistically using:

P(wi)=1tf(wi)P(w_i) = 1 - \sqrt{\frac{t}{f(w_i)}}

  • f(wi)f(w_i): frequency of word wiw_i
  • tt: a chosen threshold (e.g., 10510^{-5})

This means that very frequent words are more likely to be skipped, rare and informative words are kept more often, and it helps the model focus on learning richer, more meaningful relationships.

The Lasting Legacy

While newer models like BERT and GPT have captured recent attention, Word2Vec's fundamental insights remain relevant. The concept of dense vector representations continues to underpin modern language models, and the geometric understanding of semantic relationships influences contemporary NLP research.

Advanced visualization features of the Word2Vec Galaxy application Figure 6: Advanced visualization features showing customizable parameters and interactive controls for exploring word relationships

The Word2Vec Galaxy project demonstrates how effective visualization can bridge the gap between complex mathematical concepts and human understanding. By making high-dimensional vector spaces tangible and interactive, we can better appreciate the elegant mathematical structures underlying human language.

Looking Forward

The journey from discrete symbols to continuous vectors represents more than a technical advancement – it reflects a fundamental shift in how we conceptualize meaning itself. Word2Vec showed us that semantics could be mathematical, relationships could be geometric, and understanding could be computational.

"Word2Vec revealed that the structure of meaning could be captured in the geometry of space, transforming how we think about language, similarity, and the mathematical nature of human communication."


References:

  • Mikolov, T., et al. (2013). Efficient Estimation of Word Representations in Vector Space. arXiv preprint arXiv:1301.3781.
  • Mikolov, T., et al. (2013). Distributed Representations of Words and Phrases and Their Compositionality. Advances in Neural Information Processing Systems, 26.
  • Dean, J., et al. (2012). Large Scale Distributed Deep Networks. Advances in Neural Information Processing Systems, 25.
  • The Semicolon. “Word2Vec - Skipgram and CBOW.” YouTube, 2 Oct. 2018.

Reflective Questions

Click to expand reflective questions and answers

What are embedding models? Are they just part of LLM encoders, or more than that?

Embedding models are foundational components that transform discrete tokens (words, sentences, or other units) into dense vector representations that capture semantic meaning. While they do serve as crucial components in LLM encoders, they extend far beyond this role. Embedding models exist as standalone systems for various applications including search engines, recommendation systems, similarity matching, and content clustering. They are fundamental in how machines understand and process human language.

TF-IDF << Word2Vec?

Yes, Word2Vec represents a significant advancement over TF-IDF (Term Frequency-Inverse Document Frequency). While TF-IDF creates sparse, high-dimensional vectors based on word frequency statistics, Word2Vec learns dense, low-dimensional representations that capture semantic relationships. TF-IDF treats words as independent units and cannot understand that "king" and "queen" are related, whereas Word2Vec learns that these concepts are semantically similar and positioned near each other in vector space. TF-IDF still has value in certain information retrieval scenarios where interpretability and simplicity are priorities.

Published on July 18, 2025
0 views