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

One-Hot Encoding (NLP)

Before embeddings, the most obvious way to represent a word numerically was one-hot encoding โ€” a vector as long as the entire vocabulary, all zeros except a single 1 marking that specific word. This note explains exactly why this approach was abandoned in favor of dense embeddings.

Formula

\[ \text{one-hot}(\text{"cat"}) = [0, 0, \ldots, 1, \ldots, 0] \in \mathbb{R}^{|V|} \]

\(|V|\) is the vocabulary size. Every word gets its own vector, as long as the entire vocabulary, with a 1 at exactly its own index and 0 everywhere else.

The Two Fatal Problems

ProblemDetail
Extreme dimensionalityFor a 50,000-word vocabulary, every single word is a 50,000-dimensional vector โ€” almost entirely zeros, wasting enormous memory and compute
No notion of similarityAny two distinct one-hot vectors have a dot product of exactly 0 โ€” "cat" and "dog" are represented as exactly as "similar" (i.e. not at all) as "cat" and "asteroid"

Numerical Example โ€” Verifying the Similarity Problem

For a tiny 4-word vocabulary {cat, dog, car, run}: one-hot("cat")\(=[1,0,0,0]\), one-hot("dog")\(=[0,1,0,0]\).

\[ \text{one-hot}(\text{cat}) \cdot \text{one-hot}(\text{dog}) = (1)(0)+(0)(1)+(0)(0)+(0)(0) = 0 \]

Despite "cat" and "dog" being semantically related (both animals), their one-hot vectors are exactly as dissimilar (dot product 0) as "cat" and "car," or "cat" and "run" โ€” one-hot encoding has no mechanism for encoding this kind of similarity at all, since every pair of distinct words is equally "orthogonal."

Code

import torch

vocab = {"cat": 0, "dog": 1, "car": 2, "run": 3}
vocab_size = len(vocab)

def one_hot(word):
    vec = torch.zeros(vocab_size)
    vec[vocab[word]] = 1
    return vec

cat_vec = one_hot("cat")
dog_vec = one_hot("dog")
print(torch.dot(cat_vec, dog_vec))   # tensor(0.) -- no similarity captured at all

What This Directly Motivates

Both problems point toward the same fix: replace this huge, sparse, meaningless representation with a much smaller, dense vector โ€” where the actual numeric values are learned such that semantically similar words end up with similar vectors. This is exactly what Word Embeddings, the next note, provides.

Common Mistakes

  • Assuming one-hot encoding is purely a historical curiosity with no modern relevance โ€” it's still the conceptual starting point that motivates embeddings, and one-hot representations still appear directly as the target format in classification tasks (e.g. one-hot encoded labels in categorical cross-entropy, from Categorical Cross-Entropy), just not typically as the input representation for words anymore.

Interview Relevance

Q: "What are the two main problems with one-hot encoding words, and how do embeddings solve them?" One-hot vectors are extremely high-dimensional and sparse (one dimension per vocabulary word, mostly zeros), wasting memory and compute; and they have no notion of similarity โ€” every pair of distinct words is equally "orthogonal," regardless of actual meaning. Embeddings solve both by using much lower-dimensional, dense vectors whose values are learned so that semantically similar words end up close together in the embedding space.

Practice Question

For a vocabulary of 100,000 words, how many dimensions would a one-hot vector need? How does that compare to a typical embedding dimension of 300?

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

One-Hot Encoding (NLP) โ€“ FAQs

Quick answers about learning One-Hot Encoding (NLP) in Deep Learning.

This free note from CodingNow 2.0 explains One-Hot Encoding (NLP) 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 One-Hot Encoding (NLP), is 100% free with no signup required.
With focused practice, most students grasp One-Hot Encoding (NLP) 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