Skip to content

Representation Learning & Embeddings

Turn arbitrary IDs into learned vectors so similar things point in similar directions

Quick recap
  • A one-hot vector identifies a word but says nothing about what it means.
  • An embedding is a learned dense vector, usually read from a lookup table.
  • Cosine similarity compares directions; analogies are approximate vector arithmetic.

This lesson follows Convolutional Neural Networks and leads into Predicting the Next Token. For the geometry, review Linear Algebra.

1. The ID problem

Which pair of words is more similar: cat and dog, or cat and apple? A person answers immediately. An ID cannot: it is just an index into a vocabulary.

The standard way to feed an ID to a linear model is a one-hot vector. It has one 1 at the word’s position and 0 everywhere else. The vectors are long, and every pair of different words has dot product 0:

One-hot encoding

A one-hot vector is a perfectly good identifier. It is a poor representation of meaning. It cannot tell the model that cat and dog appear in similar contexts, and it grows with the vocabulary.

Try it

What is the dot product of two different one-hot vectors, and why is that a problem?

2. A vector per word

An embedding replaces the one-hot vector with a short, dense vector of learned numbers. The model stores a matrix with one row per vocabulary item and looks up the row for a token:

Embedding lookup

We will use two dimensions so the picture fits on a page. Real embeddings often use 50 to several thousand dimensions; the geometry is the same, but we can no longer draw it.

wordtoy vector
man(1, 0)
woman(0, 1)
king(2, 1)
queen(1, 2)
cat(0.2, 0.9)
dog(0.3, 0.8)

The vectors are not hand-written in a real system. They start random and are updated by gradient descent while the model learns a task. The next section makes the geometry useful.

3. Similarity

The dot product measures how much two vectors agree. Cosine similarity divides it by both lengths, so only the angle matters:

Cosine similarity

The value is 1 for the same direction, 0 for perpendicular vectors, and −1 for opposite directions. In the toy space, king and queen score 0.80, while cat and dog score 0.99:

King and queen
Cat and dog

A high cosine score says the vectors point in a similar direction. It does not say the words are interchangeable, true, or safe. Similarity is a property of the learned representation, not a dictionary definition.

4. Worked analogy

Vector arithmetic is the famous demonstration. Take king, subtract man, and add woman. In this toy space the result lands exactly on queen:

The analogy

Worked example: king − man + woman

quantityvector
king(2, 1)
man(1, 0)
woman(0, 1)
king − man(1, 1)
(king − man) + woman(1, 2)
queen(1, 2)

The target vector is exactly queen, so the cosine similarity is 1.00 and the nearest neighbor is queen. The closest non-input words in this toy space are dog and cat, which shows why a toy geometry can produce accidental neighbors.

In a real embedding, the arithmetic usually does not land exactly on a word. The method ranks all vocabulary vectors by cosine similarity and returns the nearest one. It is a useful demonstration, not a guaranteed rule.

The result is approximate in practice, and it can inherit stereotypes from the training text. “King − man + woman ≈ queen” is a famous example, not a law of language.

5. Where they come from

Word2vec’s skip-gram model trains a small predictor to tell whether a word appears near another word. The embedding matrix is a by-product of that prediction task. Words that appear in similar contexts get pushed toward similar vectors because they help the predictor in similar ways.

Modern language models learn embeddings as the first layer of the network. The same lookup table is trained end-to-end with next-token prediction. Recommendation systems use the same idea for users and items: an embedding is a learned ID representation, and similarity can drive retrieval.

There are two broad kinds. A static embedding gives one vector per word, no matter the sentence. A contextual embedding is produced by a model that reads the whole context, so “bank” in river bank and bank account can get different vectors.

Try it

Why can a contextual model give the same word two different vectors?

6. Limits and context

Embedding dimensions are usually not individually interpretable. A direction may mix gender, topic, syntax, sentiment, and dataset artifacts. Reading one coordinate as “royalty” is tempting and usually wrong.

Similarity can encode bias. If the training text associates a profession with one gender, the geometry can reproduce that association. Removing a word from the vocabulary does not remove the direction from the space.

Two-dimensional pictures are also misleading. t-SNE and UMAP compress high-dimensional neighborhoods into two dimensions and can create clusters that are not there. A pretty picture is not a measurement.

Finally, a vector is not a definition. It is a learned summary of co-occurrence and task pressure. The next lesson uses these vectors as the input to a model that predicts the next token.

Next on the recommended path: Predicting the Next Token

7. Going deeper

Skip-gram with negative sampling turns a softmax over the whole vocabulary into a binary classification problem: real context pairs versus random negative pairs. This makes training practical for large vocabularies. Matrix factorization methods such as GloVe reach a related objective from co-occurrence counts.

Embedding matrices are often tied to the output layer, so the same vectors are used to read a token and to score it as a prediction. Normalizing vectors before cosine similarity removes the effect of length; some retrieval systems do this, and some do not.

Contextual embeddings blur the line between “word vector” and “sentence representation.” A transformer’s hidden state for a token is not a fixed dictionary entry. It is a vector produced by attention over the current context, which is exactly where the next lessons go.

8. Practice

Try it

Two vectors have cosine similarity 0.99. What can you conclude, and what can you not conclude?

Try it

Why does a lookup table work as an embedding layer?

Try it

Does king − man + woman always return queen?

9. Resources

The MIT chapter connects representation learning to prediction tasks. The Illustrated Word2vec post gives the visual tour of similarity, analogies, and training. Both links were opened and checked for this lesson.

Continue the track

The mathematics behind this topic: Linear algebra.

All Machine Learning lessons

Site soundtrack