Sub topic 1
Why One-Hot Vectors Are Not Enough
Prerequisites
This chapter assumes that you know basic Python and NumPy, vectors, dot products, and norms. You should also know that a token is a unit of text, such as a word, and that a vocabulary is the set of tokens used by a model.
One-hot vectors are not enough
A one-hot vector represents a token with one position for each vocabulary item. Every position is zero except the position assigned to that token.
For a vocabulary containing cat, dog, and car, we might use:
| Token | One-hot vector |
|---|---|
| cat | `[1, 0, 0]` |
| dog | `[0, 1, 0]` |
| car | `[0, 0, 1]` |
The vector length equals the vocabulary size. A vocabulary with 100,000 tokens requires vectors with 100,000 components. These vectors are sparse, which means that most components are zero.
One-hot vectors also treat every pair of different words as equally unrelated. The vectors for cat and dog have a dot product of zero, just like the vectors for cat and car. The representation does not express that cats and dogs are both animals or that cars are vehicles.
This creates two problems:
- The vectors can be very large and mostly empty.
- Their geometry contains no useful word meaning.
A model could learn relationships from other features, but those relationships are not present in the one-hot representation itself. We want a compact representation in which similar words have nearby vectors.
You shall know a word by the company it keeps
The distributional hypothesis is the idea that a word's meaning can be inferred from the words that commonly appear near it. In other words, you shall know a word by the company it keeps.
Consider these sentences:
The cat chased the mouse.The dog chased the mouse.
The words cat and dog occur in similar contexts. A learning algorithm can therefore give them similar representations, even if it never receives an explicit label saying that both are animals.
A context window is a fixed number of nearby tokens used as evidence. With a window size of two, the word chased in the cat chased the mouse is associated with nearby words such as cat, the, and mouse, depending on the exact window convention.
A context-based model does not store a single manually written definition. Instead, it learns numerical features from many examples. These features are dense: most components have nonzero values. The resulting vector can encode several patterns, such as topics, grammatical roles, and associations.
One-hot vectors identify tokens. Distributional representations describe how tokens behave. The next chapters show how word2vec and GloVe turn this idea into trainable dense embeddings.
Exercise
Why do one-hot vectors for cat and dog fail to show that the words are related?
Show answer
Their vectors are orthogonal, so their dot product is zero. One-hot encoding records identity, not relationships learned from shared contexts.