Skip to content

RNN/PCNN: normalize constant inputs with variance 1; PCNN losses scaled by a flow - #94

Merged
PatrickHenkel1 merged 2 commits into
refactoring_v1.0.0from
rnn-constant-inputs-and-pcnn-flows
Nov 10, 2026
Merged

PatrickHenkel1 merged 2 commits into
refactoring_v1.0.0from
rnn-constant-inputs-and-pcnn-flows

Conversation

@PatrickHenkel1

Copy link
Copy Markdown
Collaborator

Two changes to the recurrent models. Both came up while using them as room temperature models in an MPC (AgentLib-MPC branch 143).

Fix: constant inputs (RNNModel, PCNNModel). The variance floor 1e-12 scaled a constant input column by 1e6. A tiny deviation at runtime, such as a value rounded by the MPC, then shifted the hidden states by up to 3. Constant columns are now normalized with variance 1, as in the single step models (new helper input_variance).

Feature: PCNN losses scaled by a flow. A loss can now be scaled by a flow input, b * V * (T_x - T), for example ventilation with the air flow V:

PCNNModel(heating={"load": None}, losses={"T_sup": None, "T_amb": None}, flows={"T_sup": "V_flow"})
The flow is not an input of f. Initial rates and the consistency check (1 - sum b > 0, using the largest flow in the training data) take the flow into account. Existing configs are unchanged.

Tests:

  • new regression test for the fix;
  • test_loss_scaled_by_a_flow (recovers the true rate, config round trip, invalid configs);
  • a contract case confirming that the AgentLib-MPC CasADi step function reproduces Keras for a PCNN with flow.

280 passed locally. test_deterministic_operations fails only in my environment and is unrelated to this PR. CHANGELOG updated.

PatrickHenkel1 and others added 2 commits November 9, 2026 23:58
The normalization of the recurrent models used the variance floor 1e-12, so a
constant input column (e.g. a fixed air flow in the training data) was scaled
by 1e6. Any later deviation from the training value, such as a rounded value in
an MPC (0.07222 instead of 0.072222), reached the network as a huge normalized
input; in a PCNN it shifted the hidden states by up to 3.

Constant columns are now normalized with variance 1, as in the single step
models (expr.blocks, ann_models.output_scale). New helper input_variance, used
by build_step_model and build_pcnn_step_model, with a regression test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A loss of the energy accumulator can be scaled by a flow input,
b * V * (T_x - T), e.g. a ventilation with the air flow V and the supply
temperature T_x. Before, every loss had a constant conductance, so a varying
air flow had to be learned by the recurrent network.

- PCNNModel(flows={loss input: flow input}); PCNNTerm.flow
- the step model multiplies the gradient by the flow (keras Multiply, translated
  by AgentLib-MPC); the flow is no input of f, like the controls
- the initial rates given as None are estimated with the scaled term
- the consistency check 1 - sum b > 0 uses the largest flow of the training data
- flows is only written to the config when used

Tests: estimated rate equals the true dt*cp/C of the one room data, closed loop
rollout, config round trip, invalid configurations, CasADi contract.

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

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 9e40bc8 into refactoring_v1.0.0 Nov 10, 2026
28 checks passed
@PatrickHenkel1
PatrickHenkel1 deleted the rnn-constant-inputs-and-pcnn-flows branch November 10, 2026 16:07
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