📚Academy
likeone
online

Similarity Challenge.

Put your embedding knowledge to the test. Predict similarities and prove your mastery.

This assessment covers

  • Predicting cosine similarity between word pairs
  • Understanding how embeddings power real applications
  • Vector analogies and relationship encoding
  • Everything from the AI Foundations course

How machines measure "similar."

Humans intuitively know that "dog" and "puppy" are related while "dog" and "algebra" are not. But how does a machine know? The answer is similarity metrics — mathematical formulas that compare vectors and output a number representing how close two concepts are.

Cosine Similarity — measuring the angle

The most common similarity metric in AI. It measures the angle between two vectors, ignoring their length. Imagine two arrows starting from the same point. If they point in the same direction, cosine similarity = 1.0 (identical meaning). If they are perpendicular, cosine = 0.0 (completely unrelated). If they point in opposite directions, cosine = -1.0 (antonyms). The formula: dot product divided by the product of magnitudes.

Euclidean Distance — measuring the gap

The straight-line distance between two points in space. If cosine similarity is about direction, Euclidean distance is about position. Two words can point in the same direction (high cosine) but be far apart in absolute position (high Euclidean distance). In practice, cosine similarity is preferred for text because document length affects position but not direction.

Dot Product — the raw score

Multiply each pair of matching dimensions together and sum the results. The dot product captures both direction AND magnitude. Cosine similarity is just the dot product normalized by the lengths. When vectors are already normalized (length = 1), the dot product and cosine similarity are identical — which is why many systems normalize their embeddings before storing them.

Here is how these three metrics compare in practice:

  SIMILARITY METRICS COMPARED

  Word Pair           Cosine    Euclidean    Dot Product
  ──────────          ──────    ─────────    ───────────
  cat ↔ dog           0.95      0.31         0.92
  cat ↔ kitten        0.89      0.47         0.85
  cat ↔ car           0.27      1.22         0.24
  cat ↔ algebra       0.05      1.38         0.04

  Higher cosine = more similar (max 1.0)
  Lower Euclidean = more similar (min 0.0)
  Higher dot product = more similar

  In practice: cosine similarity is the standard for NLP
  because it ignores vector length (document size)
Vector Analogies — relationships as directions

The famous equation king - man + woman = queen works because relationships are encoded as consistent directions in embedding space. The direction from "man" to "king" captures the concept of male royalty. The direction from "man" to "woman" captures gender. Subtracting one direction and adding another navigates the meaning-space — like following a map. This works for geography (Paris - France + Japan = Tokyo), tenses (walking - walk + swim = swimming), and many other relationships.

Real-world applications

Semantic search finds documents by meaning, not keywords. Recommendation engines find similar products by comparing embedding vectors. RAG retrieves relevant context before the AI generates a response. Duplicate detection identifies near-identical content by checking cosine similarity thresholds. Clustering groups similar items together for analysis. All of these rely on the same core operation: comparing vectors.

The bottom line: similarity is the bridge between human meaning and machine math. When you understand how cosine similarity works, you understand the engine behind modern search, recommendations, and AI-powered retrieval. Now prove it.

Embedding mastery.

Python — Cosine Similarity from Scratch
import math

def cosine_similarity(vec_a, vec_b):
    """Measure how similar two vectors are (0 = unrelated, 1 = identical)."""
    dot = sum(a * b for a, b in zip(vec_a, vec_b))
    mag_a = math.sqrt(sum(a ** 2 for a in vec_a))
    mag_b = math.sqrt(sum(b ** 2 for b in vec_b))
    if mag_a == 0 or mag_b == 0:
        return 0.0
    return dot / (mag_a * mag_b)

# Toy 2-D embeddings (real ones use 768+ dimensions)
cat   = [0.9, 0.1]   # mostly "animal", a little "object"
dog   = [0.85, 0.15]  # very close to cat
car   = [0.1, 0.95]   # mostly "object", a little "animal"

print(f"cat · dog = {cosine_similarity(cat, dog):.4f}")  # ~0.9949
print(f"cat · car = {cosine_similarity(cat, car):.4f}")  # ~0.2688

The math behind cosine similarity.

You do not need to memorize the formula, but understanding it makes the concept click. Cosine similarity has three steps:

Step 1: Dot Product — multiply and sum

Multiply each pair of matching dimensions, then add them all up. For vectors [3, 4] and [4, 3]: (3 x 4) + (4 x 3) = 12 + 12 = 24. The dot product is large when vectors point in similar directions and small (or negative) when they point in different directions.

Step 2: Magnitudes — measure the lengths

Calculate the length of each vector using the Pythagorean theorem. For [3, 4]: sqrt(3^2 + 4^2) = sqrt(9 + 16) = sqrt(25) = 5. For [4, 3]: same thing, also 5. The magnitude tells you how "strong" the vector is, independent of its direction.

Step 3: Divide — normalize the result

Divide the dot product by the product of both magnitudes: 24 / (5 x 5) = 24/25 = 0.96. This normalization is what makes cosine similarity ignore vector length and focus purely on direction. Whether a document is 100 words or 10,000 words, its direction in embedding space is what matters for similarity.

  COSINE SIMILARITY WORKED EXAMPLE

  Vector A = [3, 4]     (e.g., the word "cat")
  Vector B = [4, 3]     (e.g., the word "dog")

  Dot Product:  (3 × 4) + (4 × 3) = 12 + 12 = 24
  Magnitude A:  sqrt(3² + 4²) = sqrt(25) = 5
  Magnitude B:  sqrt(4² + 3²) = sqrt(25) = 5

  Cosine Sim :  24 / (5 × 5) = 24/25 = 0.96

  Result: very similar! These vectors point nearly the same way.
  For comparison: perpendicular vectors = 0.00, opposite = -1.00
That is the entire formula. Dot product divided by magnitudes. Three operations, one number that captures how similar two concepts are. Every semantic search engine, every recommendation system, every RAG pipeline runs this exact calculation millions of times per second.

Full course review.

The AI Foundations gauntlet.

You now understand how AI actually works.

Most people use AI without understanding any of this. You now know:

  • Neurons — weighted sum + bias + activation function
  • Networks — layers of neurons that find increasingly complex patterns
  • Tokens — how AI reads text (not words, not characters)
  • Prompt techniques — zero-shot, few-shot, chain-of-thought, role-play
  • Temperature — the creativity dial
  • Embeddings — words as vectors, meaning as geometry
  • Cosine similarity — how AI measures relatedness
  • RAG — retrieval-augmented generation

This foundation makes everything else in AI make sense. You're ready for the next course.

🔒

This lesson is for Pro members

Unlock all 520+ lessons across 52 courses with Academy Pro.

Already a member? Sign in to access your lessons.

Academy
Built with soul — likeone.ai