Sub topic 10
Sentence Embeddings as Document Representations
Prerequisites and purpose
This chapter assumes that you understand tokenisation, TF-IDF, and the basic idea of a vector embedding. You should also know Python and how to install packages with pip.
A sentence embedding is a numeric vector that represents the meaning of a sentence or document. Texts with similar meanings should have vectors that are close together, even when they use different words. BERTopic uses these vectors to find groups of semantically similar documents.
For example, these texts are likely to have nearby embeddings:
- “The team won the match.”
- “The players celebrated their victory.”
They share few exact words, but both describe a sports victory.
From text to vectors
A sentence-transformer model converts text into a fixed-length vector. The vector may contain hundreds of values, such as:
[0.12, -0.04, 0.77, ..., 0.21]Each individual value usually has no useful human interpretation. The whole vector is useful because distances between vectors represent relationships between texts.
| Representation | Main signal | Typical weakness |
|---|---|---|
| Bag of words | Shared word counts | Misses word order and synonyms |
| TF-IDF | Important words | Still depends on exact words |
| Sentence embedding | Learned semantic relationships | Can reflect model bias or miss domain terms |
A bag-of-words representation treats “car” and “automobile” as unrelated unless preprocessing connects them. A sentence-embedding model can often place them near each other because it learned from many examples of language use.
Creating embeddings with Sentence Transformers
The following example creates embeddings and compares two texts with cosine similarity. Cosine similarity measures the angle between two vectors. A value near 1 means that the vectors point in similar directions.
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity
documents = [
"The football team won the final match.",
"The players celebrated their victory in the championship.",
"The government introduced a new tax policy.",
]
model = SentenceTransformer("all-MiniLM-L6-v2")
embeddings = model.encode(documents, normalize_embeddings=True)
similarity = cosine_similarity(
embeddings[0:1],
embeddings[1:2],
)[0, 0]
print("Embedding shape:", embeddings.shape)
print("Sports-document similarity:", round(float(similarity), 3))all-MiniLM-L6-v2 is a compact general-purpose model. Larger models can produce better results but require more memory and computation. A domain-specific model may work better for medical, legal, or scientific text.
How BERTopic uses them
BERTopic first creates one embedding for each document. It then reduces the vectors to make their structure easier to cluster. A clustering algorithm groups nearby documents. Later, BERTopic uses the original text to describe each group with class-based TF-IDF.
Practical limits
Embeddings are not automatically correct measures of meaning.
- Very long documents may lose detail because the model has an input-length limit.
- A general model may not understand specialised vocabulary.
- Similar writing style can make unrelated documents appear close.
- Different languages may need a multilingual model.
- Normalising vectors affects how distance calculations behave.
For long documents, split the text into sections, embed the sections, and combine their results carefully. Do not assume that clustering quality improves merely because the vectors have more dimensions.
Exercise
Why can sentence embeddings place “car” and “automobile” near each other when bag-of-words vectors may not?
Show answer
Sentence-embedding models learn semantic relationships from language-use examples, while bag-of-words vectors mainly record whether exact terms appear.