Lecture: 1 and 2, The Transformer Blueprint; Tokenization, Embeddings and Similarity Duration: 35 minutes Difficulty: Beginner
By the end of this lab you will:
| Requirement | How to Check |
|---|---|
| Python 3.10+ | python --version |
| Dependencies installed | pip install sentence-transformers numpy scikit-learn rich |
No API key needed — this lab runs 100% locally using sentence-transformers.
cd lab_01_embeddings
python lab_01_embeddings.py
| File | Description |
|---|---|
lab_01_embeddings.py |
Main lab script with TODO sections for you to complete |
lab_01_embeddings.ipynb |
Jupyter notebook version with explanations |
solutions.py |
Complete solutions (don’t peek until you’ve tried!) |
data/terms_it.json |
20 categorized terms with Italian/English translations |
The script loads 20 terms across 5 categories (with Italian and English translations).
No code changes needed — just observe the output.
The script generates embeddings using paraphrase-multilingual-MiniLM-L12-v2. Each term becomes a 384-dimensional vector.
No code changes needed — observe the embedding shape.
Your turn! Find the compute_similarity_matrix() function and implement it.
Hint: You can use scikit-learn:
from sklearn.metrics.pairwise import cosine_similarity
return cosine_similarity(embeddings)
Or implement it manually with numpy:
norms = np.linalg.norm(embeddings, axis=1, keepdims=True)
normalized = embeddings / norms
return np.dot(normalized, normalized.T)
Your turn! Find the find_top_similar_pairs() function and implement the pair-finding logic.
Hint: Loop over the upper triangle of the similarity matrix (where i < j), collect all pairs with their scores, sort by score, and return the top K.
Look at the output:
Uncomment the compare_models() call in main() to compare:
paraphrase-multilingual-MiniLM-L12-v2 (multilingual)all-MiniLM-L6-v2 (English-only)Which model produces better clusters?
When implemented correctly, you should see:
Example output:
Top 10 Most Similar Term Pairs
┌───┬────────────────────┬────────────────────┬────────────┬────────────────┐
│ # │ Term 1 (IT) │ Term 2 (IT) │ Similarity │ Same Category? │
├───┼────────────────────┼────────────────────┼────────────┼────────────────┤
│ 1 │ paracetamolo │ ibuprofene │ 0.82 │ ✓ Yes │
│ 2 │ risonanza magnetica│ tomografia comput. │ 0.79 │ ✓ Yes │
│ 3 │ diabete mellito │ glicemia a digiuno │ 0.71 │ ✗ No │
│...│ ... │ ... │ ... │ ... │
└───┴────────────────────┴────────────────────┴────────────┴────────────────┘
Observation: 8/10 of the most similar pairs are in the same category
| Issue | Solution |
|---|---|
ModuleNotFoundError: sentence_transformers |
pip install sentence-transformers |
| Model download slow | First run downloads ~120MB model. Be patient. |
| Memory error | Close other applications. The model needs ~500MB RAM. |
| All zeros in similarity matrix | You need to implement the TODO in Step 3! |