A learned Attention reduction can retain enough information to approximate the sum of six visible values.
Before testing a nested hierarchy, check whether the model can learn a sum at one level. Every observation here contains six independently drawn amounts, all of which contribute to the answer.
Seed 2600 on gpu: 1 of 1 behavioral checks met. See the measurements below.
Insights
The model can learn to approximately add six visible numbers. All six independently drawn values are visible, making this a useful positive control before testing more complicated nested statistics.
The accuracy check compares held-out error with a constant training-mean baseline. Meeting it demonstrates substantially lower error, but this experiment does not include a prediction-level permutation control. The task always contains six items.
Do not extend this result to variable cardinality, arbitrary hierarchy depth, or exact arithmetic. The nested-cardinality case asks whether local structure survives successive reductions; this control only establishes that the model can learn a numerical sum at one level.
Setup
Code
"""P039: learn a fixed-width sum through Attention reduction.All six independent values must contribute. Test RMSE is compared with theconstant training-mean predictor; learned sums remain approximations."""from collections.abc import Iteratorfrom functools import partialimport lightning.pytorch as litimport numpy as npimport torchfrom reporting import reportimport relflow as rfPROOF_ID ="P039"
Examples
Each record contains exactly six visible amounts. global_total is their mathematical sum, supplied as a hidden Number target rather than an input.
Flipping the final amount changes the total by −1.8. This illustrates the sum rule; the executable proof scores independently generated records rather than asserting this particular paired intervention.
A zero total can arise from six present, nonzero values. These exact answers illustrate the target function; the learned model is assessed by aggregate approximation error, not exact predictions on these examples.
Synthetic data and controls
Code
def records(*, rows: int, seed: int) -> Iterator[dict]:"""Yield six transactions and their total.""" rng = np.random.default_rng(seed)for _ inrange(rows): amounts = rng.uniform(-1.0, 1.0, size=6)yield {"transactions": [{"amount": float(amount)} for amount in amounts],"global_total": float(amounts.sum()), }def prediction(model: rf.Model, rows: list[dict], source: str, target: str) -> np.ndarray:"""Predict one hidden scalar from its visible repeated context.""" inputs = [{source: row[source]} for row in rows] output = model.predict(inputs).to_pylist()return np.asarray([row["predictions"][f"record/{target}"]["content"] for row in output], dtype=np.float64)def rmse(actual: np.ndarray, predicted: np.ndarray |float) ->float:returnfloat(np.sqrt(np.mean(np.square(actual - predicted))))
Model tree
Figure 1: The transaction branch and root each reduce to one Attention output. Six visible values supply the hidden total target.
How it works
Both the transaction branch and the root use rf.Attention reduction. The model must transform the visible amounts into a summary from which a numerical decoder can approximate their sum. Fixed length makes this a simpler control than inferring totals through several variable-size groups.
Training, validation, and test draw 512, 128, and 256 independent observations. Each amount is sampled uniformly from −1 to 1. After 400 deterministic steps, the model predicts totals from the transaction inputs alone.
Add the nested, regrouping-invariant global_total case: the present control has only one collection level. The family also calls for a largest_session_average target, broader value patterns, multiple Attention output counts, and nested capacity ranges, followed by repeated-seed gates.
The nested-cardinality proof tests a statistic whose answer changes when those same amounts are regrouped. Use preprocessing when a sum must be exact.
Reproduce
Run by stable ID from the repository root:
uv run python proofs/run.py P039
Or run the self-contained script directly:
PYTHONPATH=proofs uv run python proofs/structure/attention_learns_fixed_width_sum.py
Add --accelerator gpu for CUDA or --seed 42 for another seeded experiment. --steps 2 checks execution with a short training budget; it is recorded as a smoke run.