Semantic Distance Workbench AI & Embeddings

2D Vector Embedding Space

Tip: Drag vector heads to scrub coordinates

Real-Time Distance Metrics (vs Query)

Comparing: Query Vector (Blue)
Cosine Similarity 0.9998 cos(θ) ∈ [-1, 1] (1.0 = identical)
Cosine Distance 0.0002 1 - cos(θ) (0 = identical)
Euclidean (L2) 0.8000 √(∑(ui - vi)²)
Dot Product 1.8000 u · v = ||u|| ||v|| cos(θ)
💡
Key AI Insight: Doc B has nearly identical semantic orientation to the Query, but because Doc B is 3.5x longer (larger magnitude), raw Euclidean distance ranks concise Doc A closer, while Cosine Similarity correctly identifies both as high semantic matches.

Retrieval Ranking Benchmark (Top-K)

Candidate Document Magnitude ||v|| Angle θ Cosine Sim (Rank) Euclidean L2 (Rank) Dot Product (Rank)
Mathematical Relationship:
For L2-Normalized vectors (||u|| = ||v|| = 1):
dEuclidean² = ||u - v||² = ||u||² + ||v||² - 2(u · v) = 1 + 1 - 2 cos(θ) = 2(1 - cos θ)
∴ dEuclidean = √(2 · dCosine) ⇒ Rankings become mathematically identical!
Toggle "L2 Normalize Vectors" on the left to see Euclidean L2 ranking become identical to Cosine distance ranking.

📐 Why AI Uses Cosine Similarity for Embeddings

In text embeddings and Large Language Models, document vectors frequently have varying magnitudes simply because longer texts contain more total word occurrences or token counts.

  • Length Invariance: Cosine similarity isolates the angular direction (semantic topic) from vector length.
  • Magnitude Sensitivity: Euclidean distance can falsely penalize a comprehensive, long article that perfectly answers a query simply because its norm is large.

⚡ Vector Normalization & Fast Search (HNSW / MIPS)

Modern vector databases (e.g. Pinecone, Milvus, Qdrant, Faiss) store L2-normalized embeddings.

  • When vectors have unit length (||v|| = 1.0), the Dot Product equals Cosine Similarity: u · v = cos(θ).
  • This allows hardware accelerators (GPUs/TPUs) to compute massive cosine similarity searches via basic matrix multiplications without division operations.

🧠 User Intention vs Machine Exposition

In computational semantics and AI alignment, the ideal system minimizes distance between what the user actually wants and what the model predicts.

  • High Cosine Similarity: Indicates conceptual alignment along intentional axes.
  • Orthogonal Vectors (θ = 90°): Independent, uncorrelated semantic concepts.
  • Opposite Vectors (θ = 180°): Contradictory or antonymous concepts.
State saved

Direction, magnitude and distance in the saved vector workbench

Read the explanation

The saved distance workbench uses explicit two-dimensional toy coordinates. Its query is point eight, point six, a unit vector. The concise document is exactly the same vector, so cosine similarity is one and Euclidean distance is zero. The longer-document label has vector two point four five, two point five. Its cosine with the query is about point nine eight eight five, while Euclidean distance is about two point five one six. These are actual coordinate calculations, not embeddings generated from document text or evidence that a large score proves alignment. The magnitude preset keeps every candidate on the same ray as query point eight, point six. Half scale is point four, point three; double scale is one point six, one point two; three point five scale is two point eight, two point one. All cosine similarities are one because positive scaling cancels between numerator and denominator. Their Euclidean distances from the unit query are point five, one and two point five. The three bars show those exact distances on one scale. Switch normalization on and each nonzero vector is divided by its own magnitude. The magnitude preset candidates then coincide with the query at point eight, point six, and both Euclidean and Manhattan distance become zero. Without normalization, the double-scale candidate differs by point eight and point six: Manhattan distance is one point four while Euclidean distance is one. The source handles zero magnitude specially; the geometric demonstration uses nonzero vectors and does not turn its toy labels into factual semantic claims.

Super generates helpful tools and automates fact-checking across the internet proactively. If you enjoyed this tool, build your own with Super and share it with a friend.