Reference
Training¶
The trainer that warms up reservoir states, then fits every readout in the model algebraically — ridge regression, not gradient descent — in a single forward pass.
training
¶
ESN Training Utilities¶
This module provides trainers for ESN models that fit readout layers algebraically using ridge regression, rather than stochastic gradient descent.
| CLASS | DESCRIPTION |
|---|---|
ESNTrainer |
Trainer for fitting readout layers in ESN models. |
Examples:
>>> from resdag.training import ESNTrainer
>>> trainer = ESNTrainer(model)
>>> trainer.fit(
... warmup_inputs=(warmup,),
... train_inputs=(train,),
... targets={"output": target},
... )
See Also
resdag.layers.readouts.CGReadoutLayer : Readout with CG solver. resdag.core.ESNModel : ESN model class.
ESNTrainer
¶
ESNTrainer(model: ESNModel)
Trainer for ESN models with algebraic readout fitting.
This trainer fits all :class:ReadoutLayer instances in an ESN model
using algebraic methods (e.g., ridge regression) rather than gradient
descent. The fitting is performed efficiently in a single forward pass
using pre-hooks that intercept inputs to each readout.
The training process:
- Reset reservoir states
- Warmup phase: synchronize reservoir states with input dynamics
- Single forward pass with pre-hooks that fit each readout in topological order before it processes its input
Each readout handles its own fitting hyperparameters (e.g., alpha
for ridge regression is set during layer construction).
| PARAMETER | DESCRIPTION |
|---|---|
model
|
ESN model to train. Must contain at least one :class:
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
model |
The ESN model being trained.
TYPE:
|
Examples:
Basic training workflow:
>>> from resdag.training import ESNTrainer
>>> from resdag.core import ESNModel
>>>
>>> trainer = ESNTrainer(model)
>>> trainer.fit(
... warmup_inputs=(warmup_data,),
... train_inputs=(train_data,),
... targets={"output": train_targets},
... )
Training with driving inputs:
>>> trainer.fit(
... warmup_inputs=(warmup_feedback, warmup_driver),
... train_inputs=(train_feedback, train_driver),
... targets={"output": targets},
... )
Multi-readout training:
>>> trainer.fit(
... warmup_inputs=(warmup_data,),
... train_inputs=(train_data,),
... targets={
... "position": position_targets,
... "velocity": velocity_targets,
... },
... )
See Also
ESNModel : ESN model class. CGReadoutLayer : Conjugate gradient readout layer. ESNModel.forecast : Forecasting after training.
Notes
- Warmup and training data must have the same number of input tensors.
- Training data and targets must have the same sequence length.
- Target keys must match readout names (user-defined or auto-generated).
Source code in src/resdag/training/trainer.py
fit
¶
fit(warmup_inputs: tuple[Tensor, ...], train_inputs: tuple[Tensor, ...], targets: dict[str, Tensor]) -> None
Fit all readout layers in a single forward pass.
Uses pre-hooks to fit each readout layer just before its forward method executes. This ensures downstream layers receive outputs from already-fitted readouts.
| PARAMETER | DESCRIPTION |
|---|---|
warmup_inputs
|
Warmup sequences for state synchronization.
Format:
TYPE:
|
train_inputs
|
Training sequences for fitting.
Format:
TYPE:
|
targets
|
Dictionary mapping readout name to target tensor.
Each target shape:
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If no warmup or training inputs provided. If number of warmup and training inputs don't match. If any readout is missing from targets. If target sequence length doesn't match training inputs. |
Notes
After calling fit(), all readouts will have is_fitted=True and the model is ready for inference or forecasting.
Emits a UserWarning if targets dict contains names not matching any readout.
Examples:
>>> trainer = ESNTrainer(model)
>>> trainer.fit(
... warmup_inputs=(warmup_data,),
... train_inputs=(train_data,),
... targets={"output": targets},
... )
>>> print(model.CGReadoutLayer_1.is_fitted)
True
Source code in src/resdag/training/trainer.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
fit_stream
¶
Fit :class:IncrementalRidgeReadoutLayer readouts over a stream of chunks.
The streaming counterpart of :meth:fit. Instead of fitting from one
in-memory (B, T, F) block, it warms the reservoir once and then
consumes chunks one at a time — e.g. windows yielded by a
:class:torch.utils.data.DataLoader — accumulating each readout's ridge
sufficient statistics with
:meth:~resdag.layers.IncrementalRidgeReadoutLayer.partial_fit and solving
once at the end with
:meth:~resdag.layers.IncrementalRidgeReadoutLayer.finalize. No more than a
single chunk's states are ever held in memory, which is what lets a
sequence too long to materialise — or one streamed off disk — be fitted.
Because the reservoir is stateful, the chunks must be contiguous in time and in order: state flows from the end of one chunk into the start of the next, exactly as it would in a single long forward pass. Shuffling the chunks would desynchronise the reservoir and is not supported.
Every readout in the model must be an
:class:~resdag.layers.IncrementalRidgeReadoutLayer (the only readout with a
partial_fit / finalize interface). The accumulators are reset at
the start, so calling fit_stream again re-fits from scratch.
| PARAMETER | DESCRIPTION |
|---|---|
warmup_inputs
|
Warmup sequences for state synchronization, format
TYPE:
|
chunks
|
An iterable yielding
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If no warmup inputs are provided, if a chunk's input arity does not match the warmup arity, if any readout is missing from a chunk's targets, or if a target's sequence length does not match its chunk's input length. |
TypeError
|
If any readout in the model is not an |
RuntimeError
|
If |
Notes
After fit_stream returns, every readout has is_fitted=True and
the model is ready for inference or forecasting.
Examples:
>>> from resdag.layers import IncrementalRidgeReadoutLayer
>>> trainer = ESNTrainer(model) # model uses IncrementalRidgeReadoutLayer
>>> def chunk_stream():
... for x, y in dataloader: # contiguous windows
... yield (x,), {"output": y}
>>> trainer.fit_stream(
... warmup_inputs=(warmup_data,),
... chunks=chunk_stream(),
... )
See Also
fit : Single-pass in-memory fitting. resdag.layers.IncrementalRidgeReadoutLayer : The streaming readout.
Source code in src/resdag/training/trainer.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 | |
fit_force
¶
fit_force(warmup_inputs: tuple[Tensor, ...], targets: Tensor, *, forgetting: float | None = None, delta: float | None = None, initial_feedback: Tensor | None = None, reset: bool = True) -> Tensor
FORCE-train the readout to autonomously generate a target pattern.
Implements FORCE learning (Sussillo & Abbott 2009, "Generating Coherent
Patterns of Activity from Chaotic Neural Networks"): the reservoir is run
closed-loop — driven at every step by the network's own readout
output fed back as the next input — while the single
:class:~resdag.layers.RLSReadout is updated online (one rank-1 RLS step
per timestep) toward the target signal d_t at that step. Because the
readout is trained inside the feedback loop it will actually run in, the
fed-back output progressively locks onto the target; after training the
network free-runs and reproduces the pattern autonomously.
When to use FORCE (honest scope)
FORCE's value is training the readout inside the closed loop, so its weights are fit against the states the loop actually visits rather than the teacher-forced states an open-loop fit sees. Its classic advantage is stabilising autonomous generation in regimes where an open-loop teacher-forced ridge fit would diverge in closed loop — high-dimensional or strongly chaotic reservoirs where teacher-forced and free-running state trajectories pull apart and open-loop errors compound.
It is not a free win over open-loop ridge in general. For a clean,
low-dimensional periodic target trained over a long window, an open-loop
teacher-forced ridge readout (:meth:fit) is already an excellent
closed-loop generator — the smooth target leaves little error to compound
— and FORCE will match it rather than beat it (empirically, across
seeds, FORCE reproduces such a target to a small fraction of its scale and
stays competitive with ridge, but any seed-specific win is not a
guaranteed property). Reach for FORCE when the reservoir is chaotic enough
that the open-loop free run is unstable, or when the target is
non-stationary and you want the online / forgetting machinery of
:class:RLSReadout; for a stationary, easily-generated target, the
simpler :meth:fit is often sufficient.
Per step t (after an optional teacher-forced warmup that settles the
reservoir on the target):
- Feed the current feedback
z_t(the network's own last output, or the warmup's for the first step) through the model, reading the readout's input feature vectorr_t(reservoir state, possibly concatenated with the input, exactly as the graph presents it). - RLS-update the readout toward
d_tusingr_t— the a-priori errord_t - W_t^\top r_tdrives a rank-1 update ofWand the inverse-correlation matrixP(see :class:RLSReadout). - Recompute the output
z_{t+1} = W_{t+1}^\top r_twith the updated weights and feed it back as the next step's input.
The readout's RLS state (P and the weights) is reset at the start of
the FORCE pass (via :meth:RLSReadout.reset_state) so a re-run trains
from scratch; pass forgetting / delta to override the readout's
recursion hyperparameters for the pass.
| PARAMETER | DESCRIPTION |
|---|---|
warmup_inputs
|
Teacher-forced warmup driving the reservoir open-loop with the true
signal to settle its state before the closed-loop FORCE phase.
Format
TYPE:
|
targets
|
The pattern to generate, shape
TYPE:
|
forgetting
|
Overrides the readout's forgetting factor :math:
TYPE:
|
delta
|
Overrides the readout's :math:
TYPE:
|
initial_feedback
|
Feedback of shape
TYPE:
|
reset
|
If
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The closed-loop training outputs of shape |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the model's readout is not an :class: |
ValueError
|
If the model has more than one readout (FORCE trains a single
generator readout), if |
Notes
- Single-readout, self-generating only. FORCE here trains exactly one
readout whose output is fed straight back as the model's feedback, so
the readout must be an
RLSReadoutand itsout_featuresmust equal the feedback dimension. Multi-readout or driven-generation setups are out of scope and rejected with a clear error. - Driven models. If the model has driver inputs, pass the driver
warmup slices in
warmup_inputs; during the closed-loop phase the drivers are held at their last warmup value (FORCE classically targets autonomous generation, not driven mapping). A feedback-only model is the common case. - Reservoir regime. FORCE works best with a chaotic reservoir
(spectral radius
> 1, e.g.1.5); the online readout tames the chaos into the target pattern. See the module example.
Examples:
>>> from resdag.models import classic_esn
>>> from resdag import RLSReadout
>>> from resdag.training import ESNTrainer
>>> model = classic_esn(
... 600, feedback_size=1, output_size=1, spectral_radius=1.5,
... readout=RLSReadout, readout_name="output", readout_bias=False,
... )
>>> trainer = ESNTrainer(model)
>>> trainer.fit_force(
... warmup_inputs=(target[:, :200, :],),
... targets=target[:, 200:, :],
... delta=1.0,
... )
>>> generated = model.forecast(target, horizon=1000)
See Also
fit : Open-loop teacher-forced algebraic readout fitting. resdag.layers.RLSReadout : The online RLS readout FORCE drives. resdag.core.ESNModel.forecast : Free-run generation after FORCE training.
Source code in src/resdag/training/trainer.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 | |