Examples
========

Runnable example scripts live in the
`examples <https://github.com/chaobrain/braintrace/tree/main/examples>`__ directory.
The top-level scripts are grouped by filename prefix:

- ``0xx-`` for spiking neural network examples
- ``1xx-`` for rate-based recurrent neural network examples

The directory also includes two tutorial-style example series:

- ``examples/pp_prop/`` for ``braintrace.pp_prop`` / ``ES_D_RTRL`` examples
- ``examples/drtrl/`` for ``braintrace.D_RTRL`` examples

Most scripts can be run directly. For example:

.. code-block:: bash

   python examples/001-gif-snn-for-dms.py


Spiking Neural Networks
-----------------------

- **000-lif-snn-for-nmnist.py** - Trains a recurrent LIF-delta SNN on the
  N-MNIST dataset, using framed event inputs from ``tonic`` and the shared
  ``OnlineTrainer`` implementation based on ``IODimVjpAlgorithm``.

- **001-gif-snn-for-dms.py** - Trains a GIF recurrent SNN on a delayed
  matching-to-sample task generated by ``DMSDataset``, using the shared
  ``OnlineTrainer`` based on ``IODimVjpAlgorithm`` for online learning.

- **002-coba-ei-rsnn.py** - Trains an excitatory/inhibitory recurrent SNN on
  an evidence-accumulation task, with configurable CUBA or COBA synapses and
  online learning via ``ES_D_RTRL`` or ``D_RTRL``.

- **003-snn-memory-and-speed-evaluation-all.py** - Runs a synthetic benchmark
  over multiple sequence lengths and compares BPTT, ``ES_D_RTRL``, and
  ``D_RTRL`` in terms of training time and memory usage.

- **003-snn-memory-and-speed-evaluation-batched.py** - Trains and benchmarks
  an SNN on the DVSGesture dataset with the online algorithms running in
  ``brainstate.mixin.Batching()`` mode, reporting accuracy, speed, and memory.

- **003-snn-memory-and-speed-evaluation-vmap.py** - Trains and benchmarks the
  same DVSGesture setup while keeping per-sample states under ``vmap``,
  reporting accuracy, speed, and memory.

- **004-feedforward-conv-snn.py** - Builds a feedforward convolutional SNN with
  ``braintrace.nn.Conv2d``, ``LayerNorm``, IF neurons, and
  ``LeakyRateReadout``. By default it trains on N-MNIST, and the script also
  includes SHD data loading plus both online and BPTT trainers.


Rate-Based Recurrent Neural Networks
------------------------------------

- **100-gru-on-copying-task.py** - Trains a GRU on the copying task and
  compares online learning with ``ParamDimVjpAlgorithm`` against BPTT.

- **101-integrator-rnn.py** - Trains a ``MiniGRU``-based RNN to integrate a
  white-noise input signal and compares online ``D_RTRL`` with BPTT while
  plotting predictions and training losses.


Tutorial Series
---------------

- **pp_prop/** - A tutorial-linear example suite for
  ``braintrace.pp_prop`` (``ES_D_RTRL`` / ``IODimVjpAlgorithm``). The series
  covers neuron-model variants, batching with ``vmap`` versus batched
  primitives, single-step versus multi-step VJPs, sparse/LoRA/convolutional
  operators, Poisson-MNIST classification, and ``decay_or_rank`` sweeps. See
  ``examples/pp_prop/README.md`` and ``docs/tutorials/pp_prop.md``.

- **drtrl/** - A tutorial-linear example suite for ``braintrace.D_RTRL``.
  The series covers integrator basics, batching modes, VJP variants, LoRA and
  convolutional operators, row-scan MNIST classification, toy character-level
  language modeling, and the ``fast_solve`` plus
  ``normalize_matrix_spectrum`` knobs. See ``examples/drtrl/README.md`` and
  ``docs/tutorials/drtrl.md``.


Supporting Files
----------------

- **README.md** - Brief overview of the examples directory and its extra Python
  dependencies.

- **snn_models.py** - Shared SNN model and trainer definitions used by the
  ``000`` and ``001`` examples.

- **utils.py** - Shared command-line parser utilities used by the ``003-*``
  benchmark scripts.
