๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #261

Word Embeddings

Word embeddings replace one-hot encoding's huge, meaningless vectors with much smaller, dense vectors โ€” learned such that words with similar meanings end up close together in the embedding space, restoring exactly the notion of similarity one-hot encoding lacked.

The Core Idea

Instead of a vector as long as the entire vocabulary, each word gets a dense vector of a fixed, much smaller size (commonly 100โ€“300 dimensions) โ€” every value in this vector is a learned parameter, adjusted during training so that words used in similar contexts end up with similar vectors, following the "distributional hypothesis": words that appear in similar contexts tend to have similar meanings.

The Famous Analogy Property

\[ \text{vec}(\text{"king"}) - \text{vec}(\text{"man"}) + \text{vec}(\text{"woman"}) \approx \text{vec}(\text{"queen"}) \]

Well-trained embeddings, remarkably, capture semantic relationships as consistent directions in the embedding space โ€” the vector difference between "king" and "man" ends up pointing in a similar direction as the difference between "queen" and "woman" (both roughly encoding "male → female"), letting simple vector arithmetic solve word analogies. This emergent property was one of the most striking early demonstrations of what learned embeddings capture.

How Embeddings Are Implemented

\[ \mathbf{e}_{\text{word}} = \mathbf{E}[\text{token\_id}] \]

\(\mathbf{E}\) is an embedding matrix of shape \((\text{vocab\_size}, d_{\text{embed}})\) โ€” essentially a lookup table. Looking up a word's embedding is simply indexing into this matrix at that word's token id row. \(\mathbf{E}\) itself is a learnable parameter, updated via backpropagation exactly like any other weight matrix.

Code

import torch
import torch.nn as nn

embedding_layer = nn.Embedding(num_embeddings=10000, embedding_dim=300)

token_ids = torch.tensor([42, 7, 891])   # 3 words, represented as their vocabulary indices
embeddings = embedding_layer(token_ids)
print(embeddings.shape)   # (3, 300) -- one dense 300-dim vector per word

# Compare to one-hot: same 3 words would need (3, 10000) -- one-hot is over 30x larger here

Two Ways to Get Embeddings

ApproachDetail
Train from scratchThe embedding matrix starts randomly initialized and is learned entirely from your specific task's training data
Use pretrained embeddingsLoad embeddings already trained on a huge external corpus (Word2Vec, GloVe โ€” the next few notes), often as a strong starting point, sometimes frozen, sometimes further fine-tuned

Common Mistakes

  • Training an embedding layer from scratch on a very small dataset โ€” with limited data, the embedding matrix may never see enough examples of many words to learn meaningful representations; pretrained embeddings are usually a better starting point in this case.
  • Forgetting that nn.Embedding is just a lookup table with learnable parameters โ€” it's mathematically equivalent to multiplying a one-hot vector by the embedding matrix, but implemented much more efficiently as a direct index lookup instead of an actual matrix multiplication.

Interview Relevance

Q: "What does it mean for word embeddings to capture semantic similarity, and how is that actually achieved?" Words used in similar contexts across a large training corpus end up with similar embedding vectors โ€” this happens automatically during training because the embedding matrix's parameters are updated via backpropagation to make the model's predictions (whatever the specific training objective is โ€” predicting a word from context, or vice versa) more accurate, and words that behave similarly in context end up needing similar vector representations to support similar predictions.

Practice Question

Mathematically, why is nn.Embedding(vocab_size, embed_dim) equivalent to multiplying a one-hot vector by a \((\text{vocab\_size}, \text{embed\_dim})\) matrix, even though it's implemented as a direct lookup rather than a matrix multiplication?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Word Embeddings โ€“ FAQs

Quick answers about learning Word Embeddings in Deep Learning.

This free note from CodingNow 2.0 explains Word Embeddings in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Word Embeddings, is 100% free with no signup required.
With focused practice, most students grasp Word Embeddings in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now