Start · 03
The mental model¶
Use rd.ESN(...).fit(training_data) for a preset, or connect layers and wrap
them in rd.Model for a custom DAG. Both support the same fit, forecast,
tune and save commands. The four ideas below explain what those commands do.
1 — The dynamics are designed, not learned¶
A reservoir is a recurrent network whose weights are chosen and then frozen: a connectivity structure (random, a graph, any matrix-building function), scaled to a target spectral radius, driven by your signal. It is a fixed nonlinear dynamical system that unfolds your input's history into hundreds of feature trajectories. Because nothing inside is learned, every structural choice is a hyperparameter; ResDAG therefore treats connectivity structure as a pluggable function rather than a fixed implementation detail.
Reservoir families are an open set. The same interface also holds
reservoirs with no randomness at all:
NGReservoir builds features from delayed inputs and
polynomial combinations — next-generation reservoir computing.
2 — Reservoir layers are stateful¶
The hidden state persists across forward calls — each reservoir family
defines its own shape, such as (batch, reservoir_size) for an
ESNLayer. That persistence is what lets a warmup pass hand a
synchronized reservoir to a forecast loop. The state-management API:
layer.reset_state() # forget everything (lazy re-init)
layer.get_state() # clone the live state
layer.set_state(saved) # restore a checkpoint
layer.set_random_state(batch_size=4)
model.reset_state() # every reservoir in an rd.Model or rd.ESN
By default, stored state is detached at forward-call boundaries, so
consecutive batches do not accumulate autograd history. Set
detach_state_between_calls=False deliberately, or use
model.forward_stateless(..., states=...) to carry attached state explicitly.
The raw graph's as_module() adapter can process independent batches without
changing stored states. A configured reset initializer supplies the starting state; otherwise
it is zero.
Explicit-state execution requires reservoirs to be separate functional graph
nodes. If a reservoir is hidden inside nn.Sequential or another module,
forward_stateless(), the default as_module() adapter and forecasting reject
the graph before running its operations. This prevents stored state from changing outside the
returned state dictionary. Ordinary stateful model(x) calls still support
those containers, as does as_module(stateful=True). Containers with only
pointwise layers, such as a linear readout followed by an activation, can remain
grouped.
Moving a model with .to(...) preserves its stored state values. An ordinary
batch-size change initializes a fresh state; a state explicitly restored with
set_state instead raises on a mismatched batch size to avoid discarding it.
3 — Readout training is a closed-form solve¶
Readouts are fitted algebraically on collected states rather than trained
by gradient descent. The friendly rd.ESN defaults to
AutoReadoutLayer, a linear ridge readout that
checks numerical safety before choosing Cholesky, augmented QR or SVD. It
preserves the requested regularization and reports its choices. Lower-level
factories retain their explicit Ridge/Cholesky default. These solves need no
learning rate or epoch schedule. Other solvers plug into the same contract:
CGReadoutLayer (iterative conjugate gradient) and SVD/pinv/streaming
variants (see Theory · Readout solvers).
ESNTrainer.fit resets once and advances one continuous warmup-plus-training
prefix in dependency order. A readout's pre-hook fits its training suffix before
the readout produces the complete prefix for downstream nodes. Multiple heads
share upstream work; a later reservoir warms up on the fitted upstream mapping
without replaying it.
Expose these dependencies as separate functional graph nodes or standard
torch.nn.Sequential containers. A custom module that hides both readouts
and reservoirs must expose that order before algebraic fitting.
Because every readout is an ordinary nn.Linear underneath,
gradient-based training remains available: freeze the reservoir and train
a deep head with Adam, or set trainable=True and backpropagate through
everything. The Train page covers all three
approaches.
The pure-PyTorch quickstart
Prefer a normal optimizer loop? ReservoirFeatureExtractor wraps a
reservoir as a plain nn.Module that drops into nn.Sequential ahead
of any head, frozen by default — so you compute its features once under
torch.no_grad() and train the head with Adam like any other model.
The runnable snippet is
Train · Path 2 — frozen reservoir, gradient head,
and Scale & deploy
embeds the same frozen backbone inside a classifier nn.Module.
4 — Forecasting is autoregressive feedback¶
A trained model maps signal now → signal one step ahead. Forecasting
closes the loop: after a teacher-forced warmup, each prediction is fed back
as the next input, horizon times. Exogenous drivers slot in alongside the
feedback with a fixed timing convention: drivers
for the forecast window start exactly where the warmup drivers ended.
For scikit-learn users
The common model interface separates fitting from autonomous rollout:
| scikit-learn | ResDAG |
|---|---|
est.fit(X, y) |
model.fit(data) learns training-only scaling, warms up the reservoir and fits the readout |
| forecasting future values | model.forecast(history=data, horizon=...) synchronizes on observations, then feeds predictions back into the model |
| applying a trained model to supplied inputs | model(x) returns predictions in original units and carries reservoir state |
The ESN preset handles both phases in one PyTorch module:
ESN(...).fit(series).forecast(horizon=...). It learns scaling only from
training data and returns original-unit tensors:
import torch
from resdag import ESN
series = torch.randn(2000, 3).cumsum(0) # (time, features)
prediction = ESN(reservoir_size=300).fit(series).forecast(horizon=200)
esn.graph exposes the raw graph. Build custom branches with the same
workflow through rd.Model; put either directly inside nn.Sequential.
The Build section expands on idea 1 — layers, topologies, initializers, and architectures; the Workflows section covers ideas 2–4 in practice — training, forecasting, tuning, and deployment.