advanced32 min studyLesson 693 in Engineering

Two-tower retrieval requires matching representations and the exact search metric

Connect precomputed item vectors, query encoding, inner products, normalization, approximate search, and version compatibility.

On this page

What you’ll understand

  • Distinguish maximum inner product from cosine and squared-distance retrieval.
  • Apply the unit-vector identity only after valid normalization.
  • Trace query, item, feature, and index versions through a retrieval request.

A two-tower retriever separately encodes request context and items into vectors whose compatibility score can be searched efficiently. Item vectors can often be prepared before the request, while the query tower runs online. That separation makes large-catalog retrieval practical, but it also creates a representation contract. The trained score, normalization, indexed metric, feature mapping, and item-vector version must agree. A fast nearest-neighbor index cannot repair a query compared with incompatible item vectors or an unintended similarity function.

State what the score depends on

Let a query tower produce q from the request context and an item tower produce v_i from item features. A dot-product score enables precomputation when the item representation does not depend on request-specific features omitted from its tower. Rich cross-features between a particular user and item may need a later ranker instead of a precomputed item vector. Preserve the distinction between a model architecture that permits separate encoding and an arbitrary scoring network whose item side cannot be cached independently without changing its function.

For a fixed nonzero query, maximum inner product depends on item-vector norm as well as direction. Normalizing only the query multiplies all dot products by the same positive factor and preserves ordering, but normalizing each item generally changes an unnormalized dot-product ranking. Cosine similarity divides by both norms. If training used magnitude as part of the learned score, silently replacing it with cosine is a model change requiring evaluation. Zero vectors have no defined cosine direction and need an explicit fallback or exclusion policy before normalization.

s(q,v)=q⊤v,cos⁡(q,v)=q⊤v∥q∥2∥v∥2(∥q∥2,∥v∥2>0)s(q,v)=q^\top v,\qquad \cos(q,v)=\frac{q^\top v}{\|q\|_2\|v\|_2}\quad(\|q\|_2,\|v\|_2>0)
Vectors share a compatible dimension and representation space. The raw inner product and cosine are different scores unless normalization conditions hold. Cosine is dimensionless; embeddings are abstract learned coordinates whose scale convention belongs to the model, not a physical distance guarantee.↔ Scroll wide equations horizontally.

Normalization connects scores but does not certify approximate results

For unit-length query and item vectors, squared Euclidean distance equals two minus twice their inner product. Exact minimization of that distance then gives the same ordering as exact maximum inner product or cosine, subject to ties. Without unit norms, the general squared-distance identity includes separate norm terms and can produce a different ranking. Some libraries report squared distances, so do not interpret their numeric outputs as unsquared lengths. Check normalization and the documented metric at both index construction and query time.

Equivalent exact scores do not guarantee two approximate index configurations return identical neighbors. Quantization, pruning, search effort, and stale or missing vectors can change results. Evaluate approximate recall against exact top neighbors under the same score, then evaluate relevance coverage separately. Sampled training negatives introduce another distribution choice: a training correction for sampled items is not automatically a correction for logged exposure bias or a proof that retrieval scores estimate click probability. Keep score meaning, training sampling, and online feedback identification separate.

∥q−v∥22=∥q∥22+∥v∥22−2q⊤v=2−2q⊤vwhen ∥q∥2=∥v∥2=1\|q-v\|_2^2=\|q\|_2^2+\|v\|_2^2-2q^\top v=2-2q^\top v\quad\text{when }\|q\|_2=\|v\|_2=1
The first identity holds for compatible finite real vectors. The second equality requires both unit norms. It concerns exact scores; approximate search and low-precision storage introduce additional errors and do not inherit an identical-neighbor guarantee merely from this algebra.↔ Scroll wide equations horizontally.

Publish an index with its compatible model contract

Track query-tower, item-tower, feature-schema, ID mapping, normalization, and index versions together. Updating one tower while retaining old item vectors can change compatibility even when dimensions match. A rollout needs a validated compatible pair or an explicitly tested transition, not only a successful deserialization. Record the snapshot used for each request and define how new, removed, or modified items enter it. Compare exact probes, relevance cohorts, latency, and resource cost before switching a production index generation.

The vector lab illustrates a two-dimensional projection rather than a retriever. Reset, set both angle 0° and magnitude 1, predict components (1,0), then Play or Step. Reset and set both angle 90° and magnitude 1 for (0,1), within numerical display precision. Reset and set both angle 0° and magnitude 2 for (2,0). The graph uses Cartesian projections of one vector and playback traces its selected angle; it has no item index, query tower, learned score, or nearest-neighbor algorithm. Use the explicit vectors in the worked example to compare actual metrics.

Item normalization can reverse an inner-product choice

Use query q=(1,0) and item vectors A=(2,2), B=(1,0). Compare exact maximum inner product, cosine similarity, and squared Euclidean distance after normalizing both item vectors and the query.

  1. Raw dot products are q·A=2 and q·B=1, so maximum inner product selects A. A's larger norm contributes to this result; the raw score is not cosine.

  2. Cosines are 2/√8=1/√2≈0.7071 for A and 1 for B, so cosine selects B. Normalizing only q would not change the original raw-dot ordering, because q already has unit norm.

  3. After all vectors are normalized, A has squared distance 2−2/√2≈0.5858 from q, while B has distance 0. Minimizing these normalized squared distances agrees with cosine and selects B.

The result

The unit-vector identity is exact under normalization, while applying normalization changed the original inner-product retrieval objective. Metric choice belongs to the trained serving contract.

Explore the model

Interactive lab

Interactive lab · vectors

Direction and magnitude are distinct representation choices

Reset, set both angle 0° and magnitude 1, predict (x,y)=(1,0), then Play or Step. Separate fresh Reset runs with both angle 90°,magnitude 1 and angle 0°,magnitude 2 give (0,1) and (2,0), within display rounding. The model projects one two-dimensional vector and playback traces it; it neither searches an index nor computes cosine between two supplied embeddings.

Direction and magnitude are distinct representation choicesPlayback traces a fixed vector and its projections. Angle and magnitude set the geometry; the moving marker does not change them. Current readings: x component: 1.393; y component: 0.975; Magnitude: 1.7. All parameters are adjustable with the labeled controls below.−7.46−3.7303.737.46−3.5−1.7501.753.5xyvvₓ = 1.39
Vectorx / y projectionsTrace marker
x component
1.393
y component
0.975
Magnitude
1.7
t = 0 s

Playback traces a fixed vector and its projections. Angle and magnitude set the geometry; the moving marker does not change them. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forPreserve the model's magnitude, normalization, and metric conventions before evaluating index approximation.

Check your understanding

A retriever was trained for unnormalized dot products. What follows from normalizing every item before cosine search?

Try it yourself

Two unit vectors have inner product 0.6. Compute their squared Euclidean distance and ordinary Euclidean distance. Explain which normalization assumption makes the short formula valid.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Use 2−2s for squared distance, then take its square root for ordinary distance.

Compare with the solution

Squared distance is2−2×0.6=0.8 and ordinary distance is√0.8≈0.89442719. The short formula requires both vectors to have unit norm; otherwise use their separate squared norms. A library reporting 0.8 under an L2 metric may be reporting squared distance, so inspect its documented convention.

Take it with you

  • Two-tower serving separates encoders only under an explicit scoring and feature contract.
  • Raw dot product, cosine, and distance have matching rankings only under the stated conditions.
  • Version query vectors, item vectors, schemas, normalization, and the index as compatible artifacts.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.