Reading a stream
Every cell on this page ran on the Wikipedia Current Events Portal stream described in Example data; the output under each one is what it printed.
Inputs
Section titled “Inputs”The memory reads two arrays: one sentence embedding per item, from any encoder, and the arrival times, non-decreasing, in the unit of half_life (hours by default). Before reading, fit the projection on a reference sample that is not the stream itself: a development stream, or the first weeks of the source. It is the only part fitted to data, and it is fitted once.
Memory.project shows what the memory will read: every item becomes a unit vector of d numbers.
memory = Memory().fit_projection(reference)points = memory.project(embeddings)points.shape, np.linalg.norm(points[:3], axis=1)((49090, 256), array([1., 1., 1.]))
If you already have unit vectors in the memory’s space, skip the projection with projected=True. The result is the node of each item; identities are the order of birth, so the first items open nodes 0, 1, 2 and the stream keeps returning to node 0.
history = memory.read(points, hours, projected=True)history.node[:12]array([0, 1, 0, 2, 3, 4, 0, 0, 5, 0, 0, 0])
step: one item at a time
Section titled “step: one item at a time”Memory.read is a loop over Memory.step, which returns a Step and keeps no record. It suits a live system that receives items one by one:
fresh = Memory().fit_projection(reference)for i in range(4): print(fresh.step(embeddings[i], hours[i]))Step(node=0, born=True, probability=1.0, new_story=1.0, surprise=-344.3348756540148) Step(node=1, born=True, probability=0.9999999992170956, new_story=0.9999999992170956, surprise=-339.71975513795644) Step(node=0, born=False, probability=0.9265275232131279, new_story=0.07347243770148036, surprise=-341.6424156863069) Step(node=2, born=True, probability=1.0, new_story=1.0, surprise=-338.65247768598493)
The first item always opens a node: the memory is empty. The third goes back to node 0 with probability 0.93, and the new-story slot keeps the rest. The surprise is a negative number because it is the logarithm of a density on a sphere of 256 dimensions, which can be far above 1; only its differences mean something. The read explains it.
Continuing a stream
Section titled “Continuing a stream”A stream can be read in several calls; pass the history back to keep one record. Reading in two halves gives exactly the same nodes as reading at once, because the memory carries its whole state between calls:
split = Memory().fit_projection(reference)first = split.read(embeddings[:20_000], hours[:20_000])both = split.read(embeddings[20_000:], hours[20_000:], history=first)len(both), bool(np.array_equal(both.node, history.node))(49090, True)
Inspecting the memory now
Section titled “Inspecting the memory now”Memory.nodes() returns the active nodes as they are after the last item: identity, mass, concentration, centre and time of birth. At the end of the Portal stream, 31 December 2024, the table is full (256 rows) and the heaviest node holds a mass of 16:
nodes = memory.nodes()order = np.argsort(-nodes.mass)[:5]for i in order: print(f"node {nodes.ident[i]:>5} mass {nodes.mass[i]:6.2f} kappa {nodes.concentration[i]:5.0f}")len(nodes), nodes.centre.shapenode 7010 mass 15.94 kappa 193 node 5153 mass 5.11 kappa 307 node 7073 mass 2.54 kappa 249 node 7060 mass 2.51 kappa 500 node 7014 mass 2.51 kappa 500 (256, (256, 256))
Saving
Section titled “Saving”The memory saves its configuration, projection and table; the history, everything about every item. The memory stays small whatever the stream’s length; the history grows with it (here, 49,090 points of 256 numbers in single precision).
import os
memory.save("memory.npz")history.save("history.npz")print(f"memory.npz {os.path.getsize('memory.npz') / 1e6:.1f} MB")print(f"history.npz {os.path.getsize('history.npz') / 1e6:.1f} MB")memory.npz 2.6 MB history.npz 47.3 MB
memory = Memory.load("memory.npz")history = History.load("history.npz")Each step compares the item with at most max_nodes centres of d numbers, so the cost per item does not grow with the stream. In practice the sentence encoder costs far more per item than the memory.
import time
timed = Memory().fit_projection(reference)begin = time.perf_counter()timed.read(points[:5_000], hours[:5_000], projected=True)print(f"{(time.perf_counter() - begin) / 5_000 * 1e3:.2f} ms per item")0.45 ms per item
Determinism and rounding
Section titled “Determinism and rounding”Reading is deterministic: the same stream, configuration and projection give the same nodes, item by item, with no sampling and no random initialisation. Each item is decided once, with the memory as it was before it, and never revised; another order would give another partition, as with any online method.
It is not, however, insensitive to rounding. A near tie at a birth changes everything after it, so changes at the level of floating-point rounding, a different BLAS, another number of threads, single instead of double precision, can change node identities, mostly with the soft write. In the paper, perturbations of moved B³ F1 by a standard deviation of 0.001 to 0.002 on the test streams. Compare runs by their scores and their stories, not by node numbers.