ML/DL literacy

Embeddings before LLMs

Build geometric intuition for embeddings without transformers

50 min3/5 in module

Learning objectives

  • Build geometric intuition for embeddings without transformers
  • Visualize a 2D embedding projection
  • Relate distance to semantic similarity on toy words/docs

Why embeddings matter for AI engineers

Before transformers and retrieval-augmented generation, embeddings already powered search, recommendations, deduplication, and clustering. They remain the backbone of semantic search, vector databases, and reranking pipelines you will build later in this curriculum.

An embedding is a dense vector representation of a discrete object — a word, a sentence, a user, a product SKU. Instead of storing a one-hot vector with 50,000 dimensions (mostly zeros), you store a compact vector of, say, 384 or 768 floats where each dimension captures some learned aspect of meaning.

The key idea: similar things should be close in vector space. "King" and "queen" should have smaller cosine distance than "king" and "banana." This geometric property is what makes nearest-neighbor search work for "find documents like this one."

You do not need a transformer to build this intuition. Classical methods — word2vec, GloVe, TF-IDF followed by SVD — all produce vectors you can plot, measure, and reason about. Understanding embeddings geometrically makes every later topic (attention, RAG, fine-tuning) easier.

Callout — embeddings are coordinates, not magic: An embedding is just a point in space. What makes it useful is how that space was trained — what co-occurrence or contrastive signal shaped the geometry.

From sparse to dense

Consider representing three words: "cat," "dog," and "car."

One-hot encoding gives each word its own axis:

Word dim_0 dim_1 dim_2
cat 1 0 0
dog 1 0 0
car 0 0 1

Every word is orthogonal to every other word. Distance tells you nothing about meaning — "cat" and "dog" are exactly as far apart as "cat" and "car."

Dense embeddings compress meaning into fewer dimensions:

Word dim_0 (animal?) dim_1 (size?)
cat 0.9 -0.3
dog 0.85 0.5
car -0.8 0.7

Now "cat" and "dog" are neighbors; "car" is farther away. The model (or algorithm) learned which directions in space correspond to useful distinctions.

How classic word embeddings are trained

Word2Vec (skip-gram and CBOW)

Word2Vec trains embeddings by predicting context from a word (skip-gram) or a word from context (CBOW). If "king" frequently appears near "queen" and "royal," their vectors get pushed together during training.

The famous result: vector arithmetic emerges. king - man + woman ≈ queen. That is not programmed — it falls out of co-occurrence statistics compressed into a low-dimensional space.

GloVe

GloVe (Global Vectors) factorizes a word co-occurrence matrix. It combines global corpus statistics with local context window ideas. In practice, GloVe and word2vec produce similarly useful vectors for many downstream tasks.

TF-IDF + dimensionality reduction

Even simpler: represent documents as TF-IDF vectors (sparse, high-dimensional), then apply PCA or UMAP to project to 2D for visualization. You lose some nuance but gain interpretability — great for a first project.

For AI engineering work today, you will mostly use pretrained embedding models (sentence-transformers, OpenAI embeddings, Cohere embed). But the geometric intuition from training or visualizing small embeddings transfers directly.

Distance metrics: what "similar" means

Once you have vectors, you need a metric. The two most common:

Cosine similarity measures the angle between vectors, ignoring magnitude:

cos_sim(a, b) = (a · b) / (||a|| × ||b||)

Range: -1 to 1. Values near 1 mean same direction (similar). Used heavily in semantic search because it handles documents of different lengths well when vectors are normalized.

Euclidean distance (L2) measures straight-line distance:

L2(a, b) = sqrt(Σ (a_i - b_i)²)

Smaller is more similar. Sensitive to vector magnitude — if embeddings are not normalized, a long document vector can appear "far" from a short one even when the topic matches.

Metric Best when Watch out for
Cosine Normalized embeddings, text search Assumes direction = meaning
L2 Unnormalized vectors, spatial data Magnitude bias
Dot product Some ANN indexes (FAISS inner product) Requires consistent normalization strategy

Most vector databases let you choose the index metric at ingest time. Mismatching training normalization and search metric is a common production bug.

Visualizing in 2D

Humans cannot inspect 768-dimensional space. Dimensionality reduction projects high-D vectors to 2D (or 3D) for exploration:

  • PCA — linear, fast, preserves global variance. Good first pass.
  • t-SNE — nonlinear, preserves local neighborhoods. Clusters look pretty but distances between clusters are misleading.
  • UMAP — nonlinear, faster than t-SNE, better global structure. Popular for embedding viz.

When you plot word or document embeddings in 2D, look for:

  • Clusters — topics or categories grouping together.
  • Outliers — mislabeled data, duplicates, or domain-specific jargon.
  • Gradients — continuous variation (e.g. sentiment from negative to positive along one axis).

Callout — 2D plots lie a little: Neighbors in a t-SNE plot were neighbors in high-D space, but the distance between two clusters in 2D is not meaningful. Use 2D for exploration, not for production similarity thresholds.

Embeddings in the modern stack

Even with LLMs everywhere, embeddings show up constantly:

  • Semantic search — embed query and documents; retrieve top-k by cosine similarity.
  • Reranking — bi-encoder retrieval (fast, embedding-based) followed by cross-encoder reranking (slow, accurate).
  • Clustering and dedup — near-duplicate detection at scale.
  • Classification features — embed text, train a linear classifier on top (cheap, strong baseline).

Later modules will have you build retrieval pipelines. The embedding step you visualize here is the same step that feeds your vector index.

Engineering problem (staff framing)

Embeddings power search/RAG. Version them with the index.

Diagram — Embed → ANN

flowchart LR
  T[Text] --> E[Encoder] --> V[Vector] --> ANN[ANN] --> N[Neighbors]

Precise definitions & mental model

Dense vectors, cosine/dot, static vs contextual embeddings.

Tradeoffs — when to use what

Static cheap / weak; sentence-transformers mid; LLM states expensive.

Failure modes (interview + on-call)

Mixed models in one index; cosine≠relevance; skip normalization.

Production & OSS practices

Re-embed on model upgrade; recall@k / nDCG on held-out queries.

Deep dive (FAANG / OSS bar)

Push «embeddings-before-llms» past tutorial depth: write the interface contract (inputs/outputs/invariants), list three measurable metrics, and name two degrade modes if the happy path fails. Add a short threat note: what an attacker or noisy tool result could do, and which layer catches it (schema, policy, HITL, or eval gate).

flowchart LR
  Contract[Interface contract] --> Metrics
  Metrics --> Degrade[Degrade modes]
  Degrade --> Threat[Threat + control]

Micro-project: 2D viz

In m2/embeddings/ (or your course-portfolio monorepo under the matching module folder):

  1. Choose a small corpus — 50–200 short texts (product reviews, news headlines, or a word list with categories like animals, countries, verbs).
  2. Obtain embeddings using one of:
    • Pretrained sentence-transformers model (fastest path), or
    • Train small word2vec on a text file (more educational), or
    • TF-IDF + PCA (simplest baseline).
  3. Project embeddings to 2D with PCA, t-SNE, or UMAP.
  4. Plot with matplotlib or plotly; color points by category/label.
  5. Write a short NOTES.md answering:
    • Do neighbors in 2D match your semantic expectations?
    • Give one example pair that is close but should not be (or far but should be close) — explain why.
    • Which distance metric did you use and why?

Commit the plot as embedding_2d.png and the code that generates it.

Acceptance criteria map directly to the lesson objectives: you should be able to point at the plot and explain why "dog" is closer to "cat" than to "car" in vector space.

Checklist

  • Embeddings computed for a toy corpus (words or short docs)
  • 2D projection plotted and committed
  • At least one concrete example relating distance to semantic similarity
  • Notes on which metric and reduction method you used
Project checklist0/3 done

ShipAI delivery model is: