Zucchetti LLM Course — Lecture 2
From text to tokens to vectors — and how to measure meaning
Kamyar Zeinalipour
Section 1 of 3
The invisible bottleneck most practitioners ignore
LLMs don't see words or characters β they see tokens
Input text:
"Hello, how are you?"
Token IDs:
[9906, 11, 1268, 527, 499, 30]
Used by GPT, LLaMA β starts from characters, merges frequent pairs
Used by BERT, T5 β similar idea, slightly different merge strategy
BPE starts from characters and merges the most frequent pair at each step.
Vocabulary: {l, o, w, e, r, s, t, n}
Step 0: [l, o, w, e, r] (start from characters)
Step 1: [lo, w, e, r] (merge most frequent pair: l+o)
Step 2: [low, e, r] (merge lo+w)
Step 3: [low, er] (merge e+r)
Step 4: [lower] (merge low+er β done!)
π Sennrich et al., "Neural Machine Translation of Rare Words with Subword Units", ACL 2016
BPE is not the only tokenizer. Two other algorithms dominate the landscape.
Used by BERT, DistilBERT, Electra
## prefix for subword continuationsUsed by T5, LLaMA 2, Gemma, Mistral 7B
█ for spaces: █Hello| BPE | WordPiece | SentencePiece | |
|---|---|---|---|
| Merge criterion | Most frequent pair | Max likelihood | BPE or Unigram (configurable) |
| Pre-tokenization | Required (whitespace) | Required (whitespace) | Not required (raw text) |
| Used by | GPT, LLaMA 3 (tiktoken) | BERT, Electra | T5, LLaMA 2, Gemma |
π Schuster & Nakajima, "Japanese and Korean Voice Search", ICASSP 2012 | Kudo & Richardson, "SentencePiece", EMNLP 2018
| Text | Language | Tokens (GPT-4) |
|---|---|---|
| "heart failure" | English | 2 tokens |
| "insufficienza cardiaca" | Italian | 5 tokens |
| "εΏδΈε ¨" | Japanese | 4 tokens |
Tokenizing: "Revenue grew 15.7% this quarter"
β ["Revenue", " grew", " 15", ".", "7", "%", " this", " quarter"]
The number 15.7 is split into 3 separate tokens. The model has no concept of "15.7" as a single number.
See how different models tokenize the same text:
π
Try pasting these and compare the token count:
"The quarterly revenue report shows a 15.7% increase"
"Il rapporto trimestrale mostra un aumento del 15,7%"
Section 2 of 3
Translating meaning into geometry
Think of it like GPS coordinates for meaning
Rome β [41.9, 12.5]
Naples β [40.8, 14.3]
Close coords = close cities
"machine learning" β [0.23, -0.45, ...]
"deep learning" β [0.21, -0.42, ...]
Close vectors = similar meaning
Instead of 2 dimensions, embeddings use hundreds or thousands of dimensions (depending on the model) to capture nuances of meaning.
Similar meaning β similar vectors:
"machine learning" β [0.23, -0.45, 0.12, 0.89, ..., -0.31]
"deep learning" β [0.21, -0.42, 0.15, 0.87, ..., -0.28]
β nearly identical numbers! β
Unrelated meaning β different vectors:
"coffee machine" β [-0.67, 0.33, 0.55, -0.12, ..., 0.71]
β completely different numbers β
The 2013 paper that started the embedding revolution.
king β man + woman β queen
Vector arithmetic captures semantic relationships!
Given a word, predict its neighbors. Words that appear in similar contexts get similar vectors.
π Mikolov et al., "Efficient Estimation of Word Representations in Vector Space", Google 2013 | Pennington et al., "GloVe: Global Vectors", Stanford EMNLP 2014
High-dimensional vectors projected to 2D. Related terms naturally cluster together.
We have vectors. Now how do we compare them? There are 3 main options:
Measures the angle between vectors. Ignores vector length — only cares about direction. Default for most embedding models.
Sensitive to vector magnitude. Longer vectors score higher. Use when length = importance.
L2 distance in vector space. Smaller = more similar (opposite direction from cosine).
We chose cosine. But what does a score of 0.85 actually mean?
0.90+
Near-duplicate
"car" ↔ "automobile"
0.70–0.90
Same topic
"machine learning" ↔ "deep learning"
0.40–0.70
Loosely related
"machine learning" ↔ "stock market"
< 0.40
Unrelated
"car" ↔ "pizza"
| Model | Dimensions | Languages | Best for |
|---|---|---|---|
| all-MiniLM-L6-v2 | 384 | English | Fast, lightweight, English-only tasks |
| multilingual-MiniLM | 384 | 50+ languages | Multilingual, runs locally β |
| BGE-M3 | 1024 | 100+ languages | State-of-the-art multilingual |
| text-embedding-3-large | 3072 | Multilingual | OpenAI API (highest quality) |
paraphrase-multilingual-MiniLM-L12-v2 β it runs locally, supports 50+ languages, and is fast on CPU.Think of it like resolution: 384-dim = "720p", 3072-dim = "4K" β more detail, but more cost.
Section 3 of 3
When your data doesn't speak English
Multilingual models map different languages into a shared vector space
"heart failure" (English)
β same vector as β
"insufficienza cardiaca" (Italian)
General models are trained on Wikipedia, news, books β not on your domain's jargon.
"ERP" and "SAP" β low similarity π
(doesn't know they're related enterprise systems)
"ERP" and "SAP" β high similarity β
(trained on enterprise data, knows the relationship)
| Scenario | Recommended Model | Why |
|---|---|---|
| English-only, fast prototyping | all-MiniLM-L6-v2 |
Fast, lightweight, good enough |
| Multilingual content | multilingual-MiniLM |
50+ languages, runs locally |
| Production, high accuracy | BGE-M3 or OpenAI |
State-of-the-art quality |
| Specialized domain | Fine-tuned model | Trained on your data (Lecture 9) |
Everything from Lecture 2 onward builds on these fundamentals.
Prompting, RAG, Agents, Fine-Tuning β they all depend on embeddings and understanding how LLMs work.
Let’s see everything we learned — step by step in Colab
Open Google Colab → run all cells step by step
lab_01_embeddings.ipynb
Next lecture → Lecture 3: Prompt Engineering
How to communicate effectively with LLMs
Repository: github.com/KamyarZeinalipour/zucchetti-llm-course
Lecture 2 of 10 · Zucchetti LLM Course