Lecture 20 · Animations
Information retrieval: dense retrieval
The search a bi-encoder does, run in front of you one step at a time: every document is scored against the query, by inner product or by cosine, and the top $k$ are kept. Press Step to advance one unit and read what happened; press Play to let it run.
All data on this page is synthetic, generated in your browser from a fixed seed, so Reset always gives back the same picture. The vectors are two-dimensional so that they can be drawn; the lecture’s encoder produces 384 dimensions.
1One query, every document
A hand-built toy corpus of 18 abstracts, already encoded: each is a point $E(d)$, its direction standing for what it is about and its distance from the origin for its norm $\lVert E(d)\rVert$ — the three long reviews sit far out. The diamond is the query $E(q)$; drag it. Each Step scores one more document, as the six-line search does, and the shaded region is where a document has to lie to be in the top $k$. The ringed point is the abstract judged relevant to the chosen query.
The bi-encoder’s score is an inner product, and an inner product factors into lengths and an angle:
$$E(q)\cdot E(d) = \lVert E(q)\rVert\,\lVert E(d)\rVert\cos\theta .$$Normalising both vectors to unit length leaves only $\cos\theta$: the cosine is the inner product of the normalised vectors. $\lVert E(q)\rVert$ multiplies every document’s score by the same positive number, so it never changes the order. The inner product therefore ranks by $\lVert E(d)\rVert\cos\theta$, the cosine by $\theta$ alone. The top-$k$ region is $\{x : E(q)\cdot x \ge s_k\}$, a half-plane whose edge is perpendicular to $E(q)$, or, for the cosine, the cone $\cos\theta \ge s_k$ of half-angle $\arccos s_k$; $s_k$ is the $k$-th best score so far.
- Under the inner product the half-plane reaches out to the long reviews: a large $\lVert E(d)\rVert$ buys a place in the top $k$ at a wide angle. That is the long-document bias normalisation removes.
- For the first query, the relevant abstract shares no word with the query, and is still the nearest by angle. For the third, the rare term BRCA1 is shared exactly, and the map ranks the relevant abstract only fourth: the encoder has blurred it into “breast cancer” in general.
- Every step costs one inner product, and the scan cannot stop early: a document’s score is unknown until it has been computed.
- On the first query press Score all, then switch to inner product: “Cardiovascular outcomes: a long review” takes rank 1 and the relevant abstract drops to rank 4.
- Under the inner product, drag the query straight outwards along its own direction. Every score grows by the same factor and the ranking does not change.
- Choose the third query and compare the shared words column with the ranking: here the lexical signal is right and the dense one is not.