Word Embeddings
Word embeddings are dense, low-dimensional vector representations of words that capture semantic and syntactic relationships, allowing computers to treat language mathematically. They matter enormously in modern AI because virtually every NLP system — from search engines to large language models — depends on some form of learned word representation. The remarkable fact: a simple vector arithmetic operation reveals that king − man + woman ≈ queen, meaning geometry in embedding space encodes human-level conceptual relationships.
Words are just symbols to a computer — "cat" and "dog" look no more related than "cat" and "democracy." Word embeddings solve this by mapping every word to a list of numbers (a vector) so that words with similar meanings end up close together in that number space. The model learns these numbers automatically by studying which words tend to appear near each other in billions of sentences. The "aha moment" is realizing that meaning emerges from context — you can tell a lot about a word by the company it keeps.
How It Works
At heart, word embeddings are a lookup table: each word in the vocabulary gets a row in a matrix, and that row is the word's vector. The key question is how to learn good numbers for those rows. The dominant approach — popularized by Word2Vec — trains a tiny neural network on a self-supervised task called predicting context. No human labels are needed; the text itself provides supervision.
The Skip-Gram objective asks: given a center word, predict which words are likely to appear nearby (within a sliding window).
Explanation: We want to maximize the total log-probability of seeing each observed context word c given its center word w, across all word–context pairs in the training data. Here:
- is the loss (we minimize its negative, i.e., maximize probability)
- is the center word
- is a context word (neighbor in the window)
- is the dataset of all observed (center, context) pairs
The probability of a context word is computed via a softmax over dot products:
Explanation: Words whose vectors point in a similar direction (high dot product) get assigned higher probability of being neighbors. Here:
- is the embedding vector for center word
- is the embedding vector for context word
- is the vocabulary size (sum over all possible words in the denominator)
After training, the embedding matrix is extracted and used as a lookup table in downstream tasks.
A minimal Python example — loading and using pre-trained embeddings: ```python
# pip install gensim from gensim.models import KeyedVectors import gensim.downloader as api
# Load a small pre-trained Word2Vec model (~66 MB) model = api.load("word2vec-google-news-300")
# Each word maps to a 300-dimensional vector king_vec = model["king"] print(f"Vector shape: {king_vec.shape}") # (300,)
# Semantic arithmetic: king - man + woman ≈ queen result = model.most_similar( positive=["king", "woman"], negative=["man"], topn=3 ) print(result)
# [('queen', 0.71), ('monarch', 0.64), ('princess', 0.63)]
# Find nearest neighbors neighbors = model.most_similar("python", topn=5) for word, score in neighbors: print(f" {word}: {score:.3f}") ```
Training your own embeddings from scratch with Gensim: ```python from gensim.models import Word2Vec
# Toy corpus — in practice, use millions of sentences sentences = [ ["the", "cat", "sat", "on", "the", "mat"], ["the", "dog", "ran", "in", "the", "park"], ["cats", "and", "dogs", "are", "pets"], ["paris", "is", "the", "capital", "of", "france"], ["london", "is", "the", "capital", "of", "england"], ] model = Word2Vec( sentences=sentences, vector_size=50, # embedding dimensions window=2, # context window size min_count=1, # ignore words appearing fewer times sg=1, # 1 = skip-gram, 0 = CBOW epochs=100 )
# Retrieve a vector vec = model.wv["cat"] print(f"'cat' vector (first 5 dims): {vec[:5]}")
# Save / reload model.wv.save("my_embeddings.kv") ```
Measuring similarity — cosine similarity:
Explanation: Cosine similarity measures the angle between two vectors — 1 means identical direction (very similar words), 0 means perpendicular (unrelated), −1 means opposite. ```python import numpy as np def cosine_similarity(u, v): return np.dot(u, v) / (np.linalg.norm(u) * np.linalg.norm(v)) cat = model.wv["cat"] dog = model.wv["dog"] mat = model.wv["mat"] print(f"cat ↔ dog: {cosine_similarity(cat, dog):.3f}") # high print(f"cat ↔ mat: {cosine_similarity(cat, mat):.3f}") # low ```
Bengio is a Canadian computer scientist and Turing Award laureate (2018) who co-founded the deep learning revolution. His landmark 2003 paper A Neural Probabilistic Language Model (with Ducharme, Vincent, and Jauvin) introduced the idea of learning continuous word representations jointly with a language model — the conceptual grandfather of all modern embeddings. Surprising fact: Bengio initially struggled to get the 2003 paper accepted; reviewers found the idea of learning word vectors simultaneously with a language model unnecessarily complicated.
Mikolov is a Czech ML researcher who created Word2Vec while at Google in 2013, making high-quality word embeddings practical at massive scale for the first time. His key insight was that you could strip away most of the neural network complexity and train just the embedding lookup efficiently using negative sampling, cutting training time from weeks to hours on billions of words. Notably, he discovered the famous king−man+woman≈queen analogy accidentally while exploring what the geometry of the learned space actually encoded.
Intuition
Imagine a huge city map where every word is a building. Words that "live together" — appearing in similar sentences — end up built close to each other on this map. "Cat" and "dog" are neighbors because both appear near words like "pet," "furry," "feed," and "vet." "King" and "queen" are neighbors, but they're also both exactly as far from "man" and "woman" as each other — and that direction of difference (the road between the male and female neighborhoods) is the same road you travel between "king" and "queen." The vector arithmetic (king − man + woman = queen) is just asking: start at king, walk the road to the male neighborhood, then walk the same distance in the female direction — and you land at queen. The geometry of the map encodes human concepts automatically, without anyone programming it. The model learned this map purely by reading text and noticing who lives near whom. Think of it this way: if you were blindfolded and could only hear what words were said around you, you'd eventually figure out that "cat" and "dog" must be similar things — because people talk about them the same way. That's exactly what Word2Vec does.
Historical Development
Origins · 1 minhow this was discovered
Why It Matters
Prerequisites
What This Leads To
See Also
Transformer Architecture · Tokenization · Language Models · Dimensionality Reduction · Cosine Similarity