Embeddings
Turning meaningless IDs into dense vectors that can learn meaning.
By the end of this chapter, you'll be able to:
- Explain why a raw token ID can't represent meaning
- Build a random embedding matrix and look up a token's vector
- Understand what 'dimension' means for an embedding, and why it's chosen
The previous chapter ended on a warning: token IDs are just addresses, not meaning. An embedding is what turns that meaningless address into something a network can actually learn from — a vector of real numbers.
From one ID to many numbers
Instead of representing the token "ai" as the single integer 0, an embedding
represents it as a list of numbers — say, 4 of them: [0.12, -0.87, 0.33, 0.05].
Every token in the vocabulary gets its own vector, all stored together in one
embedding matrix: one row per vocabulary entry, one column per dimension.
import random
class Embedding:
def __init__(self, vocab_size, embedding_dimension):
self.vocab_size = vocab_size
self.embedding_dimension = embedding_dimension
self.weights = self.create_matrix()
def create_matrix(self):
matrix = []
for _ in range(self.vocab_size):
vector = [
random.uniform(-1, 1)
for _ in range(self.embedding_dimension)
]
matrix.append(vector)
return matrix
def get_vector(self, token_id):
return self.weights[token_id]embedding = Embedding(vocab_size=3, embedding_dimension=4)
print(embedding.weights)
# [[ 0.12, -0.87, 0.33, 0.05],
# [-0.44, 0.21, -0.19, 0.67],
# [ 0.08, 0.91, -0.52, -0.31]]
print(embedding.get_vector(0))
# [0.12, -0.87, 0.33, 0.05]| d1 | d2 | d3 | d4 | |
|---|---|---|---|---|
| ID 0 | 0.12 | -0.87 | 0.33 | 0.05 |
| ID 1 | -0.44 | 0.21 | -0.19 | 0.67 |
| ID 2 | 0.08 | 0.91 | -0.52 | -0.31 |
embedding.get_vector(0) returns row 0 — highlighted above.
get_vector(0) does the same job id_to_token[0] did in the previous
chapter — it's still a lookup by ID — but what comes back is a vector, not a
single arbitrary number.
Why random?
Every vector here starts as pure noise: random.uniform(-1, 1) for every dimension of
every token. That's intentional. At this point, the model knows nothing — the embedding
matrix is just scaffolding waiting to be shaped by training. The next chapter is
entirely about how that reshaping actually happens.
What "dimension" buys you
With only 1 dimension per token, an embedding is barely more expressive than the raw ID. With 4, as above, a token already has 4 independent "directions" it could vary along. Real models use far more — often several hundred to a few thousand dimensions per token — giving the training process enough room to encode subtle distinctions: tense, sentiment, topic, formality, and relationships between words that were never explicitly labeled.
Check your understanding
1. What does embedding.get_vector(0) return in the example above?
2. Immediately after initialization (before any training), what do two semantically similar tokens' vectors look like relative to each other?
Try it yourself
These are SIGNAL's real learned embeddings, squashed from 96 dimensions onto a 2D map. Pick a word to see its nearest neighbours.
Try it — Learning Lab
Embedding Space
SIGNAL's 96-dimensional word vectors, squashed onto a 2D map. Pick a word to see its nearest neighbours by cosine similarity.
The question:How does a model know that two words mean similar things?
Loading SIGNAL's model (about 650 KB, once)…