Skip to content

The projection

Between the encoder and the memory sits the only part of vmfstream that is fitted to data: Projection, three steps applied to every embedding.

  1. Centre. Subtract the mean of a reference sample.
  2. Reduce. Keep the d directions along which the reference varies most (principal component analysis, PCA).
  3. Normalise. Scale to length one, so that the item is a direction, a point on the sphere Sd−1S^{d-1}.

It is fitted once, by Memory.fit_projection(reference), and never changes while the stream is read. The cells on this page use the Portal stream and the paper’s development stream of Example data.

Sentence embeddings of one source tend to share a common direction: the topics, register and length typical of that source. Left in place, it makes every pair of items look somewhat alike. The cosine between two random items shows how much:

def random_cosines(x, pairs=20_000):
u = x / np.linalg.norm(x, axis=1, keepdims=True)
i, j = rng.integers(0, len(x), size=(2, pairs))
c = np.einsum("nd,nd->n", u[i], u[j])
low, high = np.percentile(c, [5, 95])
return f"mean {c.mean():5.2f}, 90% between {low:5.2f} and {high:4.2f}"
print("development stream, raw:", random_cosines(reference))
print("Portal stream, raw: ", random_cosines(embeddings))
Output
development stream, raw: mean  0.29, 90% between  0.06 and 0.69
Portal stream, raw:      mean  0.11, 90% between -0.04 and 0.31

The development stream, a few dozen news stories of 2015, leans strongly towards its own region (mean cosine 0.29); the Portal, nine years of everything, much less (0.11). Centring removes the lean, but only with the right mean:

from vmfstream import Projection
on_dev = Projection(256).fit(reference)
on_portal = Projection(256).fit(embeddings[hours < 24 * 366]) # the Portal's first year, 2016
print("development, fitted on development:", random_cosines(on_dev.transform(reference)))
print("Portal, fitted on development: ", random_cosines(on_dev.transform(embeddings)))
print("Portal, fitted on Portal 2016: ", random_cosines(on_portal.transform(embeddings)))
Output
development, fitted on development: mean  0.01, 90% between -0.24 and 0.54
Portal, fitted on development:      mean  0.11, 90% between -0.06 and 0.35
Portal, fitted on Portal 2016:      mean  0.01, 90% between -0.15 and 0.22

Centred on its own mean, the development stream drops to cosine 0 between random items. The Portal, centred on the development stream’s mean, stays at 0.11: that mean is not its own. Fitted on the Portal’s first year, it drops to 0.01. Fit the projection on a sample of the same kind of text as the stream, a previous period or its first weeks, but not on the stream itself: the stream is read online, and the projection must not see what is coming. The examples in these docs keep the paper’s choice, the development stream, so that they reproduce the paper.

The difference shows in scores too. In the experiment below, the same development stream read with the projection fitted on itself scores B³ F1 0.8645, and with the projection fitted on another source (WCEP, 2017) 0.843.

PCA keeps the directions of largest variance. The first few carry most of it:

explained = memory.projection.explained
print(f"variance kept by the 256 directions: {explained.sum():.1%}")
print(f"by the first 16: {explained[:16].sum():.1%}; by the first 64: {explained[:64].sum():.1%}")
Output
variance kept by the 256 directions: 97.3%
by the first 16: 61.7%;  by the first 64: 84.2%
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(6, 2.4))
ax.plot(np.arange(1, 257), np.cumsum(explained), color="#2F6F9F")
ax.set(xlabel="principal directions kept, d", ylabel="share of variance", ylim=(0, 1))
for side in ("right", "top"):
ax.spines[side].set_visible(False)
fig.savefig("variance.svg", bbox_inches="tight")
OutputCumulative share of the reference sample's variance kept by the first d principal directions.

Reducing from 1,024 to 256 numbers loses little: on the development stream, how well the cosine separates pairs of the same story from pairs of different stories less than a week apart (an AUC) is 0.977 with the raw embeddings and 0.972 after the projection. It makes each read four times cheaper, and it puts the memory in a fixed dimension, which its formulas (the normaliser Cd(κ)C_d(\kappa), the uniform density) depend on.

After the projection, items of the same node are clearly closer than random pairs, but far from cosine 1:

history = memory.read(z, hours, projected=True)
same = []
for node in history.largest(200):
idx = history.items(node)
a, b = rng.choice(idx, 50), rng.choice(idx, 50)
same.append(np.einsum("nd,nd->n", z[a], z[b])[a != b])
same = np.concatenate(same)
print("two items of the same node:", f"mean {same.mean():5.2f}, 90% between "
f"{np.percentile(same, 5):5.2f} and {np.percentile(same, 95):4.2f}")
Output
two items of the same node: mean  0.45, 90% between  0.14 and 0.72

Compare with the centred Portal above: random pairs around 0.01, items of a node around 0.45. That gap is what the memory reads, and it is why κ, not a fixed threshold, decides: the cosine lab shows the same picture in theory.

Both are non-linear and draw clusters beautifully, and vmfstream uses UMAP for exactly that, to draw the explorer’s sphere. As the projection that the memory reads, they do not fit, for three reasons.

The stream is new by definition. The projection has to place items it has never seen, one at a time. t-SNE has no way to place a new point without recomputing the whole layout; its fast version also only works in two or three dimensions. UMAP can place new points, but by looking them up among the neighbours of its fitted sample, and a story that breaks today has no neighbours there.

The memory needs geometry, not just neighbours. Its densities are functions of the cosine, and its concentration estimates come from how far apart a node’s items are. PCA is a rotation followed by a cut: cosines in the kept directions are the cosines of the embeddings. UMAP keeps who is near whom and freely distorts distances and densities, which is what makes its pictures readable and its cosines meaningless to a vMF.

We measured it. On the paper’s development stream, the projection was fitted on WCEP 2017, a different source, and the development stream was then transformed, as an online system would. The memory’s hyperparameters stayed those of the paper, and the AUC, which does not depend on them, measures the representation alone (scripts/projection_umap.py in the research repository, with the gate declared before running: UMAP would replace PCA if it reached the AUC of PCA and a B³ F1 at least 0.01 higher).

Projection d neighbours AUC, fitted on WCEP 2017 B³ F1 Nodes AUC, fitted on the stream B³ F1 Nodes
PCA 256 0.972 0.843 106 0.973 0.865 121
UMAP 16 15 0.739 0.700 29 0.969 0.836 25
UMAP 16 50 0.758 0.727 29 0.969 0.829 21
UMAP 64 15 0.718 0.712 29 0.971 0.854 25
UMAP 64 50 0.763 0.719 23 0.972 0.824 17
UMAP 256 15 0.724 0.699 19 0.972 0.844 20
UMAP 256 50 0.757 0.710 12 0.972 0.766 11

The gate is not met, by a wide margin. Fitted on another source, as an online system must be, every UMAP arm separates stories far worse than PCA (AUC 0.72 to 0.76 against 0.97), and the memory, reading UMAP’s points, merges stories into a few dozen nodes (B³ F1 0.70 to 0.73 against 0.84). Fitting and transforming took UMAP 40 to 120 seconds, against 0.4 for PCA.

The right-hand columns are UMAP’s best case, not an option: fitted on the stream itself, UMAP has seen every item, and every story, before the memory reads the first one, which is offline clustering in disguise. Even so it only matches PCA’s AUC, and the memory scores lower with it (at best 0.854 against 0.865) and opens far fewer nodes. That is what one would expect from UMAP’s layout, which pulls each cluster tight and pushes clusters apart: the cosines the memory’s hyperparameters were chosen for no longer mean the same, and nearby stories fuse.

PCA is the right tool here not because it is simple but because it changes almost nothing: a rotation, a cut that keeps 97 % of the variance, and a centring. The memory then works on the encoder’s own geometry, which is what the paper’s results measure. A better representation has to come from the encoder; the paper’s main result comes from adapting it.