Sub topic 11
Multilingual Sentence Embeddings and Cross-Lingual Retrieval
Assume you know Python, Transformer encoders, fine-tuning, and cosine similarity. This chapter focuses on using a pretrained multilingual encoder rather than training one from scratch.
A sentence embedding is a fixed-length vector that represents a sentence’s meaning. A multilingual sentence-embedding model maps sentences from several languages into one shared vector space. Sentences with similar meanings should have nearby vectors, even when they use different languages.
The retrieval problem
Cross-lingual retrieval finds a relevant document in one language for a query written in another. For example, an English user might search a French knowledge base.
The basic process is:
- Encode every document into a vector.
- Encode the query with the same model.
- Compare the query vector with document vectors.
- Return the documents with the highest similarity.
Cosine similarity is commonly used. It measures the angle between two vectors and ranges from -1 to 1. A larger value usually means that the model considers the meanings more similar.
Encoding sentences from different languages
A model such as paraphrase-multilingual-MiniLM-L12-v2 was trained so that translations and paraphrases receive similar vectors. The following example compares an English sentence with its French translation and with an unrelated English sentence.
from sentence_transformers import SentenceTransformer, util
model = SentenceTransformer("paraphrase-multilingual-MiniLM-L12-v2")
sentences = [
"The cat is sleeping on the sofa.",
"Le chat dort sur le canapé.",
"The stock market fell sharply today.",
]
embeddings = model.encode(sentences, convert_to_tensor=True)
same_meaning = util.cos_sim(embeddings[0], embeddings[1]).item()
different_meaning = util.cos_sim(embeddings[0], embeddings[2]).item()
print("English/French similarity:", same_meaning)
print("Unrelated similarity:", different_meaning)The exact scores depend on the model version and runtime. The important comparison is usually whether the translation scores higher than unrelated text.
Building a small multilingual search system
For a real collection, encode documents once and save their vectors. Encode each incoming query at request time. Then use an exact similarity search for small collections or an approximate nearest-neighbor index for large collections.
| Component | Typical choice | Main concern |
|---|---|---|
| Encoder | Multilingual sentence-transformer | Coverage and domain fit |
| Similarity | Cosine similarity | Vector normalization |
| Search index | Exact search or ANN index | Speed versus recall |
| Evaluation | Recall@k and nDCG@k | Per-language performance |
Normalize vectors when your search library expects inner-product similarity to represent cosine similarity. Also keep the model, preprocessing, and language handling identical between indexing and querying.
Common errors and limitations
A shared vector space does not guarantee equal quality for every language. Transfer can weaken when a language had little training data, uses a script poorly represented by the tokenizer, or contains domain-specific terms absent from pretraining.
Do not evaluate only with translated English queries. Test native queries and report results separately by language. A high average score can hide poor retrieval for a low-resource language.
Sentence embeddings also compress an entire sentence into one vector. This is useful for semantic matching, but it can lose exact names, numbers, negation, or fine-grained requirements. For high-stakes search, combine vector retrieval with lexical search and reranking.
Exercise
Why should a multilingual retrieval system use the same encoder for both queries and documents?
Show answer
The encoder places both languages in the same vector space. Using unrelated encoders would make their vector coordinates incomparable unless an additional alignment method were trained.