Vector Embeddings & High-Dimensional Semantic Space
RAG, Vector Search & Embeddings • Vector Math9 min read

Vector Embeddings & High-Dimensional Semantic Space

Dense vector representations, Cosine similarity vs Dot product vs Euclidean distance, and normalization.

Core Conceptual Brief

Embeddings transform unstructured text into dense vectors where semantic meaning translates to spatial proximity. Understand vector geometries, dimensionality trade-offs, and similarity metrics.

Theoretical & Empirical Architecture

A vector embedding is a mapping from raw text into a dense vector space RD\mathbb{R}^D where DD is the dimensionality (e.g., 768, 1536, or 3072):

f:text⟶v⃗∈RDf: \text{text} \longrightarrow \vec{v} \in \mathbb{R}^D

In Simple English: Imagine plotting every sentence as a GPS pin on a giant map with 1,536 coordinates instead of just North/East. Sentences with similar meaning ("My dog is hungry" and "The puppy needs food") land near each other on the map, even when they use completely different words.

In this semantic vector space, texts with similar conceptual meaning are positioned close together, even if they share zero overlapping vocabulary (e.g. "puppy" and "young dog").

Under the Hood: Key Engineering Mechanics

1Similarity Metrics Compared

Cosine Similarity measures the angle between two arrows: cos⁡(θ)=u⃗⋅v⃗∥u⃗∥∥v⃗∥\cos(\theta) = \frac{\vec{u} \cdot \vec{v}}{\|\vec{u}\| \|\vec{v}\|}. Think of it like two compass needles: pointing the exact same way gives 1.0 (match), perpendicular at 90 degrees gives 0.0 (unrelated), and opposite gives -1.0. If vectors are scaled to length 1 (L2L_2-norm = 1), Cosine Similarity becomes just multiplying matching coordinates and adding them up (Dot Product u⃗⋅v⃗\vec{u} \cdot \vec{v}), which is much faster on CPUs and GPUs.

2Dimensionality & Matryoshka Embeddings

Models like OpenAI text-embedding-3 or modern open-weights (BGE, Cohere) support Matryoshka Representation Learning (MRL). MRL trains the model so that the first KK dimensions (e.g. 512 out of 3072) retain 98%+ of the semantic retrieval accuracy, cutting vector database memory storage and search latency by over 80%.

3Asymmetric Search (Query vs Document)

In search applications, queries are short (5-10 words) while documents are long paragraphs (200-500 words). High-performance embedding models use task prefixes (e.g. "search_query: " vs "search_document: ") to map asymmetric inputs into the same semantic neighborhood.

Computing Normalized Dot Product & Cosine Similarity

import numpy as np

def cosine_similarity(a: np.ndarray, b: np.ndarray) -> float:
    # Standard formula
    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

def fast_normalized_dot_product(u: np.ndarray, v: np.ndarray) -> float:
    # If vectors are pre-normalized upon insertion into DB:
    # norm(u) == 1.0 and norm(v) == 1.0
    u_norm = u / np.linalg.norm(u)
    v_norm = v / np.linalg.norm(v)
    return float(np.dot(u_norm, v_norm))

vec_a = np.array([0.23, 0.89, -0.45, 0.12])
vec_b = np.array([0.21, 0.85, -0.49, 0.15])

score = fast_normalized_dot_product(vec_a, vec_b)
print(f"Semantic Alignment Score: {score:.4f}")  # ~0.9982 (Strong semantic match)

Production Guardrails & Takeaways

  • Always pre-normalize embeddings to unit length (L2 norm = 1) at ingestion time.
  • On unit-normalized vectors, Dot Product produces the exact same ranking as Cosine Similarity at 3x faster compute.
  • Use Matryoshka embeddings to truncate vector dimensions when scaling to millions of documents.

Real Staff AI Systems Interview Question

Q: Why is Dot Product computationally faster than Cosine Similarity in production vector databases?

Staff Answer:

Cosine Similarity requires calculating the vector magnitude (square root of the sum of squares) for both vectors on every comparison. When vectors are pre-normalized to unit length (norm = 1.0) during ingestion, the denominator is 1.0, reducing the operation to a single hardware-accelerated dot product (FMA - Fused Multiply-Add instruction).

Vector Embeddings & High-Dimensional Semantic Space | AI Systems Engineering Guide · DataArena