Sub topic 2
What an embedding is: text as a point in vector space
This chapter assumes you know basic Python, what a dot product and a vector length are, and roughly what a neural network does. It builds the core idea that the rest of the tutorial relies on.
From words to numbers
Keyword search compares strings. Two texts match only if they share words. A computer cannot compare meaning directly, so we need a way to turn text into numbers that capture meaning.
An embedding is a list of floating-point numbers, a vector, that represents a piece of text. A model produces it. The model is trained so that texts with similar meaning get vectors that are close together, and texts with unrelated meaning get vectors that are far apart.
You can picture each text as a point in a space. A 2D space is a sheet of paper with an x and y coordinate. Real embeddings use many more coordinates, often 384, 768, or 1024. The idea is the same: each text is a point, and distance between points reflects difference in meaning.
What the dimensions mean
Each coordinate, or dimension, is not a human-readable feature. No single number means "is about passwords." Meaning is spread across all dimensions together. Individual values are only useful in comparison with the vectors of other texts.
Two properties matter in practice:
- Fixed size. Every input, whether a word or a paragraph, becomes a vector of the same length for a given model.
- Model-specific. Vectors from different models live in different spaces. You cannot compare a vector from one model with a vector from another, even if the sizes match.
Seeing an embedding
The sentence-transformers library loads a pretrained model and encodes text in one call.
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("all-MiniLM-L6-v2")
sentences = [
"How do I reset my password?",
"Steps to recover access to your account",
"The weather is sunny today",
]
embeddings = model.encode(sentences, normalize_embeddings=True)
print(embeddings.shape) # (3, 384)
print(embeddings[0][:5]) # first five numbers of the first vectorThe shape (3, 384) means three sentences, each with 384 numbers. The first five values look like arbitrary small decimals. That is expected, since no single dimension has a readable meaning.
The argument normalize_embeddings=True scales each vector to length 1. This makes later comparisons simpler, and the next chapter explains why.
Why closeness means similarity
The model is trained on many pairs of texts that are known to be related, such as a question and its answer. Training pushes the vectors of related pairs together and pushes unrelated ones apart. A later chapter covers this in detail. For now, treat the model as a function that has learned a useful layout of meaning.
This is why the password question and the account recovery sentence land near each other even though they share almost no words. Their meaning overlaps, and the layout reflects that.
Common errors
- Mixing models. Embedding documents with one model and queries with another gives meaningless distances.
- Reading single dimensions. Do not try to interpret one coordinate on its own.
- Assuming closeness is always meaning. Models also pick up topic, style, and language. Two sentences with opposite claims on the same topic can still be close.
Exercise
You embed 1,000 product descriptions with a model that outputs 768-dimensional vectors. What is the shape of the resulting array, and can you compare these vectors with query vectors made by a different 384-dimensional model?
Show answer
The shape is (1000, 768). You cannot compare them. The sizes differ, and even with matching sizes, vectors from different models live in different spaces, so distances between them have no meaning.
Exercise
Two sentences, "The drug is effective" and "The drug is not effective," get very close embeddings. Why can this happen, and what does it tell you about embeddings?
Show answer
Embeddings capture overall topic and context, and both sentences share nearly all their words and subject. Closeness reflects similarity of content, not agreement in meaning, so negation and opposite claims can be missed.