TechCoder.io / AI & Machine Learning

Word Embeddings

Turning words into vectors. Understand Word2Vec, GloVe, FastText, contextual embeddings (BERT), and how computers capture semantic meaning at different levels.

By TechCoder TeamLast updated: 2026-07-23
In a Nutshell

Turning words into vectors. Understand Word2Vec, GloVe, FastText, contextual embeddings (BERT), and how computers capture semantic meaning at different levels. This hands-on tutorial focuses on practical implementation of word embeddings concepts.

Word Embeddings

How do you explain to a computer that "King" is similar to "Queen"? With Word Embeddings, we represent words as dense vectors (lists of numbers) in a high-dimensional space where similar words cluster together.

1. One-Hot Encoding vs. Embeddings πŸ†š

  • One-Hot Encoding:

    • Cat = [1, 0, 0, 0]
    • Dog = [0, 1, 0, 0]
    • Problem: No relationship between vectors. They are orthogonal. Huge memory usage for large vocabularies.
  • Dense Embeddings:

    • Cat = [0.2, 0.9, -0.1]
    • Dog = [0.2, 0.8, -0.2]
    • Result: Similar words have similar vectors. Compact representation.

2. Word2Vec 🧠

Created by Google in 2013. It learns word associations from a large corpus of text. Key idea: "You shall know a word by the company it keeps." (J.R. Firth, 1957)

  • CBOW (Continuous Bag of Words): Predict target word from context words.
    • Input: ["The", "sat", "on"] β†’ Predict: "cat"
  • Skip-Gram: Predict context words from target word. Better for rare words.
    • Input: "cat" β†’ Predict: ["The", "sat", "on"]

3. GloVe (Global Vectors) 🌍

Developed at Stanford, GloVe captures global co-occurrence statistics rather than local windows.

Key Insight: GloVe directly factorizes the word co-occurrence matrix.

  • If words A and B frequently appear together β†’ their vectors will be close.
  • The ratio P(ice|solid) / P(steam|solid) captures the relationship "solid β†’ ice".

Advantage over Word2Vec: Better captures global semantic relationships and analogy tasks.

PYTHON PLAYGROUND
⏳ Loading editor…

πŸ“š Official Resources

4. Vector Arithmetic βž•

The most famous example of Word2Vec/GloVe magic:

King - Man + Woman = Queen

This shows that the model captured the concept of "Gender" and "Royalty" as directions in the vector space:

PYTHON PLAYGROUND
⏳ Loading editor…

5. FastText: Subword Embeddings πŸ”€

Created by Meta AI, FastText improves on Word2Vec by representing words as bags of character n-grams:

  • "playing" β†’ ["pla", "lay", "ayi", "yin", "ing"] (n-grams of size 3)
  • The word vector = sum of its n-gram vectors

Why this matters:

  • Handles out-of-vocabulary (OOV) words β€” "Techcoder" can be embedded via its n-grams
  • Better for morphologically rich languages (Turkish, Finnish, German)
  • Works well for misspelled words in noisy text

πŸ“š Official Resource

6. Contextual Embeddings: The BERT Revolution 🌊

Word2Vec, GloVe, and FastText create static embeddings β€” each word has ONE vector regardless of context.

Problem: "Bank" has the same vector in:

  • "I went to the bank to deposit money." (financial institution)
  • "The river bank was muddy." (river edge)

BERT's solution: Contextual Embeddings

BERT generates a different vector for "bank" depending on the surrounding words:

PYTHON PLAYGROUND
⏳ Loading editor…

7. Embedding Models Comparison πŸ“Š

ModelTypeDimensionsBest For
Word2VecStatic100-300Word analogies, fast inference
GloVeStatic50-300Global semantics, well-studied
FastTextStatic (subword)100-300Noisy text, OOV words, non-English
BERTContextual768Classification, QA, NER
text-embedding-3-smallContextual (API)1536RAG, semantic search (production)
all-MiniLM-L6-v2Contextual (local)384Fast CPU inference, free

πŸ“š Official Resources

8. Cosine Similarity πŸ“

To measure how similar two words/sentences are, we calculate the cosine of the angle between their vectors.

  • 1.0: Identical direction (Synonyms).
  • 0.0: Unrelated (Orthogonal).
  • -1.0: Opposite direction (Antonyms).
PYTHON PLAYGROUND
⏳ Loading editor…

Quiz

Quiz

Question 1 of 3

What is the main advantage of Embeddings over One-Hot Encoding?

They are easier to calculate
They capture semantic meaning and relationships between words
They use more memory

Key Takeaways

βœ… Static Embeddings (Word2Vec, GloVe, FastText) create one vector per word β€” fast but context-blind.
βœ… Contextual Embeddings (BERT, GPT) create different vectors per word based on context β€” essential for disambiguation.
βœ… FastText handles out-of-vocabulary words via character n-grams β€” great for noisy or multilingual text.
βœ… Cosine Similarity measures semantic proximity between vectors.
βœ… The MTEB Leaderboard is the best resource for choosing an embedding model for your task.

What's Next?

Word2Vec creates static embeddings. But what architecture makes BERT's contextual embeddings possible?

Next Chapter: Transformers & Attention.