Module 9.2 began at one-hot encoding—bridging Module 9.1 linguistic preprocessing to numbers—then climbed through TF-IDF, BoW, Word2Vec (CBOW / Skip-gram), GloVe, FastText, and sentence embeddings.
This capstone brings embeddings into the PyTorch training loop: nn.Embedding is a learnable lookup table. It is how modern NLP models—RNNs from Volume 08 and especially Attention & Transformers in Volume 10—turn token IDs into differentiable dense vectors end-to-end.
Learning Objectives
By the end of this lesson, students should be able to:
- Explain
nn.Embeddingas a trainable weight matrix indexed by token IDs. - Relate embedding lookup to a multiply by a one-hot vector (without materializing it).
- Build a tiny classifier that embeds tokens, pools, and predicts a label in PyTorch.
- Initialize an embedding layer from pretrained Word2Vec/GloVe/FastText weights.
- Choose
padding_idx, freezing vs. fine-tuning, and embedding dimension thoughtfully. - Connect this module’s static methods to contextual embeddings coming in Volume 10.
An embedding layer (torch.nn.Embedding) stores a matrix W ∈ RV×d and maps integer indices to rows of W. During backpropagation, only the rows touched by the batch receive gradient updates—learning task-specific dense representations.
Full Module Arc
Sparse identity vectors.
Sparse document features.
Pretrained dense static vectors.
Task-trained (or fine-tuned) table.
Lookup = Efficient One-Hot Multiply
If e_i is a one-hot vector for index i, then e_iT W selects row i. nn.Embedding performs that selection directly. This is why embeddings replaced giant one-hot inputs in neural NLP: same information, dense trainable geometry, vastly less memory.
| API / idea | Meaning |
|---|---|
nn.Embedding(V, d) | V vocabulary rows, d-dimensional vectors |
padding_idx | Row kept at zeros; no gradient (for PAD) |
weight | The (V, d) parameter matrix |
from_pretrained | Load GloVe/Word2Vec rows; optional freeze |
Code: Classifier with nn.Embedding
Pretrained vs. From Scratch
From scratch
- Random init, learn on task data.
- Needs enough labels.
- Fully task-specific geometry.
Frozen pretrained
- Load GloVe/W2V/FastText.
- Train only the head.
- Good when data is scarce.
Fine-tuned
- Load then unfreeze.
- Adapt to domain jargon.
- Watch for overfitting.
Strengths and Tradeoffs
Strengths
- End-to-end differentiable with any PyTorch model.
- Memory-efficient vs. explicit one-hots.
- Easy warm-start from Module 9.2 static vectors.
Tradeoffs
- Still one vector per token ID (type-level) unless the rest of the net is contextual.
- Vocab / OOV decisions remain critical.
- Deep context needs attention stacks (Vol. 10).
“nn.Embedding is the same as Word2Vec.” Word2Vec is a pretraining objective on unlabeled text. nn.Embedding is a layer: a parameter table updated by whatever loss you attach (classification, LM, etc.). You can initialize the layer with Word2Vec weights, then fine-tune—or learn it entirely from your supervised objective.
Looking Ahead: Volume 10
Embedding layers feed every modern sequence model. In Volume 10 you will stack attention on top of token embeddings so each position’s vector becomes contextual—different for “bank” in finance vs. river contexts. Module 9.2 gave you the representation toolkit; Attention & Transformers put it to work at scale.
Knowledge Check
- Short Answer: What does
nn.Embedding(V, d)store? Answer: A V×d trainable weight matrix (one vector per token ID). - True/False: Embedding lookup is equivalent to multiplying by a one-hot vector. Answer: True.
- Multiple Choice:
padding_idxis used to: (a) delete the vocab, (b) zero a PAD row and block its gradient, (c) enable TF-IDF. Answer: (b). - Short Answer: How do you load GloVe rows into PyTorch? Answer: nn.Embedding.from_pretrained(...) or copy into .weight.
- True/False:
nn.Embeddingalways produces contextualized vectors by itself. Answer: False—context comes from later layers. - Multiple Choice: Freezing pretrained embeddings means: (a) deleting them, (b) not updating those weights, (c) converting to one-hot. Answer: (b).
- Short Answer: Name one reason embeddings beat raw one-hots in neural nets. Answer: Dense, trainable, memory-efficient similarity structure.
- Short Answer: What Volume 10 topic builds on token embeddings for context? Answer: Attention (and Transformers).
- Multiple Choice: In the sample classifier, pooling happens: (a) before embedding, (b) after embedding over the sequence, (c) only in sklearn. Answer: (b).
- True/False: Word2Vec is an objective; nn.Embedding is a layer that can hold its (or other) vectors. Answer: True.
Key Takeaways
nn.Embeddingis the PyTorch home for learnable token vectors.- It efficiently replaces one-hot × matrix multiplies in the forward pass.
- Warm-start from Word2Vec/GloVe/FastText or train from scratch.
- Module 9.2’s path: sparse counts → static dense → trainable layers.
- Next volume: Attention makes embeddings contextual.
Hands-on idea: Train BagClassifier on a tiny sentiment set; compare random embeddings vs. frozen GloVe init on the same split.
Discussion prompt: Capstone review—for a new text project, when do you stop at TF-IDF, when load FastText, and when train nn.Embedding inside a net?
Recap: The embedding layer turns token IDs into trainable dense vectors and closes Volume 09’s representation module. Continue to Vol. 10 Attention.