Understanding Word Embeddings: Word2Vec, GloVe, and Beyond

Learning Objectives: By the end of this lesson, you will be able to:

  • Explain the concept and motivation behind word embeddings
  • Use pre-trained Word2Vec and GloVe embeddings for similarity tasks
  • Identify limitations of static embeddings and the need for contextual representations

1. Welcome and Introduction (5 minutes)

Icebreaker Activity

Have you ever noticed how your phone or search engine can suggest words that “mean the same thing”? How do computers know that “king” and “queen” are related, or that “Paris” and “France” are connected?

Lesson Overview

  • Duration: Approximately 2.5 hours including all exercises (or 60–90 minutes when selected activities are assigned)
  • Prerequisites: Basic Python, understanding of tokenization and text representation
  • Materials Needed: Python (Jupyter Notebook or IDE), Gensim, pre-trained embedding files (Word2Vec, GloVe)

2. Learning Objectives and Session Outline (5 minutes)

What We’ll Cover Today:

  1. The motivation for word embeddings
  2. Word2Vec: Skip-gram and CBOW models
  3. GloVe: Global Vectors for Word Representation
  4. Using pre-trained embeddings for similarity tasks
  5. Limitations of static embeddings
  6. Hands-on practice and exercises
  7. Common pitfalls and how to avoid them

Success Criteria:

By the end of this session, you should be able to load and use word embeddings for basic NLP tasks and explain their strengths and weaknesses.

3. Foundation Knowledge (15-20 minutes)

Core Concepts

What Are Word Embeddings?

Word embeddings are dense vector representations of words that capture their meanings, semantic relationships, and context. Unlike one-hot encoding, embeddings place similar words closer together in a high-dimensional space.

Real-World Analogy: Imagine a map where cities that are culturally or geographically close are also close on the map. Word embeddings create a “semantic map” for words.

Why This Matters: Embeddings allow algorithms to understand relationships between words, enabling tasks like similarity search, analogy solving, and more.

Word2Vec

Word2Vec is a family of models that learn word embeddings from large text corpora. The two main architectures are:

  • Skip-gram: Predicts context words from a target word.
  • CBOW (Continuous Bag of Words): Predicts a target word from context words.

GloVe

GloVe (Global Vectors) is another method that learns embeddings by factorizing a word co-occurrence matrix, capturing global statistical information.

Key Terminology

  • Embedding: A dense vector representing a word’s meaning.
  • Context Window: The number of words considered around a target word.
  • Analogy: A relationship like “king - man + woman = queen” in embedding space.

4. Detailed Explanation and Examples (25-30 minutes)

Understanding Word Embeddings

Traditional representations like one-hot encoding treat all words as equally distant. Embeddings, however, capture similarity and relationships. For example, the distance between “cat” and “dog” is smaller than between “cat” and “car”.

Practical Applications

  • Finding similar words (e.g., synonyms)
  • Solving analogies (e.g., “Paris” is to “France” as “Berlin” is to “Germany”)
  • Improving downstream NLP tasks (classification, translation)

Common Misconceptions

  1. Misconception: All embeddings are the same. Reality: Different algorithms and corpora produce different embeddings.
  2. Misconception: Embeddings always capture context. Reality: Static embeddings (Word2Vec, GloVe) do not capture word sense in context.

5. Live Coding Demonstration (20-25 minutes)

Example 1: Loading Pre-trained Word2Vec Embeddings with Gensim

from gensim.models import KeyedVectors
# Download GoogleNews-vectors-negative300.bin.gz from Google if needed
model = KeyedVectors.load_word2vec_format('GoogleNews-vectors-negative300.bin.gz', binary=True)
print(model['king'])  # 300-dimensional vector

Explanation:

  • Loads a pre-trained Word2Vec model
  • Retrieves the embedding for the word “king”

Example 2: Finding Similar Words and Solving Analogies

# Most similar words to 'computer'
print(model.most_similar('computer'))

# Analogy: king - man + woman = ?
print(model.most_similar(positive=['king', 'woman'], negative=['man']))

Key Points:

  • Embeddings can be used for similarity and analogy tasks
  • The model computes cosine similarity between vectors

Example 3: Using GloVe Embeddings

from gensim.models import KeyedVectors

# GloVe text has no word2vec header; KeyedVectors adds similarity search.
glove = KeyedVectors.load_word2vec_format(
    'glove.6B.50d.txt', binary=False, no_header=True
)
print(glove['king'])

Debugging Demonstration

Show common errors and how to fix them:

# Common mistake: KeyError for out-of-vocabulary words
try:
    print(model['unicorn'])
except KeyError:
    print('Word not in vocabulary!')

# Corrected version: check before accessing
if 'unicorn' in model:
    print(model['unicorn'])
else:
    print('Word not in vocabulary!')

6. Hands-On Practice (30-40 minutes)

Exercise 1: Basic Practice (10 minutes)

Objective: Find the most similar words to a given word using pre-trained embeddings.

Instructions:

  1. Load Word2Vec or wrap GloVe in KeyedVectors; do not use a plain dictionary.
  2. Write a function to return the top 5 most similar words to a user-provided word.

Starter Code:

def most_similar_words(model, word, topn=5):
    # Your code here
    pass

print(most_similar_words(model, 'science'))

Solution:

def most_similar_words(model, word, topn=5):
    if word in model:
        return model.most_similar(word, topn=topn)
    else:
        return f"'{word}' not in vocabulary."

print(most_similar_words(model, 'science'))

Exercise 2: Intermediate Challenge (15 minutes)

Objective: Solve analogies using word embeddings.

Requirements:

  • Write a function that takes three words (A, B, C) and returns the word D such that A is to B as C is to D.
  • Use the model’s analogy capabilities.

Hints:

  • Use model.most_similar(positive=[B, C], negative=[A])
  • Handle out-of-vocabulary words gracefully

Exercise 3: Creative Application (15 minutes)

Objective: Visualize word embeddings in 2D using PCA or t-SNE.

Challenge:

  • Select a set of related words (e.g., countries, animals)
  • Reduce their embeddings to 2D and plot them

Extension Ideas:

  • Color-code by category
  • Try with your own list of words

7. Common Pitfalls and Troubleshooting (10 minutes)

Frequent Mistakes

  1. Problem: Using words not in the embedding vocabulary Cause: Pre-trained models have a fixed vocabulary Solution: Always check if a word exists before accessing its vector

  2. Problem: Assuming embeddings capture context Cause: Static embeddings do not account for word sense Solution: Use contextual embeddings (e.g., BERT) for context-sensitive tasks

Debugging Tips

  • Print the vocabulary size and sample words
  • Use try/except for KeyErrors
  • Visualize embeddings to check for clustering

8. Best Practices and Professional Tips (5 minutes)

Industry Standards

  • Use pre-trained embeddings for most tasks
  • Fine-tune embeddings if you have enough data
  • Document the source and version of embeddings used

Next Steps in Your Learning Journey

  • Explore contextual embeddings (ELMo, BERT)
  • Learn about fine-tuning embeddings for specific tasks
  • Try using embeddings in downstream applications (classification, clustering)

Accuracy Note: Static and Contextual Embeddings

Word2Vec and GloVe assign one static vector to each vocabulary item, so the vector for “bank” is identical in “river bank” and “bank loan.” Contextual encoders instead compute a representation for each occurrence. Also, downloaded GloVe text is only a mapping from words to vectors: a plain Python dictionary has no most_similar method. Load it into a similarity-aware structure such as gensim.models.KeyedVectors, or explicitly compute cosine similarity against its matrix.

9. Summary and Review (5 minutes)

Key Takeaways

  1. Word embeddings capture semantic relationships between words
  2. Word2Vec and GloVe are popular static embedding methods
  3. Always handle out-of-vocabulary words and understand the limitations of static embeddings

Quick Review Questions

  1. What is the main advantage of word embeddings over one-hot encoding?
  2. How do Skip-gram and CBOW differ?
  3. Why might you need contextual embeddings?

For Instructors

Teaching Tips

  • Show real-world examples of word analogies
  • Encourage students to experiment with their own word lists
  • Discuss the evolution from static to contextual embeddings

Assessment Ideas

  • Quiz on embedding concepts and terminology
  • Coding assignment: build a word similarity tool
  • Peer review: students explain analogy results to each other

Adaptation Notes

  • For beginners: focus on using pre-trained models
  • For advanced students: introduce training embeddings from scratch
  • For online delivery: provide links to pre-trained embedding files