Quickstart
This page reads a real stream from start to end. Every block of code below was run to build these docs, and the box under it is what it printed.
Example data
Section titled “Example data”The examples use the Wikipedia Current Events Portal from 1 January 2016 to 31 December 2024: 49,090 items, each a summary of one to three sentences written by Wikipedia editors on the day (CC BY-SA). portal.npz holds the texts, their arrival times in hours since the first day, and their embeddings; reference.npy holds the embeddings of another stream, the paper’s development stream (English news of 2015), on which the projection is fitted.
import numpy as np
portal = np.load("portal.npz")texts = portal["texts"]hours = portal["hours"] # since 2016-01-01embeddings = portal["embeddings"] # 1,024 numbers per itemreference = np.load("reference.npy")print(f"{len(texts):,} items over {hours[-1] / 24:,.0f} days")print(texts[0])embeddings.shape, reference.shape49,090 items over 3,287 days A shooting takes place at a pub in Tel Aviv, Israel, leaving two dead and eight injured. The gunman managed to escape and possibly killed his accomplice, an Arab taxi driver, whose body was found nearby. The police identified the shooter as 30 year old muslim Nisa'at Melkham from Ar'ara and searched his home. ((49090, 1024), (4519, 1024))
Two helpers convert between dates and the stream’s hours:
from datetime import date, timedelta
start = date.fromisoformat(str(portal["start"]))
def hours_at(day): return (date.fromisoformat(day) - start).days * 24.0
def day_of(t): return start + timedelta(hours=float(t))1. Embed the texts
Section titled “1. Embed the texts”SentenceEncoder is the paper’s representation: each sentence through all-roberta-large-v1, the mean of the unit sentence vectors. The embeddings in portal.npz were computed this way once, on a GPU; here three of them are recomputed on a CPU to check that they match:
from vmfstream.encode import SentenceEncoder
encoder = SentenceEncoder()fresh = encoder.encode(list(texts[:3]))fresh.shape, float(np.abs(fresh - embeddings[:3]).max())((3, 1024), 8.543673902750015e-05)
A difference of is rounding between devices. You do not need this extra if you already have embeddings from any encoder: the memory reads arrays.
2. Read the stream
Section titled “2. Read the stream”from vmfstream import Memory
memory = Memory() # the analytic configurationmemory.fit_projection(reference)history = memory.read(embeddings, hours)print(f"{history.node.max() + 1:,} nodes born")print(f"{len(memory.nodes())} in the table at the end")7,075 nodes born 256 in the table at the end
Memory() is the paper’s analytic configuration, chosen with the published encoder used here; Memory.main() is the configuration of its main result, chosen with an adapted encoder. Hyperparameters explains both. The read takes about half a millisecond per item, half a minute for the nine years.
The largest nodes, with the dates of their first and last item:
for s in history.summary()[:5]: print(f"node {s.node:>5} {s.size:>5} items", f"{day_of(s.first)} to {day_of(s.last)}")node 5153 3451 items 2022-03-20 to 2024-12-31 node 3709 2198 items 2020-03-09 to 2021-03-03 node 0 1474 items 2016-01-01 to 2017-07-28 node 3749 1204 items 2020-03-23 to 2022-02-21 node 3864 1170 items 2020-06-02 to 2022-01-25
The largest nodes are kinds of report rather than single events. Reading their exemplars: node 5153 gathers armed attacks and airstrikes, mostly in Syria and the Horn of Africa, from 2022 to the end; 3709 the reports of new COVID-19 variants; 0, the very first node, knife and gun attacks; 3749 vaccine approvals; 3864 national COVID-19 case counts. Timelines shows how they rise and fade.
3. Ask about a theme
Section titled “3. Ask about a theme”Encode a query like any item and ask which nodes matched it around a date:
query = memory.project(encoder.encode(["Russian invasion of Ukraine"])[0])matches = history.query(query, time=hours_at("2022-02-24"), k=3)matches[(5073, 0.6188776843033941), (4802, 0.5125656839229904), (5038, 0.46890935325831506)]
and read what the best node holds, as of a month after the date:
node = matches[0][0]for i in history.exemplars(node, central=3, diverse=2, until=hours_at("2022-03-26")): print(day_of(hours[i]), texts[i][:100])2022-02-21 Russia says that it has killed five Ukrainian soldiers who tried to infiltrate its territory. Ukrain 2022-03-02 Russian Ground Forces say that they have captured the Black Sea port of Kherson. The Ukrainian Minis 2022-02-24 Russian troops enter the Chernobyl Exclusion Zone. Ukraine says that Russian forces have captured th 2022-03-20 Bulgaria says it will end gas imports from Russia's Gazprom when its 10-year deal with Gazprom expir 2022-02-24 Russia says that two Russian civilian ships have been hit by anti-ship missiles in the Sea of Azov,
Thematic retrieval covers queries, exemplars and the nodes alive on a day, including a query that goes wrong and how to notice.
4. Draw it
Section titled “4. Draw it”timeline draws the mass of the largest nodes over time; each band grows while its node receives items and thins with the half-life once they stop:
from vmfstream.viz import timeline
fig = timeline(history, top=8)fig.savefig("timeline.svg", bbox_inches="tight")For the whole memory day by day, node by node, see the memory explorer.
Reading item by item
Section titled “Reading item by item”read is a loop over step. A live system can call step as items arrive, and save the memory between runs:
live = Memory().fit_projection(reference)for e, t in zip(embeddings[:3], hours[:3]): print(live.step(e, t))live.save("memory.npz")Memory.load("memory.npz").nodes().massStep(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) array([1.9265, 1. ])
The first item opens node 0, the second opens node 1, and the third joins node 0. All three arrived on the first day, so nothing has been forgotten yet: node 0’s mass is 1 for its first item plus 0.93 of the third. With the soft write each node receives its posterior share, and the 7 % the posterior gave to a new story is not written anywhere.