ML Foundations
Align running batch counts when accumulate has an initial value
Account for the extra initial state before pairing running totals with input batches.

Evidence and testing
Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.
An initial state adds an output
itertools.accumulate produces running results from an iterable. When a non-None initial value is supplied, the initial value is included as the first output before the input elements are accumulated. A sequence of three batch sizes therefore produces four outputs, not three. [1]
A caller pairing each batch with its completed running count must distinguish the initial state from the results after each batch. A normal zip stops at the shorter input, so using it blindly can silently shift all pairs and omit the last running result. [2]
Consume the initial state explicitly
Run python examples/python_accumulate_initial_alignment.py. With an initial state of zero and batch sizes two, three and one, the running outputs are zero, two, five and six. A strict zip of the original batches with all four outputs raises ValueError because the lengths differ. [1, 2]
The example starts a fresh accumulator, takes its initial output using next, then pairs the remaining running totals with the batch sizes using strict zip. The result associates the three sizes with completed counts two, five and six. Assertions check the initial state and the aligned pairs.
Strict zip detects a length mismatch during consumption; it does not independently prove that matching-length sequences refer to the same records. Explicitly consuming the initial state is what establishes the intended offset in this fixture. [2]
Executed with Python 3.12.14; stdout and checksum are in private evidence.
"""Align running counts with input batches after an explicit initial state."""
from itertools import accumulate
batch_sizes = [2, 3, 1]
with_initial = list(accumulate(batch_sizes, initial=0))
print('with initial:', with_initial)
try:
list(zip(batch_sizes, with_initial, strict=True))
except ValueError:
print('strict alignment: length mismatch')
counts = accumulate(batch_sizes, initial=0)
initial = next(counts)
aligned = list(zip(batch_sizes, counts, strict=True))
print('initial state:', initial)
print('aligned pairs:', aligned)
assert initial == 0
assert aligned == [(2, 2), (3, 5), (1, 6)]
Recorded output
with initial: [0, 2, 5, 6] strict alignment: length mismatch initial state: 0 aligned pairs: [(2, 2), (3, 5), (1, 6)]
Choose an alignment contract
If the application needs a value before each batch rather than after it, the desired offset is different. Write that contract down and test the first and final batch as well as empty input. Do not discard an output simply because a length check fails.
The demonstration uses a finite list and integer addition. It does not test custom accumulation functions, distributed ordering, streaming failures, memory limits or performance. Python versions must support the initial and strict keyword arguments used by the example.