Skip to content

Keep trained state across a segment; outputs carry segment and config_id - #37

Merged
cboulay merged 3 commits into
devfrom
cboulay/segment-keeps-trained-state
Nov 16, 2026
Merged

cboulay merged 3 commits into
devfrom
cboulay/segment-keeps-trained-state

Conversation

@cboulay

@cboulay cboulay commented Nov 16, 2026

Copy link
Copy Markdown
Member

This adopts the new stream model in ezmsg-learn (background: ezmsg-org/ezmsg-sigproc#256). segment (an int on AxisArray) changes only when the source measured a gap: the data is no longer continuous. config_id (an int digest, 0 = undeclared) says whether the stream configuration is the same. A gap is not a new model, so a trained model should survive one.

Stack: this is the bottom of two. cboulay/config-id (narrower resets for trained models) sits on top of it. It is based on dev.

What changes

  1. Trained state survives a segment change. These processors override _reset_segment to keep their model or long-run statistics: sklearn, SGD, torch (simple and trainable), the old MLP, SSR/LRR, and SLDA (a classifier loaded from file). The RNN keeps its model and resets only its hidden state. The transformer decoder resets only its cache. LRR needs no override of its own: SSR keeps its fit, and the inner sigproc AffineTransformTransformer is used directly (it has no memory, so the new sigproc does not reset it at a gap).
  2. Outputs carry segment and a derived config_id. Outputs that are built fresh rather than copied from the input (the regressors, DecodeOutputAdapter, SGD and SLDA classifier messages, incremental decomposition, sklearn/torch/RNN/transformer/MLP heads, RefitKalman) now carry the input's segment and a config_id derived with sigproc's ConfigChain/ConfigJoin from the input's id plus what the stage adds (output channels, classes, the decomposed layout, the state dimension). An undeclared input gives an undeclared output.
  3. partial_fit goes through _prepare_state for the incremental decomposition and SSR/LRR, so training messages reset state by the same rule as signal messages instead of a hand-rolled hash comparison.
  4. Empty (end-of-stream) messages pass through the adaptive and static linear regressors, DecodeOutputAdapter, sklearn, SGD and the RNN as empty outputs with the right channels. Before, they were predicted on (which sklearn refuses) or dropped. The incremental decomposition and SLDA already returned their empty template; it now carries segment and config_id too.
  5. SeqSeqSampler restarts its buffer at a gap. A message whose segment or config_id differs from the buffer's starts a new buffer, rather than being joined to samples from before the gap. An empty message no longer fails when trimming.
  6. A sklearn model fitted before the first call is kept. fit()/partial_fit() reset state outside the hash check, so the first __call__ saw a new hash and rebuilt the model, which threw the training away. Both now go through _prepare_state, and SklearnModelProcessor's hash is the input feature count, the only thing the model is built for. That narrower hash is needed for this fix (the fit() dummy message and the first real message differ in everything else), so it lands here rather than in the next PR. The output channel axis is rebuilt with the model.

Behaviour changes for existing users

  • At a gap, the processors in (1) keep their trained model or statistics. Before, a segment change ran the full reset. The RNN hidden state and the transformer cache still reset.
  • RefitKalman is unchanged here: a gap still runs its full reset. The next PR narrows that.
  • Outputs of these stages carry the input's segment and a new derived config_id, so downstream stages reset only when this stage's output configuration changes.
  • SGD returns an empty classifier message for an empty input. Before, it returned None. The other predictors return empty outputs where before they raised or returned None.
  • SeqSeqSampler no longer joins samples across a gap or a config change.
  • SklearnModelProcessor: a model trained with fit/partial_fit before the first call is now used. It resets only when the input feature count changes, not when labels, key or dtype change. After such a reset, the output channel axis follows the new model.

Testing

  • Full suite: 387 passed, 24 skipped at the tip, against ezmsg 3.10.0b7, ezmsg-baseproc 1.17.0 and ezmsg-sigproc 3.13.0.
  • New tests: tests/unit/test_stream_contract.py (models kept across a segment for SGD, LRR, sklearn, torch and RNN; LRR keeps accumulating on a new segment; derived config_id on ALR and IPCA outputs; windowed training does not reset the decomposition; empty messages pass; SeqSeqSampler restart), and in tests/unit/test_sklearn.py fit/partial_fit before the first call and a feature-count change rebuilding the model and channels.

Dependencies

  • ezmsg-baseproc >= 1.17.0: _reset_segment, resets decided from config_id, end_of_stream() (used in tests). It requires ezmsg 3.10.0b7 (AxisArray.config_id).
  • ezmsg-sigproc >= 3.13.0: ConfigChain / ConfigJoin, and AffineTransformTransformer keeping its weights at a gap (LRR relies on this).
  • Both floors are raised in pyproject.toml.

A new segment means a gap in the data, not a new model. Processors
whose state is a trained model or long-run statistics (sklearn, SGD,
torch, MLP, LRR/SSR, SLDA, incremental decomposition) now keep it at a
segment change; RNN and the transformer decoder drop only their hidden
state or cache.

Outputs built fresh rather than copied from the input now carry the
input's segment and a config_id derived with ConfigChain/ConfigJoin from
the input's id and what the stage adds (output channels, classes, the
decomposed layout), so downstream stages reset only when this stage's
output configuration changes.

partial_fit on the incremental decomposition and SSR goes through
_prepare_state, so training messages reset state by the same rule as
signal messages. Empty (end-of-stream) messages pass through the
models without being predicted on.
The sampler appended every message to its buffer, so samples from
before a gap (or from another stream configuration) were joined to the
ones after it as if continuous. A message whose segment or config_id
differs from the buffer's now starts a new buffer. An empty
(end-of-stream) message no longer fails when trimming.
fit() and partial_fit() reset state directly, outside the hash check, so
the first __call__ saw a new hash and rebuilt the model, discarding the
training. Both now go through _prepare_state, and the processor's hash
is the input feature count, the only thing the model is built for: a
fit on a bare array and the first signal message agree, while a change
in feature count still rebuilds the model. The output channel axis is
rebuilt with the model, so it follows the new model's outputs.
@cboulay
cboulay merged commit 8b96234 into dev Nov 16, 2026
9 checks passed
@cboulay
cboulay deleted the cboulay/segment-keeps-trained-state branch November 16, 2026 23:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant