Module 1: Introduction to Vectors

6. Why Vectors in AI: From Words to Geometry

Capsule overview

In this capsule you're going to understand why AI systems use vectors to represent text. So far you've seen WHAT vectors are (direction + magnitude), HOW to visualize them (2D/3D/high dimensions) and HOW to operate on them (addition, subtraction, scaling). Now you'll see WHY all of that matters in AI: how to turn words into vectors, why nearby vectors capture similar meaning, and how that enables semantic search and RAG.

This is the capsule that connects geometry to language. When you finish, you'll understand the "distributional hypothesis" (words used in similar contexts have similar meanings), why embeddings capture that intuition, and how semantic search uses vector geometry to search for meaning instead of just exact words. This intuition is the foundation of the ENTIRE modern AI stack: LLMs, RAG, vector databases, semantic search.


The problem: How do you represent meaning?

Problem 1: Text as strings

If you represent text as strings (character sequences), you can't perform mathematical operations:

"dog" + "cat" = ❓ (not defined)
distance("dog", "cat") = ❓ (there's no natural way to measure it)
"dog" vs "automobile" = ❓ (you can't compare them quantitatively)

Consequence: You can't search for "similar texts" automatically. You can only do exact search (keyword search): "dog" ≠ "hound" ≠ "pet", even though they mean related things.


Problem 2: One-hot encoding (the traditional representation)

In classic ML, words were represented with one-hot encoding: a vector with a 1 at the word's position and 0 everywhere else.

Example (a vocabulary of 5 words):

"dog" = [1, 0, 0, 0, 0]
"cat" = [0, 1, 0, 0, 0]
"car" = [0, 0, 1, 0, 0]
"house" = [0, 0, 0, 1, 0]
"tree" = [0, 0, 0, 0, 1]

Problems:

1. All the words are equally distant:

distance("dog", "cat") = √2
distance("dog", "car") = √2

There's no way to capture the fact that "dog" and "cat" are more similar than "dog" and "car".


2. High dimensionality:

If your vocabulary has 50,000 words, every vector has 50,000 dimensions (nearly all of them 0). Very inefficient.


3. It doesn't capture meaning:

The vectors are orthogonal (perpendicular): there's no geometric relationship between "dog" and "cat", even though they're conceptually related.


The solution: Dense embeddings

Instead of representing words with sparse vectors (mostly 0s) like one-hot, modern embeddings are dense vectors (every value is a non-zero real number) of lower dimensionality (300D-1536D), where:

  • Similar words have nearby vectors
  • Different words have distant vectors
  • Semantic relationships are captured by directions (e.g. gender, size, time)

Example (simplified to 2D for illustration):

      ↑
      |
    2 | •"cat" •"dog"  ← Domestic animals (close together)
      |
    1 |
      |
    0 |─────────────────→
      |            •"car"  ← An object (far from the animals)
   -2 |

Now it works:

  • distance("dog", "cat") = small → similar
  • distance("dog", "car") = large → different

This is the heart of semantic search: representing text as vectors so that the geometry (closeness) captures meaning (semantic similarity).


The distributional hypothesis: "You are the company you keep"

The distributional hypothesis (Firth, 1957):

"You shall know a word by the company it keeps."

In practical terms: Words that appear in similar contexts have similar meanings.

Example:

Sentences with "dog":

  • "The dog barks in the garden."
  • "My dog is very playful."
  • "Dogs are loyal animals."

Sentences with "cat":

  • "The cat meows in the garden."
  • "My cat is very independent."
  • "Cats are curious animals."

Observation: "dog" and "cat" appear in very similar contexts:

  • As the subjects of sentences about animal behavior
  • Accompanied by verbs related to sounds or actions
  • In contexts about pets and homes

Conclusion: "dog" and "cat" have related meanings (both are domestic animals).


Contrast with "automobile":

Sentences with "automobile":

  • "The automobile accelerates on the highway."
  • "My automobile needs gasoline."
  • "Automobiles are means of transportation."

Very different contexts from "dog" and "cat":

  • Verbs: accelerates, needs gasoline (not biological)
  • Topics: transportation, mechanics (not animal life)

Conclusion: "automobile" has a different meaning from "dog" and "cat".


How this translates into vectors:

Embedding models (Word2Vec, GloVe, BERT, GPT, etc.) learn vectors such that:

  • Words with similar contexts → nearby vectors
  • Words with different contexts → distant vectors

Result: The geometry of the vector space reflects the semantics of the language.

Analogy: If two people have friends in common, they have similar "social contexts" → they probably have similar interests. If one person only has friends in the sports world and another only in the academic world, their "social contexts" are different → different interests. Embeddings do the same with words: "friends in common" = "words that appear in similar contexts".


How embeddings are trained

Note: We're not going to train embeddings in this guide (that requires code and a lot of data). You'll only see the conceptual intuition of how models learn vectors.


The classic method: Word2Vec (Mikolov et al., 2013)

Idea: Train a neural network to predict words in context.

Two variants:

1. Skip-gram: Given a word, predict the words around it (the context).

Example:

Sentence: "The dog runs fast"

Task: Given "dog", predict ["the", "runs", "fast"]

Training:

  • The network learns a vector for "dog"
  • It adjusts the vector so it can correctly predict the context words
  • After millions of examples, words with similar contexts end up with similar vectors

2. CBOW (Continuous Bag of Words): Given the context, predict the central word.

Example:

Sentence: "The [?] runs fast"

Task: Given ["the", "runs", "fast"], predict "dog"

Result: The same intuition as skip-gram; words that are interchangeable in similar contexts end up with nearby vectors.


The modern method: Transformers (BERT, GPT, etc.)

Models like BERT and GPT learn contextual embeddings: a word's vector changes according to the context.

Example:

"The bank is near the river" → "bank" (riverbank)
"I went to the bank to withdraw money" → "bank" (financial institution)

In contextual embeddings, "bank" has different vectors in each sentence (different context).

Advantage: It captures polysemy (words with multiple meanings).

How they're trained: Tasks like "predict the masked words" or "predict the next word". After training on billions of words, the model learns vector representations that capture contextual meaning.


You don't need to understand all the technical details. Just remember:

  • Embeddings are trained on large amounts of text
  • The model learns vectors where words with similar contexts end up close together
  • The result is a geometric space where closeness = semantic similarity

Why nearby vectors = similar meanings

After training (millions of text examples), the model has adjusted the vectors so that:

1. Synonyms are close together:

Vector("dog") ≈ Vector("hound")
Vector("car") ≈ Vector("automobile") ≈ Vector("vehicle")

2. Related words (the same domain) are close together:

Vector("dog") is near Vector("cat") is near Vector("pet")
Vector("king") is near Vector("queen") is near Vector("monarch")

3. Unrelated words are far apart:

Vector("dog") is far from Vector("car")
Vector("king") is far from Vector("chair")

4. Semantic relationships are captured by directions:

Vector("king") - Vector("man") + Vector("woman") ≈ Vector("queen")

Interpretation: The "gender direction" is captured by the geometry

Why this works: During training, the model saw millions of sentences like:

  • "The king rules the kingdom"
  • "The queen rules the kingdom"
  • "The man works in the factory"
  • "The woman works in the factory"

The model's conclusion: "king" and "queen" appear in very similar contexts (ruling a kingdom); the main difference is gender (king ↔ man, queen ↔ woman). That difference is captured by a direction in the vector space (a gender vector). That's why the operation king - man + woman works: you're "removing the masculine gender" and "adding the feminine gender".


From keywords to semantics: The fundamental leap

Keyword search (traditional search):

How it works: Search for exact word matches (or with basic stemming/lemmatization).

Example:

Query: "dog"

Documents:

  • Doc 1: "The dog is a domestic animal" → ✅ Match (contains "dog")
  • Doc 2: "The hound is a domestic animal" → ❌ No match (doesn't contain "dog")
  • Doc 3: "Cats are pets" → ❌ No match (doesn't contain "dog")

Problems:

  • It doesn't find synonyms ("hound")
  • It doesn't find related concepts ("pet", "cat")
  • It only searches for exact words

Semantic search:

How it works: Convert the query and the documents into vectors, then search for nearby vectors (geometric similarity).

Example:

Query: "dog" → Vector query = [0.23, -0.45, ..., -0.34] (1536D)

Documents:

  • Doc 1: "The dog is a domestic animal" → Vector doc1 = [0.23, -0.45, ..., -0.34]
  • Doc 2: "The hound is a domestic animal" → Vector doc2 = [0.24, -0.44, ..., -0.33]
  • Doc 3: "Cats are pets" → Vector doc3 = [0.25, -0.43, ..., -0.32]
  • Doc 4: "Cars have wheels" → Vector doc4 = [9.34, 5.21, ..., 7.56]

Similarity (cosine). The values are illustrative: they're meant to show the relative ordering, not real measurements from a specific model.

  • similarity(query, doc1) ≈ very high (contains "dog")
  • similarity(query, doc2) ≈ high ("hound" is a synonym of "dog")
  • similarity(query, doc3) ≈ medium-high ("cat" is a related concept)
  • similarity(query, doc4) ≈ low ("car" isn't related)

Result: It returns Doc1, Doc2, Doc3 (all related to "dog") even though Doc2 and Doc3 don't contain the word "dog".

Advantages:

  • ✅ It finds synonyms ("hound")
  • ✅ It finds related concepts ("cat", "pet")
  • ✅ It understands meaning, not just exact words

This is the leap from keywords to semantics: instead of searching for words, you search for meanings using vector geometry.


Practical applications in AI Engineering

Now that you understand why vectors capture meaning, you can see how they're used in products:

1. Semantic search

Use case: An internal documentation search engine.

Flow:

  1. The user asks: "How do I configure authentication?"
  2. Query → embedding (1536D)
  3. Search for documents with nearby embeddings
  4. Return the top-5 most relevant documents (even if they don't contain the exact words)

Why it works: "configure authentication" has a vector close to documents about "auth setup", "login configuration", "OAuth setup", even though they don't use the same words.


2. RAG (Retrieval-Augmented Generation)

Use case: A chatbot over a company knowledge base.

Flow:

  1. The user asks: "What benefits does the premium plan have?"
  2. Query → embedding
  3. Search for relevant documents in a vector database (semantic search)
  4. Pass the documents + the query to an LLM (GPT-4, Claude)
  5. The LLM generates an answer based on the retrieved documents

Why it works: Semantic search finds relevant documents (even if they don't mention "benefits" or "premium" verbatim); the LLM uses those documents to generate a precise, contextual answer.


3. Recommendations

Use case: Suggesting related articles.

Flow:

  1. The user reads the article "Introduction to vectors"
  2. Article → embedding
  3. Search for articles with nearby embeddings
  4. Suggest: "Vector spaces", "Cosine similarity", "Embeddings in AI"

Why it works: Articles on related topics have nearby embeddings (similar contexts).


4. Duplicate detection

Use case: Detecting duplicate support tickets.

Flow:

  1. A new ticket: "I can't log in"
  2. Ticket → embedding
  3. Search for tickets with very close embeddings (similarity > 0.95)
  4. Alert: "A similar ticket already exists: 'Problem with authentication'"

Why it works: Sentences with the same meaning (even if worded differently) have nearby embeddings.


5. Semantic clustering

Use case: Grouping customer feedback by topic.

Flow:

  1. Each piece of feedback → embedding
  2. Clustering (k-means, DBSCAN) in the vector space
  3. Result: Groups of similar feedback (e.g. Cluster 1 = payment problems, Cluster 2 = feature requests, Cluster 3 = support complaints)

Why it works: Feedback about the same topic has nearby embeddings → it lands in the same cluster.


Why high dimensionality is necessary

A reminder from Capsule 04: real embeddings have 768D-1536D.

Why not 2D or 3D?

Reason 1: The capacity to distinguish concepts

In 2D, you can only capture 2 aspects (e.g. "animalness" and "domesticity"). With only 2 axes, many words overlap:

  • "dog", "cat", "hamster" → all close together (domestic animals)
  • You can't distinguish "dog" (large) from "hamster" (small)

In 1536D, you can capture 1536 aspects:

  • Axis 1: animalness
  • Axis 2: domesticity
  • Axis 3: size
  • Axis 4: speed
  • Axis 5: habitat
  • ...
  • Axis 1536: (some abstract aspect)

Result: Each word has a unique position; similar words are close together, but words with subtle differences (e.g. a large "dog" vs a small "mouse") remain distinguishable.


Reason 2: Capturing complex relationships

In 3D, there aren't enough "directions" to capture all the semantic relationships (gender, size, time, hierarchy, emotion, etc.).

In 1536D, there's enough "room" for multiple directions to capture different relationships:

  • The gender direction: king - man + woman = queen
  • The size direction: elephant - mouse ≈ "large"
  • The time direction: present - past ≈ "future"

Analogy: If you describe a movie with 2 characteristics (length, year), many movies overlap. If you use 100 characteristics (length, year, genre, director, actors, language, rating, etc.), each movie is unique and you can make precise recommendations. More dimensions = more capacity to capture nuance.


Advantages of representing text as vectors

AspectText as a stringText as a vector (embedding)
SearchExact words onlyMeaning (synonyms, related concepts)
ComparisonNot quantitativeMathematical distance/similarity
OperationsNot definedAddition, subtraction, scaling
RelationshipsInvisibleCaptured geometrically (e.g. king - man + woman = queen)
ClusteringDifficultNatural (group nearby vectors)
RecommendationsRule-basedBased on automatic similarity

Conclusion: Vectors let you treat language as geometry, which enables mathematical operations that capture meaning.


Limitations and considerations

Even though embeddings are powerful, they have limitations:

Limitation 1: The "black box"

We don't know what each dimension means. Models learn implicit representations; there are no labels like "Axis 1 = size" or "Axis 42 = emotion".

Consequence: You can't "edit" embeddings manually to adjust meaning (e.g. "make 'dog' bigger"). You can only use them as they are.


Limitation 2: Bias

Embeddings learn from real text (the web, books, etc.). If the training text has biases (gender, race, etc.), the embeddings reflect them.

A famous example:

  • Vector("doctor") - Vector("man") + Vector("woman") sometimes ends up close to "nurse" instead of "doctor"
  • It reflects historical biases in the training text (more text associating "doctor" with men)

Solution: Modern models (OpenAI, Anthropic) apply "debiasing" techniques during training, but it isn't perfect.


Limitation 3: Domain dependence

Embeddings pre-trained on general text (the web, Wikipedia) may not capture specialized terminology well (medical, legal, technical).

Solution: Fine-tuning (adjusting the model with text from your domain) or using models already tuned to your domain (e.g. BioBERT for medicine).


Limitation 4: Text length

Embeddings of long sentences/documents compress a lot of content into a single vector. Information can be lost.

Example: A 5000-word document → 1 vector in 1536D. Some subtleties are lost.

Solution: Chunking (splitting long documents into smaller parts, each with its own embedding).


Comparison: Embeddings vs other representations

MethodDimensionalityCaptures similarityEfficiencyTypical use
One-hotVocabulary (e.g. 50,000D)❌ No (all words equally distant)Low (very sparse)Classic ML, simple classification
TF-IDFVocabulary (e.g. 50,000D)⚠️ Partially (frequencies)MediumTraditional search, classification
Word2VecFixed (e.g. 300D)✅ Yes (similar words close together)High (dense)Classic embeddings
BERT/GPTFixed (e.g. 768D-1536D)✅ Yes (contextual)HighModern embeddings, semantic search, RAG

Conclusion: Modern embeddings (BERT, GPT, OpenAI) are the state of the art: they capture similarity, they're efficient, and they're contextual (a word's vector changes according to the context).


Exercises

Exercise 1: The distributional hypothesis

Given these sentences, which words do you think will have nearby embeddings?

Sentences:

  • "The lion roars in the jungle"
  • "The tiger hunts in the jungle"
  • "The elephant walks in the jungle"
  • "The car accelerates on the highway"
See solution

Nearby embeddings: "lion", "tiger", "elephant"

Justification: They all appear in similar contexts:

  • As the subjects of sentences about animal action
  • In the "jungle" (the same habitat)
  • With verbs related to animal behavior (roar, hunt, walk)

Distant embedding: "car"

  • A different context: highway (not jungle)
  • A different verb: accelerates (mechanical, not biological)

Conclusion: "lion", "tiger", "elephant" have similar contexts → nearby embeddings. "car" has a different context → a distant embedding.


Exercise 2: Semantic search vs keyword search

Query: "pets"

Documents:

  • Doc 1: "Dogs are loyal pets"
  • Doc 2: "Cats are domestic animals"
  • Doc 3: "Cars need gasoline"

Which documents would each method return?

See solution

Keyword search:

  • Doc 1: ✅ Contains "pets"
  • Doc 2: ❌ Doesn't contain "pets"
  • Doc 3: ❌ Doesn't contain "pets"

Result: Only Doc 1


Semantic search:

  • Doc 1: ✅ Contains "pets" → high similarity
  • Doc 2: ✅ "domestic animals" is a related concept → high similarity
  • Doc 3: ❌ "cars" isn't related → low similarity

Result: Doc 1 and Doc 2 (both related to pets/animals)

The advantage of semantic search: It finds Doc 2 even though it doesn't contain the word "pets".


Exercise 3: Interpreting a vector operation

If:

  • Vector("large") - Vector("small") captures the "size direction"

What result would you expect from:

  • Vector("elephant") - Vector("mouse")?
See answer guide

Expected result: A vector close to the "size direction" (similar to large - small).

Justification: "elephant" and "mouse" are both animals, but the main difference is size (elephant = very large, mouse = very small). So:

elephant - mouse ≈ "large animal" - "small animal" ≈ "size direction"

Verification (conceptual):

If we compute:

Vector("ant") + (Vector("elephant") - Vector("mouse"))

We'd expect to get a vector close to a "large animal" (e.g. "whale", "rhinoceros"), because we're adding the "size direction" to a small animal.

This works in real embeddings (though only approximately; it isn't mathematically exact, but the intuition is correct).


Exercise 4: Why high dimensionality?

Explain in 1-2 sentences why a 1536D embedding can distinguish concepts better than a 2D one.

See answer guide

A possible answer: "A 1536D embedding can capture 1536 different aspects of meaning (size, speed, emotion, context, etc.), whereas 2D can only capture 2 aspects. With more dimensions, subtly different concepts (e.g. 'dog' vs 'wolf' — both canids but with different domesticity) have unique positions in the space, whereas in 2D they'd overlap."

An additional analogy: "It's like describing a person: with 2 characteristics (height, weight) many people overlap; with 1536 characteristics (height, weight, age, profession, languages, hobbies, etc.), each person is unique."


Exercise 5: Use cases

For each case, would you use keyword search or semantic search?

a) Find all documents that contain exactly the product code "ABC-12345"
b) Find documents about "how to improve my app's performance" (which might use varied terms: "optimize", "performance", "speed", etc.)
c) Find legal documents that mention exactly article 42 of law X
d) Find articles related to "artificial intelligence" (which might include "machine learning", "deep learning", "AI", "neural networks", etc.)

See solution

a) The product code "ABC-12345":
Keyword search
You need an exact match (you don't want documents about "ABC-12346" or similar codes).


b) "How to improve my app's performance":
Semantic search
The query might use varied terms ("optimize", "performance", "speed"). Semantic search finds all the relevant documents even when they use different words.


c) Article 42 of law X:
Keyword search
You need an exact match on the article. You don't want similar or related articles; you want exactly that article.


d) Articles about "artificial intelligence":
Semantic search
The topic can be expressed with many terms ("AI", "machine learning", "deep learning", "neural networks"). Semantic search captures all the related concepts.


Rule of thumb:

  • Keyword search: When you need exact matches (codes, numbers, proper nouns)
  • Semantic search: When you need concepts/meanings (topics, questions, descriptions)

Troubleshooting

Problem 1: "I don't understand how the model 'learns' vectors"

Symptom: The training process feels like magic to you.

Solution: You don't need to understand the mathematical details of training (backpropagation, gradients, etc.). Just understand the intuition:

  1. The model sees millions of sentences of text
  2. It adjusts vectors so that words with similar contexts end up close together
  3. After a lot of training, the resulting vectors capture meaning

Analogy: You don't need to understand how your brain "learned" the meaning of "dog" (neurology). You just know that after seeing many dogs and hearing the word "dog" in context, your brain associates "dog" with the concept. Embeddings do the same: after seeing "dog" in millions of contexts, the model associates a vector with that concept.


Problem 2: "How do I know if two embeddings are 'close'?"

Symptom: You don't know when to consider two vectors "similar".

Solution: Use similarity metrics (you'll see them in Module 3):

  • Euclidean distance: The direct distance between points (smaller = closer)
  • Cosine similarity: The angle between vectors (1 = identical, 0 = orthogonal, -1 = opposite)

Typical thresholds (cosine):

  • 0.9: Very similar (nearly synonyms)

  • 0.7-0.9: Similar (related concepts)
  • 0.5-0.7: Moderately related
  • < 0.5: Barely related

You don't need to calculate it by hand; tools like Pinecone, Weaviate, or NumPy do it for you.


Problem 3: "Why not always use semantic search instead of keyword?"

Symptom: You think semantic search is always better.

Solution: Keyword search is still useful for:

  • Exact searches (codes, proper nouns, numbers)
  • Cases where precision is critical (legal, medical)
  • When you have little data (semantic search needs good pre-trained or fine-tuned embeddings)

The better approach: Hybrid (keyword + semantic):

  1. Filter with keyword (e.g. documents that mention "product X")
  2. Rank with semantic search (order by semantic relevance)

Many modern systems (Elasticsearch with vector search, Weaviate) support hybrid search.


Problem 4: "Do embeddings capture ALL of the meaning?"

Symptom: You think embeddings are perfect.

Solution: No. Embeddings compress meaning into a fixed-dimension vector (1536D). There's information loss, especially for:

  • Very long texts (documents of thousands of words → 1 vector)
  • Contextual subtleties (irony, sarcasm, metaphors)
  • Structural information (tables, code, formulas)

Embeddings are a useful approximation, not a perfect representation. For most tasks (semantic search, RAG), they're enough. For very specific tasks (deep literary analysis, complex reasoning), they may not capture every nuance.


Summary

In one sentence: AI systems use vectors to represent text because vector geometry (closeness = similarity) captures semantic meaning mathematically, enabling semantic search, RAG and the manipulation of concepts like king - man + woman ≈ queen.

Key points:

  • The problem: Text as strings allows neither mathematical operations nor the capture of similarity
  • The solution: Embeddings (dense 768D-1536D vectors) where similar words → nearby vectors
  • The distributional hypothesis: Words in similar contexts have similar meanings
  • Training: Models (Word2Vec, BERT, GPT) learn vectors from millions of texts
  • The result: A geometric space where closeness = semantic similarity
  • Semantic search: Searching for meaning (nearby vectors) instead of just exact words (keyword)
  • Operations: king - man + woman ≈ queen works thanks to geometric directions in high dimensions
  • High dimensionality: 1536D captures 1536 aspects of meaning (vs 2-3 in low dimensions)
  • Applications: Semantic search, RAG, recommendations, clustering, duplicate detection
  • Limitations: A black box, bias, domain dependence, information loss in long texts

Connection to the next capsule

In Capsule 07 (Capstone exercise), you're going to apply EVERYTHING you learned in the module: representing concepts as 2D vectors (simplified), computing operations (addition, subtraction), measuring distance, and visualizing how "geometric closeness = semantic similarity". This hands-on exercise will consolidate your intuition before moving on to Module 2 (Vector Spaces), where you'll see how high-dimensional spaces work conceptually and how vectors are compared (cosine similarity, distance).


Additional resources

  1. Jay Alammar: "The Illustrated Word2vec" — A blog with diagrams on how embeddings are trained and how they capture meaning. Excellent for visualizing the process. In English.

  2. Stanford CS224N: "Word Vectors" — Slides from the Stanford course on embeddings. Technical but very clear. In English.

  3. TensorFlow: "Word Embeddings" — A Google tutorial on embeddings with visual examples. In English.

  4. OpenAI: "Embeddings Guide" — OpenAI's official documentation on how to use embeddings for semantic search. With code examples. In English.

  5. Pinecone: "What are Embeddings?" — A blog aimed at vector databases that explains what embeddings are and why they're useful. In English.

  6. 3Blue1Brown: "But what is a Neural Network?" — A 19-minute video on how neural networks work (the basis of embedding models). Excellent visualizations. In English.

  7. Anthropic: "Understanding Word Embeddings" — Research articles on embeddings and how to interpret them. Technical but useful for going deeper. In English.


Next capsule: 07-capstone-exercise-1.md — A hands-on exercise to apply everything you've learned: representing concepts as 2D vectors, computing operations, measuring distance, and visualizing "closeness = similarity".