Skip to content

Phase 4: RNN warmup without init model, trajectory loss, step model export - #74

Merged
PatrickHenkel1 merged 1 commit into
refactoring_v1.0.0from
v1/phase-4-rnn
Nov 6, 2026
Merged

PatrickHenkel1 merged 1 commit into
refactoring_v1.0.0from
v1/phase-4-rnn

Conversation

@PatrickHenkel1

Copy link
Copy Markdown
Collaborator

Phase 4 of the 1.0.0 refactoring (Phases 1–3 are merged in refactoring_v1.0.0).

RNN without an initialization model

  • Warmup like in the MPC: A sequence of warmup_width + label_width steps starts from zero states. The warmup steps are not part of the loss. AgentLib-MPC (branch 143) warms the model up the same way. init_layer, init_features and warmup_columns_* are removed.
  • Predicting the change: For an output px.Change('T') the model predicts the trajectory of T, i.e. the measured start value plus the accumulated changes. The model is trained on this trajectory (loss_on='trajectory', alternatively 'outputs'), optionally with step weights (first_step_weight, step_weights).
  • Closed loop: If T is also an input, the prediction is fed back during training and evaluation, as in the MPC.
  • Evaluation: eval_horizon can be longer than label_width and shows the stability beyond the training horizon. td.horizon_metrics holds RMSE, MAE and bias per step, by_feature the same per output.
prep = PreprocessingMultiStep(inputs=[..., 'reaTZon_y'], output=px.Change('reaTZon_y'),
                              label_width='12h', warmup_width='6h', eval_horizon='24h')
m = RNNModel(rnn_layer='LSTM', rnn_units=16)

Export for AgentLib-MPC

{name}.keras now contains the step model rnn_step: [sequence, *states] -> [prediction, *states]. Its input layers are named *_input, because AgentLib-MPC recognizes inputs by that name.

Verified with a worktree of 143-feature-multistep-model-support:

  • Trained physXAI models exported with save_for_agentlib and translated with physXAI_2_agentlib_json give exactly the keras trajectory in the MPC of branch 143 (solve_mpc from its tests). That holds for RNN, GRU and LSTM, each with and without feeding back T. The maximum deviation is about 1e-6.
  • Branch 143's own tests (test_recurrent_ml_model.py, test_physXAI_plugin): 28 passed.
  • New contract test unittests/contract/test_agentlib_rnn.py. It is skipped with AgentLib-MPC main and runs as soon as the installed version supports RNNs.

Branch 143 needs no change. It finds the step model at the top level by its signature.

Further changes

  • Single step models: Single step models on PreprocessingMultiStep data are removed (evaluate_multi), along with closed_loop.py. The replacement is PreprocessingSingleStep(eval_horizon=...) from Phase 3.
  • Feature selection and executables: The feature selection and TAir_evaluateMultiStep.py / TAir_FeatureSelection.py were migrated to this. Their output was Change(T_zone), which has no base column; they now use Change(reaTZon_y), so that the multi step evaluation runs.
  • Batch size: It is set at RNNModel. In PreprocessingMultiStep it is ignored with a warning.
  • Fixed bugs:
  • fit no longer passes shuffle=True on top of the already shuffled tf.data pipelines, which removes the keras warning.

Tests

  • Unit tests: 110 passed, 1 skipped (the RNN contract test with AgentLib main).
  • Core RNN test: For RNN, GRU and LSTM, open and closed loop, as well as for absolute outputs, the evaluation matches a manual step-by-step application of the saved step model exactly (zero states, warmup, feedback).
  • AgentLib-MPC main: The test_physXAI_plugin tests and simple_mpc_nn_physXAI.run_example(testing=True) pass.
  • Executables: All executables pass the smoke run (1 epoch).
  • TAir_rnn.py with full epochs: 16 s, early stopping after 30 epochs. The BOPTEST data cover only 14 days, so with 6 h warmup and 24 h horizon only one window each fits into val and test. The horizon errors are therefore not meaningful.

🤖 Generated with Claude Code

…d export the step model

- RNNModel: no separate initialization model. A sequence of warmup_width + label_width
  steps starts from zero states, the warmup is not part of the loss (as in AgentLib-MPC).
- Change(T) outputs are accumulated to the trajectory of T from the measured start value,
  the loss runs on the trajectory (loss_on='trajectory' | 'outputs') with optional step
  weights. If T is also an input, it is fed back in training and evaluation (closed loop).
- The saved .keras file is the step model [sequence, *states] -> [prediction, *states];
  verified against the MPC of AgentLib-MPC branch 143 (RNN, GRU, LSTM, with and without
  feedback) and covered by a contract test that runs once AgentLib-MPC supports RNNs.
- TrainingDataMultiStep: sequences as views on the time series, eval_horizon longer than
  the training horizon, HorizonMetrics per output.
- Removed: init_layer, init_features, warmup_columns_*, closed_loop.py and the multi step
  evaluation of single step models on PreprocessingMultiStep (use eval_horizon of
  PreprocessingSingleStep; feature selection migrated).
- Fixes: RNN summary / verbosity / batch size, Rescaling with tensors, init_layer
  validator ('is not'), seed set in RNNModel.__init__, PINN pipelines returned None.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 6, 2026 06:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@PatrickHenkel1
PatrickHenkel1 merged commit bbbea26 into refactoring_v1.0.0 Nov 6, 2026
25 checks passed
@PatrickHenkel1
PatrickHenkel1 deleted the v1/phase-4-rnn branch November 6, 2026 10:10
@PatrickHenkel1 PatrickHenkel1 mentioned this pull request Nov 8, 2026
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.

2 participants