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
Enjoy this tool? Build your own with Super