Learn a total from independent item values when collection lengths vary within the training range.
Seed 3609 on gpu: 2 of 2 behavioral checks met. See the measurements below. The experiment code has changed since this run; these measurements describe its earlier version.
Insights
Attention can learn totals across varying collection lengths without a supplied count field. Independent signed amounts prevent count alone from predicting the answer. The reduction retains additive evidence and present-token count alongside its learned summary, providing a route for both content and multiplicity to survive.
The held-out accuracy and item-permutation controls support this route within the trained length range. They do not establish a general sum rule: a model could still learn relationships specialized to familiar lengths. The unseen-length proof separately checks longer bags and duplication. Treat this case as evidence for ordinary interpolation, rather than a guarantee that every accepted input shape will be handled accurately.
Setup
Code
"""Learned Attention can interpolate a sum over seen cardinalities.Check ordinary in-range accuracy and approximate order invariance.Run: uv run python proofs/run.py P001"""from __future__ import annotationsfrom collections.abc import Iteratorfrom copy import deepcopyimport lightning.pytorch as litimport numpy as npimport torchfrom reporting import reportimport relflow as rfPROOF_ID ="P001"TRAIN_MAX =6CAPACITY =12
Examples
Targets use mask=True; the labels below are supervision hidden from the encoder.
This is the first bag in a different order. Its label stays 1.2, and the permutation gate checks that predictions remain approximately unchanged.
Synthetic data and controls
Code
def random_records(*, rows: int, seed: int, minimum: int=1, maximum: int= TRAIN_MAX) -> Iterator[dict]:"""Draw variable-length numerical bags and their mean and sum."""ifnot1<= minimum <= maximum <= CAPACITY:raiseValueError(f"length range must satisfy 1 <= minimum <= maximum <= {CAPACITY}") rng = np.random.default_rng(seed)for _ inrange(rows): length =int(rng.integers(minimum, maximum +1)) values = rng.uniform(-1.0, 1.0, size=length) row: dict[str, object] = {"items": [{"amount": float(value)} for value in values],"mean_amount": float(values.mean()),"total": float(values.sum()), }yield rowdef permute(observations: list[dict], *, seed: int) ->list[dict]:"""Jointly permute complete items without changing any target.""" rng = np.random.default_rng(seed) rows = deepcopy(observations)for row in rows: items = row["items"] row["items"] = [items[index] for index in rng.permutation(len(items))]return rowsdef prediction(model: rf.Model, observations: list[dict]) -> np.ndarray: inputs = [{"items": row["items"]} for row in observations] output = model.predict(inputs)["predictions"].to_pylist()return np.asarray([row["record/total"]["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))))def score(*, train: list[dict], test: list[dict], predicted: np.ndarray) ->dict[str, float]:"""Compare held-out RMSE with the constant training-target mean.""" actual = np.asarray([row["total"] for row in test], dtype=np.float64) baseline = rmse(actual, float(np.asarray([row["total"] for row in train], dtype=np.float64).mean())) measured = rmse(actual, predicted)return {"rmse": measured, "baseline_rmse": baseline, "nrmse": measured / baseline}
Model tree
Figure 1: A single Attention output summarizes a branch with capacity twelve; training and this test use shorter bags.
How it works
Training, validation, and test records contain one through six independent amounts drawn from −1 to 1. Both the repeated item branch and root use learned rf.Attention reductions. There is no visible count input.
Attention’s reduction retains aggregate evidence and present-token count as well as its normalized learned summary. These representations offer a route to predicting sums with changing length. Independent values prevent count alone from solving the task; a complete-item permutation checks approximate order stability. Unseen-length tests separately check whether the learned behavior extends beyond interpolation.
Repeat across seeds and change capacities, widths, and nested placement. This in-range result alone establishes neither arbitrary-length generalization nor an exact arithmetic sum; empty and missing-item cases need separate checks.
The family’s promotion target is at least three core seeds and ten lightweight calibration seeds.
Reproduce
Run by stable ID from the repository root:
uv run python proofs/run.py P001
Or run the self-contained script directly:
PYTHONPATH=proofs uv run python proofs/aggregation/attention_sum_in_range.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.