Word Embeddings (Word2Vec, GloVe, FastText)
Represent words as dense vectors that capture similarity and analogy structure.
In plain words
A word embedding gives every word a short list of numbers so that words that mean similar things get similar lists, which lets a computer measure how close two words are.
Think of it like…
A map of words. Instead of an alphabetical list, every word is placed on a map where words used in similar ways (cat, dog, pet) cluster together and unrelated words (car) sit far away, and directions on the map can carry meaning.
Where you’ve seen it
With toy 2-D vectors cat = (0.9, 0.1), dog = (0.8, 0.3) and car = (0.1, 0.95), the dot product of cat and dog is 0.75 and the lengths are 0.906 and 0.854, so cosine similarity is 0.75 / 0.774 = 0.969; cat versus car is 0.185 / 0.865 = 0.214, which captures the intuition that cats and dogs are related and cars are not. A toy analogy uses man = (1, 0, 0.5), woman = (1, 1, 0.5), king = (3, 0, 2) and queen = (3, 1, 2): king − man + woman = (3, 1, 2) = queen exactly (real vectors only approximate this and the nearest neighbour search excludes the query words). In practice, the widely used Google News Word2Vec vectors cover about 3 million words and phrases at 300 dimensions, which is 3,000,000 × 300 × 4 bytes = 3.6 GB. A smaller domain-specific vocabulary of 100,000 words at 300 dimensions takes 120 MB. FastText breaks the word "where" into the boundary-marked character trigrams <wh, whe, her, ere, re> (n from 3 to 6 in practice), so a misspelling such as "wheer" still shares several pieces and gets a sensible vector. A support-ticket team averages the vectors of the words in a ticket to get a 300-dimensional document vector, trains a logistic regression on it and compares with TF-IDF; embeddings help most when the training set is small and wording varies. They also check for learned bias in the vectors before using them in anything that affects people.
Words you’ll hear
- Embedding
- A dense, low-dimensional vector representing a word or other item.
- Skip-gram / CBOW
- Word2Vec objectives: predict context from centre word, or centre from context.
- Cosine similarity
- The cosine of the angle between two vectors; 1 means the same direction.
- Subword n-grams
- Character pieces used by FastText to build vectors for rare and unseen words.
Visual diagram
Theory & deep dive
How embeddings are learned
The distributional hypothesis says a word is characterised by the company it keeps. Skip-gram takes a centre word and trains the model to predict the words within a window (commonly ±5); CBOW does the reverse and is faster for frequent words. Computing a full softmax over a vocabulary of 100,000 words is expensive, so negative sampling turns each step into a small binary problem: push the true (centre, context) pair together and a few randomly sampled wrong pairs apart. After training, the input embedding matrix, 100,000 rows × 300 columns (120 MB in float32), is the product. GloVe instead fits vectors whose dot products approximate the log of global co-occurrence counts, and gives comparable quality.
FastText represents each word by its character n-grams plus the whole word: "where" yields <wh, whe, her, ere, re> for n = 3. The word vector is the sum of these piece vectors, so words sharing pieces share information, rare and morphologically rich words (German compounds, Turkish suffixes) get good vectors and an unseen word or typo is still representable. That is the practical reason to prefer FastText on noisy user text.
cat = (0.9, 0.1), dog = (0.8, 0.3): dot 0.75, |cat| = 0.906, |dog| = 0.854 → cosine 0.969. car = (0.1, 0.95): cat·car = 0.185, |car| = 0.955 → cosine 0.214. Similar words point the same way.
Using embeddings and their limits
Common uses: nearest-neighbour search for synonyms and query expansion; document vectors by averaging (or TF-IDF-weighted averaging) the vectors of its words, fed to a linear model; initialising the embedding layer of a neural network (fine-tuning it or freezing it); and clustering vocabulary or short texts. Analogies such as king − man + woman ≈ queen arise from consistent offsets: in the toy vectors (1, 0, 0.5) for man, (1, 1, 0.5) for woman, (3, 0, 2) for king and (3, 1, 2) for queen the arithmetic is exact, while real vectors only land near the answer and evaluation tools exclude the input words. Evaluate by downstream task performance rather than analogy scores alone.
Limits: vectors are static, so "bank" gets one vector blending finance and rivers; they inherit bias from the training text (occupation words can align with gender), so audit them before using in decisions about people; they depend on corpus domain and size (train or fine-tune on your own text for specialised vocabularies such as medicine or finance); and averaging loses word order. Contextual models (BERT-style transformers) produce a different vector for each occurrence, fixing polysemy, at much higher compute cost, so static embeddings remain useful for fast, lightweight pipelines.
king − man + woman = (3, 0, 2) − (1, 0, 0.5) + (1, 1, 0.5) = (3, 1, 2), which equals queen in this toy space. Real embeddings give a nearby vector, and the closest word (after excluding the inputs) is often queen.
How it works, step by step
Step 1 of 41. Choose or train vectors
Use pretrained vectors for general text or train on your corpus for specialised vocabulary.
- Prefer
- FastText for noisy text
When to use it & trade-offs
What it is
A word embedding maps each word to a dense vector (typically 100 to 300 numbers) so that words used in similar contexts end up near each other. Word2Vec learns vectors by predicting context words from a centre word (skip-gram) or the centre word from its context (CBOW), usually with negative sampling. GloVe factorises a global word co-occurrence matrix. FastText represents a word as the sum of its character n-gram vectors, so it can build vectors for rare words, misspellings and unseen words. Similarity is measured with cosine similarity, and relationships can show up as vector offsets (king − man + woman ≈ queen). These vectors are static: each word has one vector regardless of context.
Why it matters: One-hot or TF-IDF vectors treat "car" and "automobile" as unrelated columns and are huge and sparse. Embeddings compress words into a small dense space where distance means something, which helps small-data classifiers, similarity search, clustering and initialising neural networks. They are also the conceptual step towards contextual transformers: understanding static embeddings clarifies what attention-based models improve, such as polysemy (bank of a river versus a bank loan).
When to reach for it
- Small-data text classification with averaged vectors
- Synonym discovery and query expansion
- Initialising embedding layers in neural models
When to avoid or be careful
- Averaging vectors for tasks where word order decides meaning
- Using general vectors for a highly specialised vocabulary without checking coverage
- Treating analogy results as proof of fairness or quality
Static embedding methods
Strengths: Efficient; good similarity structure.
Watch out: No vectors for unseen words.
Strengths: Uses corpus-wide statistics; widely available.
Watch out: Static; needs a large co-occurrence matrix to train.
Strengths: Builds vectors from subword pieces.
Watch out: Larger models; subword noise.
Strengths: Different vector per context.
Watch out: Much heavier compute.
Interview signals & numbers to know
When this topic comes up
- “How does Word2Vec learn embeddings?”
- Skip-gram versus CBOW
- Cosine similarity versus Euclidean distance
- How FastText handles unseen words
- Limits of static embeddings
Benchmarks & rules of thumb
- 300
- a common embedding dimension
- 100,000 × 300 × 4 B = 120 MB
- embedding table size in float32
- ≈ 3.6 GB
- Google News vectors: 3 million words × 300 × 4 bytes
Common mistakes & follow-ups
Common interview mistakes
- Using a different tokeniser than the one the vectors were trained with.
- Ignoring out-of-vocabulary rates on domain text.
- Assuming similarity in the vector space means a fair or unbiased model.
Likely interviewer follow-ups
- Why use cosine similarity rather than Euclidean distance?
- How does FastText produce a vector for an unseen word?
- What does a contextual embedding fix?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Word Embeddings (Word2Vec, GloVe, FastText) with data science interview Q&A and hands-on practice.