Skip to content
vdecaroPublic

Repository files navigation

torchdyno-logo
torchdyno-wordmark

PyPI - Version PyPI - Python Version pre-commit codecov pre-commit.ci status

TorchDyno is a PyTorch library for building models of temporal data from interchangeable parts, and training them with or without backpropagation. Reservoirs, state space models, oscillator and continuous-time networks, and random convolutions are all plain nn.Sequential compositions. Fit them with ridge regression, recursive least squares, FORCE, intrinsic plasticity or gradient descent.

TorchDyno is designed for researchers and practitioners studying unconventional recurrent models and learning rules.

Key features

  • Compose models: a model is an nn.Sequential of parts from four levels: weight matrices, sequence processing, readout and objectives. Each part is replaceable on its own.
  • Structure the weights: use dense, diagonal, block-diagonal and skew-symmetric matrices, each with its own initialisers and spectral scaling. nn.Linear works anywhere a matrix is expected.
  • Process sequences: run any recurrent cell step by step or with a parallel scan, use ROCKET and MiniRocket kernels, and combine layers in series, side by side, residually, reversed or in a closed loop.
  • Fit without gradients: use closed-form and online learners, alone or chained, next to backprop.
  • Handle real data: feed sequences of different lengths in one batch, and run benchmarks across seeds on NARMA, Mackey-Glass, Lorenz, memory capacity and UCR/UEA data.

Fitted and trained

Fitted readouts freeze the dynamics and fit only the readout:

  • in closed form, with ridge regression;
  • online, with recursive least squares or FORCE.

Intrinsic plasticity can adapt a reservoir first, without gradients. Nothing is backpropagated through time, so a fit takes seconds.

End-to-end training trains every parameter that requires a gradient. Freeze any part with requires_grad_(False) to train the rest around it. The benchmark guide compares the two on the same model and seeds.

Installation

TorchDyno is available on PyPI and requires Python >=3.11,<3.15. Install it with pip:

pip install torchdyno

Or add it to a uv project:

uv add torchdyno

PyTorch is installed with it. See the installation guide to choose a CPU or CUDA build, and for development setup.

Quickstart

Save this as quickstart.py. It builds an echo state network, a frozen random reservoir with a linear readout, and forecasts the Lorenz system one step ahead. Ridge regression fits the readout, keeping the penalty that scores best on the validation split.

from torch import nn

from torchdyno import seed_all
from torchdyno.benchmark import lorenz_benchmark
from torchdyno.learners import Ridge, evaluate
from torchdyno.matrices import Dense, spectral, uniform
from torchdyno.sequence import LeakyCell, Recurrent

seed_all(0)
data = lorenz_benchmark(length=2000, target_delay=1, washout=100)

reservoir = Recurrent(
    LeakyCell(
        input=Dense(3, 300, init=uniform(1.0)),
        recurrent=Dense(300, 300, init=spectral(uniform(1.0), 0.9)),
        leak=0.5,
    )
).requires_grad_(False)
esn = nn.Sequential(reservoir, nn.Linear(300, 3))

result = Ridge(
    l2=[1e-8, 1e-6, 1e-4],
    washout=data.washout,
    score_fn=data.task.primary,
).fit(esn, data.train_loader, data.val_loader)
print("chosen l2:", result.best)

nrmse = evaluate(esn, data.eval_loader, data.task.primary, washout=data.washout)
print(f"test NRMSE: {nrmse:.2e}")

The Dense matrices take their initialisers: spectral rescales the recurrent matrix to a spectral radius of 0.9. Recurrent runs the leaky cell over time, and the washout leaves out the first 100 steps, while the reservoir forgets its initial state.

Run it:

python quickstart.py

It prints the chosen penalty with its validation score, then the test error:

chosen l2: {'l2': 1e-08, 'score': 0.0002468338643666357}
test NRMSE: 4.89e-06

The last digits can differ across machines and PyTorch versions.

To adapt this experiment, swap any part on its own:

  • a ring reservoir with init=cycle(0.9);
  • three reservoirs in a Stack;
  • IntrinsicPlasticity chained before the ridge readout.

Continue with reading out a reservoir and training a model end to end.

Examples

Each example composes a well-known model from TorchDyno's parts, fits it and prints its score, in seconds:

Runnable source is available in examples/. Run one from the repository root with python -m examples.<name>, for example python -m examples.esn.

Documentation

  • Getting started: a reservoir read out by ridge, a model trained end to end, and benchmarks across seeds.
  • Core concepts: matrices, sequence processing, readout, objectives and learners.
  • API reference: public modules and interfaces.
  • Changelog: release history.

Contributing

See CONTRIBUTING.md for development setup, tests, documentation checks, and pull request guidelines.

Remarks

If you use TorchDyno in your work, cite it with the metadata in CITATION.cff, or with GitHub's Cite this repository button.

TorchDyno is released under the MIT License and maintained by Valerio De Caro. To get in touch, drop me an e-mail!

Releases

Used by

Contributors

Languages