### Install SpikingJelly (Development) Source: https://github.com/fangwei123456/spikingjelly/blob/master/README.md Installs the latest development version of SpikingJelly from source by cloning the GitHub repository and installing locally. ```bash git clone https://github.com/fangwei123456/spikingjelly.git cd spikingjelly pip install . ``` -------------------------------- ### Install TensorBoard Source: https://github.com/fangwei123456/spikingjelly/blob/master/spikingjelly/activation_based/examples/ILC-SAN/README.md Install the TensorBoard library using pip. This is a dependency for visualization. ```shell pip install tensorboard ``` -------------------------------- ### OTTT Online Training Example Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/APIs/spikingjelly.activation_based.functional.online_learning.md Demonstrates how to use the ottt_online_training function for training a spiking neural network with the OTTT method. It shows network setup, optimizer initialization, and the training loop with online parameter updates. ```python from spikingjelly.activation_based import neuron, layer, functional net = layer.OTTTSequential( nn.Linear(8, 4), neuron.OTTTLIFNode(), nn.Linear(4, 2), neuron.LIFNode() ) optimizer = torch.optim.SGD(net.parameters(), lr=0.1) T = 4 N = 2 online = True for epoch in range(2): x_seq = torch.rand([N, T, 8]) target_seq = torch.rand([N, T, 2]) functional.ottt_online_training( model=net, optimizer=optimizer, x_seq=x_seq, target_seq=target_seq, f_loss_t=F.mse_loss, online=online, ) functional.reset_net(net) ``` -------------------------------- ### MNIST CSNN Example Help Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/tutorials/en/lava_exchange.md Displays the command-line arguments available for the MNIST CSNN example script. Use this to understand configurable parameters. ```shell (lava-env) wfang@mlg-ThinkStation-P920:~/tempdir/w1$ python -m spikingjelly.activation_based.examples.lava_mnist -h usage: lava_mnist.py [-h] [-T T] [-b B] [-device DEVICE] [-data-dir DATA_DIR] [-channels CHANNELS] [-epochs EPOCHS] [-lr LR] [-out-dir OUT_DIR] options: -h, --help show this help message and exit -T T simulating time-steps -b B batch size -device DEVICE device -data-dir DATA_DIR root dir of the MNIST dataset -channels CHANNELS channels of CSNN -epochs EPOCHS training epochs -lr LR learning rate -out-dir OUT_DIR path for saving weights ``` -------------------------------- ### Install Gym (v0.18.0) Source: https://github.com/fangwei123456/spikingjelly/blob/master/spikingjelly/activation_based/examples/ILC-SAN/README.md Install the Gym library version 0.18.0 using pip. This is a dependency for reinforcement learning environments. ```shell pip install gym==0.18.0 ``` -------------------------------- ### Step Quantize Example Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/APIs/spikingjelly.activation_based.lava_exchange.md Demonstrates the step quantize function with a given step size. Requires matplotlib and torch. ```python # plt.style.use(['science', 'muted', 'grid']) fig = plt.figure(dpi=200, figsize=(6, 4)) x = torch.arange(-4, 4, 0.001) plt.plot( x, lava_exchange.step_quantize(x, 2.0), label="quantize(x, step=2)" ) plt.plot(x, x, label="y=x", ls="-.") plt.legend() plt.grid(ls="--") plt.title("step quantize") plt.xlabel("Input") plt.ylabel("Output") plt.savefig( "./docs/source/_static/API/activation_based/lava_exchange/step_quantize.svg" ) plt.savefig( "./docs/source/_static/API/activation_based/lava_exchange/step_quantize.pdf" ) ``` -------------------------------- ### Complete Example: zlib Compression for Spikes Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/tutorials/en/monitor.md A full example demonstrating how to compress and decompress spike data using `zlib` and `tensor_cache` within a SpikingJelly network. Note that `zlib` operates on the CPU, so data transfer might impact performance if spikes are originally on the GPU. ```python import torch import torch.nn as nn import zlib import numpy as np from spikingjelly.activation_based import monitor, neuron, functional, layer from spikingjelly.activation_based.cuda_kernel import tensor_cache def compress(spike: torch.Tensor): spike_b, s_dtype, s_shape, s_padding = tensor_cache.float_spike_to_bool(spike) spike_cb = zlib.compress(spike_b.cpu().numpy().tobytes()) return spike_cb, s_dtype, s_shape, s_padding def decompress(spike_cb, s_dtype, s_shape, s_padding): spike_b = torch.frombuffer(zlib.decompress(spike_cb), dtype=torch.uint8) return tensor_cache.bool_spike_to_float(spike_b, s_dtype, s_shape, s_padding) net = nn.Sequential( layer.Linear(8, 4), neuron.IFNode(), layer.Linear(4, 2), neuron.IFNode() ) for param in net.parameters(): param.data.abs_() functional.set_step_mode(net, 'm') spike_seq_monitor = monitor.OutputMonitor(net, neuron.IFNode, function_on_output=compress) T = 4 N = 1 x_seq = torch.rand([T, N, 8]) with torch.no_grad(): net(x_seq) for item in spike_seq_monitor.records: print(decompress(*item)) ``` -------------------------------- ### Complete Code Example for Hybrid STDP and Gradient Descent Training Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/tutorials/en/stdp.md Presents the full script combining all the previously shown steps for training an SNN with both STDP and gradient descent. It includes imports, hyper-parameter definitions, network construction, optimizer setup, data generation, training loop logic, and state resetting. ```python import torch import torch.nn as nn import torch.nn.functional as F from torch.optim import SGD, Adam from spikingjelly.activation_based import learning, layer, neuron, functional T = 8 N = 2 C = 3 H = 32 W = 32 lr = 0.1 ``` -------------------------------- ### Complete Example: Hybrid STDP and Gradient Descent Training Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/tutorials/cn/stdp.md Presents the full code for a hybrid training scenario, integrating all previously shown components. This serves as a runnable example for implementing combined learning rules in SNNs. ```python import torch import torch.nn as nn import torch.nn.functional as F from torch.optim import SGD, Adam from spikingjelly.activation_based import learning, layer, neuron, functional T = 8 N = 2 C = 3 H = 32 W = 32 lr = 0.1 tau_pre = 2. tau_post = 100. step_mode = 'm' def f_weight(x): return torch.clamp(x, -1, 1.) net = nn.Sequential( layer.Conv2d(3, 16, kernel_size=3, stride=1, padding=1, bias=False), neuron.IFNode(), layer.MaxPool2d(2, 2), layer.Conv2d(16, 16, kernel_size=3, stride=1, padding=1, bias=False), neuron.IFNode(), layer.MaxPool2d(2, 2), layer.Flatten(), layer.Linear(16 * 8 * 8, 64, bias=False), neuron.IFNode(), layer.Linear(64, 10, bias=False), neuron.IFNode(), ) functional.set_step_mode(net, step_mode) instances_stdp = (layer.Conv2d, ) stdp_learners = [] for i in range(net.__len__()): if isinstance(net[i], instances_stdp): stdp_learners.append( learning.STDPLearner(step_mode=step_mode, synapse=net[i], sn=net[i+1], tau_pre=tau_pre, tau_post=tau_post, f_pre=f_weight, f_post=f_weight) ) params_stdp = [] for m in net.modules(): if isinstance(m, instances_stdp): for p in m.parameters(): params_stdp.append(p) params_stdp_set = set(params_stdp) params_gradient_descent = [] for p in net.parameters(): if p not in params_stdp_set: params_gradient_descent.append(p) optimizer_gd = Adam(params_gradient_descent, lr=lr) optimizer_stdp = SGD(params_stdp, lr=lr, momentum=0.) x_seq = (torch.rand([T, N, C, H, W]) > 0.5).float() target = torch.randint(low=0, high=10, size=[N]) optimizer_gd.zero_grad() optimizer_stdp.zero_grad() y = net(x_seq).mean(0) loss = F.cross_entropy(y, target) loss.backward() optimizer_stdp.zero_grad() for i in range(stdp_learners.__len__()): stdp_learners[i].step(on_grad=True) optimizer_gd.step() optimizer_stdp.step() functional.reset_net(net) for i in range(stdp_learners.__len__()): stdp_learners[i].reset() ``` -------------------------------- ### Display Training Arguments Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/tutorials/en/conv_fashion_mnist.md Prints the available command-line arguments for the conv_fashion_mnist example. This helps in understanding and configuring training parameters. ```shell (sj-dev) wfang@Precision-5820-Tower-X-Series:~/spikingjelly_dev$ python -m spikingjelly.activation_based.examples.conv_fashion_mnist -h usage: conv_fashion_mnist.py [-h] [-T T] [-device DEVICE] [-b B] [-epochs N] [-j N] [-data-dir DATA_DIR] [-out-dir OUT_DIR] [-resume RESUME] [-amp] [-cupy] [-opt OPT] [-momentum MOMENTUM] [-lr LR] [-channels CHANNELS] Classify Fashion-MNIST optional arguments: -h, --help show this help message and exit -T T simulating time-steps -device DEVICE device -b B batch size -epochs N number of total epochs to run -j N number of data loading workers (default: 4) -data-dir DATA_DIR root dir of Fashion-MNIST dataset -out-dir OUT_DIR root dir for saving logs and checkpoint -resume RESUME resume from the checkpoint path -amp automatic mixed precision training -cupy use cupy backend -opt OPT use which optimizer. SDG or Adam -momentum MOMENTUM momentum for SGD -lr LR learning rate -channels CHANNELS channels of CSNN -save-es SAVE_ES dir for saving a batch spikes encoded by the first {Conv2d-BatchNorm2d-IFNode} ``` -------------------------------- ### Install Optional Dependencies with uv Source: https://github.com/fangwei123456/spikingjelly/blob/master/CONTRIBUTING.md Installs SpikingJelly with optional dependencies, such as Triton. Refer to `pyproject.toml` for a list of available optional dependencies. ```bash uv pip install --editable ".[triton]" ``` -------------------------------- ### Install Triton Backend Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/index.md Install Triton version 3.3.1 to enable the Triton backend for SpikingJelly. SpikingJelly has been tested with this specific version. ```bash pip install triton==3.3.1 ``` -------------------------------- ### Training with Trainer Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/tutorials/en/train_large_scale_snn.md Instantiate the `Trainer` and parse command-line arguments to start the training process. This is the standard way to initiate training using the spikingjelly library. ```python trainer = Trainer() args = trainer.get_args_parser().parse_args() trainer.main(args) ``` -------------------------------- ### LowPassSynapse Example Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/APIs/spikingjelly.activation_based.layer.misc.md Demonstrates the usage of LowPassSynapse with input spikes and visualizes the output current over time. Requires PyTorch and Matplotlib. ```python T = 50 in_spikes = (torch.rand(size=[T]) >= 0.95).float() lp_syn = LowPassSynapse(tau=10.0) pyplot.subplot(2, 1, 1) pyplot.bar(torch.arange(0, T).tolist(), in_spikes, label="in spike") pyplot.xlabel("t") pyplot.ylabel("spike") pyplot.legend() out_i = [] for i in range(T): out_i.append(lp_syn(in_spikes[i])) pyplot.subplot(2, 1, 2) pyplot.plot(out_i, label="out i") pyplot.xlabel("t") pyplot.ylabel("i") pyplot.legend() pyplot.show() ``` -------------------------------- ### Import LIFNode from clock_driven (Old Version) Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/tutorials/en/migrate_from_legacy.md Example of importing the LIFNode from the `spikingjelly.clock_driven` package in older versions. ```python from spikingjelly.clock_driven import neuron lif = neuron.LIFNode() ``` -------------------------------- ### Import LIFNode from activation_based (New Version) Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/tutorials/en/migrate_from_legacy.md Example of importing the LIFNode from the `spikingjelly.activation_based` package in the new version. This replaces the old `clock_driven` import. ```python from spikingjelly.activation_based import neuron lif = neuron.LIFNode() ``` -------------------------------- ### Build Docs Locally Source: https://github.com/fangwei123456/spikingjelly/blob/master/CONTRIBUTING.md Follow these steps to build the documentation locally and preview changes. Ensure you have a Python virtual environment set up and dependencies installed from `docs/requirements.txt`. ```bash cd docs make html ``` -------------------------------- ### Run DVS Gesture Classification Training Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/tutorials/cn/classify_dvsg.md Command to run the DVS gesture classification example. It specifies parameters such as time steps, device, batch size, epochs, data directory, and optimization settings. Use this command to start the training process. ```shell python -m spikingjelly.activation_based.examples.classify_dvsg -T 16 -device cuda:0 -b 16 -epochs 64 -data-dir /datasets/DVSGesture/ -amp -cupy -opt adam -lr 0.001 -j 8 ``` -------------------------------- ### Training Commands for Different Network Models Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/tutorials/cn/recurrent_connection_and_stateful_synapse.md Provides example shell commands to train the 'plain', 'fb' (FeedBackNet), and 'ss' (StatefulSynapseNet) models on Sequential FashionMNIST. These commands specify training parameters like device, batch size, epochs, and data directory. ```shell python -m spikingjelly.activation_based.examples.rsnn_sequential_fmnist -device cuda:0 -b 256 -epochs 64 -data-dir /datasets/FashionMNIST/ -amp -cupy -opt sgd -lr 0.1 -j 8 -model plain python -m spikingjelly.activation_based.examples.rsnn_sequential_fmnist -device cuda:0 -b 256 -epochs 64 -data-dir /datasets/FashionMNIST/ -amp -cupy -opt sgd -lr 0.1 -j 8 -model fb python -m spikingjelly.activation_based.examples.rsnn_sequential_fmnist -device cuda:0 -b 256 -epochs 64 -data-dir /datasets/FashionMNIST/ -amp -cupy -opt sgd -lr 0.1 -j 8 -model ss ``` -------------------------------- ### enable Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/APIs/spikingjelly.activation_based.learning.md Enables the input and output monitors for the learner. ```APIDOC ## enable ### Description Enable the input/output monitors. ### Method `enable()` ``` -------------------------------- ### Install CuPy for CUDA 11.x Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/index.md Install CuPy for CUDA 11.x to enable the CuPy backend for SpikingJelly. Ensure you have a compatible CUDA toolkit installed. ```bash pip install cupy-cuda11x ``` -------------------------------- ### GradOutputMonitor Example Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/APIs/spikingjelly.activation_based.monitor.md Demonstrates how to use GradOutputMonitor to track gradients of IFNode outputs in a network. It shows how to initialize the monitor, trigger gradient calculation, and access recorded data. ```python import torch import torch.nn as nn from spikingjelly.activation_based import monitor, neuron, functional, layer class Net(nn.Module): def __init__(self): super().__init__() self.fc1 = layer.Linear(8, 4) self.sn1 = neuron.IFNode() self.fc2 = layer.Linear(4, 2) self.sn2 = neuron.IFNode() functional.set_step_mode(self, "m") def forward(self, x_seq: torch.Tensor): x_seq = self.fc1(x_seq) x_seq = self.sn1(x_seq) x_seq = self.fc2(x_seq) x_seq = self.sn2(x_seq) return x_seq net = Net() for param in net.parameters(): param.data.abs_() mtor = monitor.GradOutputMonitor(net, instance=neuron.IFNode) net(torch.rand([1, 8])).sum().backward() print(f"mtor.records={mtor.records}") # mtor.records=[tensor([[1., 1.]]), tensor([[0.1372, 0.1081, 0.0880, 0.1089]])] print(f"mtor[0]={mtor[0]}") # mtor[0]=tensor([[1., 1.]]) print(f"mtor.monitored_layers={mtor.monitored_layers}") # mtor.monitored_layers=['sn1', 'sn2'] print(f"mtor['sn1']={mtor['sn1']}") # mtor['sn1']=[tensor([[0.1372, 0.1081, 0.0880, 0.1089]])] ``` -------------------------------- ### Install CuPy for CUDA 12.x Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/index.md Install CuPy for CUDA 12.x to enable the CuPy backend for SpikingJelly. Ensure you have a compatible CUDA toolkit installed. ```bash pip install cupy-cuda12x ``` -------------------------------- ### Install SpikingJelly Development Version from OpenI Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/index.md Install the latest development version of SpikingJelly by cloning the OpenI repository and installing from source. This is an alternative method for obtaining the development version. ```bash git clone https://git.openi.org.cn/OpenI/spikingjelly.git cd spikingjelly pip install . ``` -------------------------------- ### Monitor Input Gradients with GradInputMonitor Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/APIs/spikingjelly.activation_based.monitor.md This example demonstrates how to use GradInputMonitor to track the gradients of inputs for IFNode instances within a network. It shows how to initialize the monitor, perform a forward pass, and access the recorded gradient data. ```python class Net(nn.Module): def __init__(self): super().__init__() self.fc1 = layer.Linear(8, 4) self.sn1 = neuron.IFNode() self.fc2 = layer.Linear(4, 2) self.sn2 = neuron.IFNode() functional.set_step_mode(self, "m") def forward(self, x_seq: torch.Tensor): x_seq = self.fc1(x_seq) x_seq = self.sn1(x_seq) x_seq = self.fc2(x_seq) x_seq = self.sn2(x_seq) return x_seq net = Net() for param in net.parameters(): param.data.abs_() mtor = monitor.GradInputMonitor(net, instance=neuron.IFNode) with torch.no_grad(): y = net(torch.rand([1, 8])) print(f"mtor.records={mtor.records}") # mtor.records=[tensor([0.0000, 0.6854, 0.0000, 0.7968]), tensor([0.4472, 0.0000])] print(f"mtor[0]={mtor[0]}") # mtor[0]=tensor([0.0000, 0.6854, 0.0000, 0.7968]) print(f"mtor.monitored_layers={mtor.monitored_layers}") # mtor.monitored_layers=['sn1', 'sn2'] print(f"mtor['sn1']={mtor['sn1']}") # mtor['sn1']=[tensor([0.0000, 0.6854, 0.0000, 0.7968])] ``` -------------------------------- ### TDGELU Initialization and Usage Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/APIs/spikingjelly.activation_based.ann2snn.md Demonstrates how to initialize and use the TDGELU operator with a given approximation mode and step mode. The example shows a typical forward pass with a sequence input. ```APIDOC ## TDGELU Initialization and Usage ### Description Initializes the Temporal-difference (TD) GELU operator. This operator applies GELU after accumulating inputs over time in multi-step mode, or directly in single-step mode. It is fully differentiable and stateless. ### Parameters * **approximate** (*Literal* *[* "none" *,* "tanh" *]*) -- GELU approximation mode, with the same semantics as `approximate` in `torch.nn.GELU`. * **step_mode** (*str*) -- Step mode, "s" or "m". The default is "m". ### Raises **ValueError** -- If `approximate` is not "none" or "tanh". ### Example ```python op = TDGELU(approximate="none") x_seq = torch.randn(4, 2, 3) y_seq = op(x_seq) ``` ``` -------------------------------- ### Install SpikingJelly (Stable) Source: https://github.com/fangwei123456/spikingjelly/blob/master/README.md Installs the latest stable release of SpikingJelly using pip. ```bash pip install spikingjelly ``` -------------------------------- ### Import MultiStepLIFNode (Old Version) Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/tutorials/en/migrate_from_legacy.md Demonstrates how to import and instantiate a multi-step LIF neuron using the `MultiStepLIFNode` class from the old `clock_driven` package. ```python from spikingjelly.clock_driven import neuron lif = neuron.MultiStepLIFNode() ``` -------------------------------- ### Install SpikingJelly in Editable Mode with uv Source: https://github.com/fangwei123456/spikingjelly/blob/master/CONTRIBUTING.md Installs SpikingJelly and its development dependencies in editable mode. This allows immediate reflection of code changes. The `--group dev` flag installs development tools like Sphinx. ```bash uv pip install --editable . --group dev ``` -------------------------------- ### Initialize Network and Set Backend Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/tutorials/en/classify_dvsg.md Initializes the DVSGestureNet with specific parameters and sets the network to multi-step mode. Optionally configures the backend to use cupy for faster computation if specified. ```python import torch import sys import torch.nn.functional as F from torch.cuda import amp from spikingjelly.activation_based import functional, surrogate, neuron from spikingjelly.activation_based.model import parametric_lif_net from spikingjelly.datasets.dvs128_gesture import DVS128Gesture from torch.utils.data import DataLoader from torch.utils.tensorboard import SummaryWriter import time import os import argparse import datetime def main(): # ... net = parametric_lif_net.DVSGestureNet(channels=args.channels, spiking_neuron=neuron.LIFNode, surrogate_function=surrogate.ATan(), detach_reset=True) functional.set_step_mode(net, 'm') if args.cupy: functional.set_backend(net, 'cupy', instance=neuron.LIFNode) # ... ``` -------------------------------- ### Install NIR Exchange Dependencies Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/tutorials/en/nir_exchange.md Install the necessary packages for NIR exchange functionality. This is a prerequisite for using the export and import features. ```shell pip install nir nir_exchange ``` -------------------------------- ### Example Classification Result Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/tutorials/en/lif_fc_mnist.md Example output showing the firing rate for a single test sample, indicating the network's classification. ```shell Firing rate: [[0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]] ``` -------------------------------- ### Step-by-Step and Layer-by-Layer Propagation (New Version) Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/tutorials/en/migrate_from_legacy.md Demonstrates step-by-step and layer-by-layer propagation in the new version using `spikingjelly.activation_based.functional.set_step_mode` to control module behavior. ```python import torch import torch.nn as nn from spikingjelly.activation_based import neuron, layer, functional with torch.no_grad(): T = 4 N = 2 C = 4 H = 8 W = 8 x_seq = torch.rand([T, N, C, H, W]) # the network uses step-by-step because step_mode='s' is the default value for all modules net = nn.Sequential( layer.Conv2d(C, C, kernel_size=3, padding=1, bias=False), layer.BatchNorm2d(C), neuron.IFNode() ) y_seq = functional.multi_step_forward(x_seq, net) # y_seq.shape = [T, N, C, H, W] functional.reset_net(net) # set the network to use layer-by-layer functional.set_step_mode(net, step_mode='m') y_seq = net(x_seq) # y_seq.shape = [T, N, C, H, W] functional.reset_net(net) ``` -------------------------------- ### Install NIR and NIRTorch for nir_exchange Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/index.md Install NIR and NIRTorch to enable the `nir_exchange` functionality in SpikingJelly. These libraries are required for exporting to and importing from the NIR format. ```bash pip install nir nirtorch ``` -------------------------------- ### Initialize and Use SynOpCounter Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/APIs/spikingjelly.activation_based.op_counter.md Demonstrates how to initialize SynOpCounter, use it within a DispatchCounterMode context, and retrieve the total SynOp count after a forward pass. ```python from spikingjelly.activation_based.op_counter import ( SynOpCounter, DispatchCounterMode, ) import torch import torch.nn as nn model = nn.Linear(10, 5, bias=False) spike = (torch.rand(4, 10) < 0.2).float() counter = SynOpCounter() with DispatchCounterMode([counter]): model(spike) print(f"SynOp count: {counter.get_total()}") ``` -------------------------------- ### Install Dependencies for DSQN Source: https://github.com/fangwei123456/spikingjelly/blob/master/spikingjelly/activation_based/examples/DSQN/README.md Install the necessary Python packages for DSQN, including gym, gym[atari], and atari-py. Ensure you are using gym version 0.18.0. ```shell pip install gym==0.18.0 pip install gym[atari] pip install atari-py==0.2.5 ``` -------------------------------- ### InputMonitor Example Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/APIs/spikingjelly.activation_based.monitor.md Demonstrates how to use InputMonitor to record inputs to IFNode instances within a neural network. Shows how to access recorded data by index or layer name. ```python import torch import torch.nn as nn from spikingjelly.activation_based import monitor, neuron, functional, layer class Net(nn.Module): def __init__(self): super().__init__() self.fc1 = layer.Linear(8, 4) self.sn1 = neuron.IFNode() self.fc2 = layer.Linear(4, 2) self.sn2 = neuron.IFNode() functional.set_step_mode(self, "m") def forward(self, x_seq: torch.Tensor): x_seq = self.fc1(x_seq) x_seq = self.sn1(x_seq) x_seq = self.fc2(x_seq) x_seq = self.sn2(x_seq) return x_seq net = Net() for param in net.parameters(): param.data.abs_() mtor = monitor.InputMonitor(net, instance=neuron.IFNode) with torch.no_grad(): y = net(torch.rand([1, 8])) print(f"mtor.records={mtor.records}") # mtor.records=[tensor([[1.0165, 1.1934, 0.9347, 0.9539]]), tensor([[0.9115, 0.9508]])] print(f"mtor[0]={mtor[0]}") # mtor[0]=tensor([[1.0165, 1.1934, 0.9347, 0.9539]]) print(f"mtor.monitored_layers={mtor.monitored_layers}") # mtor.monitored_layers=['sn1', 'sn2'] print(f"mtor['sn1']={mtor['sn1']}") # mtor['sn1']=[tensor([[1.0165, 1.1934, 0.9347, 0.9539]])] ``` -------------------------------- ### Custom Event Integration Function Example Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/APIs/spikingjelly.datasets.shd.md Example of a custom function to integrate event segments into frames for SHD dataset processing. It splits events and integrates them into two frames. ```python import os import h5py import numpy as np from spikingjelly.datasets import utils from spikingjelly.datasets.shd import _integrate_events_segment_to_frame def custom_integrate_function_example(h5_file: h5py.File, i: int, output_dir: str, W: int): events = {'t': h5_file['spikes']['times'][i], 'x': h5_file['spikes']['units'][i]} label = h5_file['labels'][i] frames = np.zeros([2, W]) index_split = np.random.randint(low=0, high=len(events['t'])) frames[0] = _integrate_events_segment_to_frame(events['x'], W, 0, index_split) frames[1] = _integrate_events_segment_to_frame(events['x'], W, index_split, len(events['t'])) fname = os.path.join(output_dir, str(label), str(i)) utils.np_savez(fname, frames=frames) ``` -------------------------------- ### OutputMonitor Example with IFNode Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/APIs/spikingjelly.activation_based.monitor.md Demonstrates how to use OutputMonitor to record the outputs of IFNode instances within a neural network. It shows how to initialize the monitor, run the network, and access the recorded data. ```python class Net(nn.Module): def __init__(self): super().__init__() self.fc1 = layer.Linear(8, 4) self.sn1 = neuron.IFNode() self.fc2 = layer.Linear(4, 2) self.sn2 = neuron.IFNode() functional.set_step_mode(self, "m") def forward(self, x_seq: torch.Tensor): x_seq = self.fc1(x_seq) x_seq = self.sn1(x_seq) x_seq = self.fc2(x_seq) x_seq = self.sn2(x_seq) return x_seq net = Net() for param in net.parameters(): param.data.abs_() mtor = monitor.OutputMonitor(net, instance=neuron.IFNode) with torch.no_grad(): y = net(torch.rand([1, 8])) print(f"mtor.records={mtor.records}") # mtor.records=[tensor([[0., 0., 0., 1.]]), tensor([[0., 0.]])] print(f"mtor[0]={mtor[0]}") # mtor[0]=tensor([[0., 0., 0., 1.]]) print(f"mtor.monitored_layers={mtor.monitored_layers}") # mtor.monitored_layers=['sn1', 'sn2'] print(f"mtor['sn1']={mtor['sn1']}") # mtor['sn1']=[tensor([[0., 0., 0., 1.]])] ``` -------------------------------- ### Example CNN Model Structure Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/tutorials/en/ann2snn.md This is an example of a CNN model structure after BatchNorm2d modules have been fused into preceding Conv layers. It shows the sequence of Conv2d, AvgPool2d, Flatten, and Linear layers, along with spiking modules. ```python CNN( (network): Module( (0): Conv2d(1, 32, kernel_size=(3, 3), stride=(1, 1)) (3): AvgPool2d(kernel_size=2, stride=2, padding=0) (4): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1)) (7): AvgPool2d(kernel_size=2, stride=2, padding=0) (8): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1)) (11): AvgPool2d(kernel_size=2, stride=2, padding=0) (12): Flatten(start_dim=1, end_dim=-1) (13): Linear(in_features=32, out_features=10, bias=True) (spiking_0): Module( (scaler0): VoltageScaler(0.193247) (if_node): IFNode( v_threshold=1.0, v_reset=None, detach_reset=False, step_mode=s, backend=torch (surrogate_function): Sigmoid(alpha=4.0, spiking=True) ) (scaler1): VoltageScaler(5.174733) ) (spiking_1): Module( (scaler0): VoltageScaler(0.325697) (if_node): IFNode( v_threshold=1.0, v_reset=None, detach_reset=False, step_mode=s, backend=torch (surrogate_function): Sigmoid(alpha=4.0, spiking=True) ) (scaler1): VoltageScaler(3.070336) ) (spiking_2): Module( (scaler0): VoltageScaler(0.121967) (if_node): IFNode( v_threshold=1.0, v_reset=None, detach_reset=False, step_mode=s, backend=torch (surrogate_function): Sigmoid(alpha=4.0, spiking=True) ) (scaler1): VoltageScaler(8.198915) ) ) ) ``` -------------------------------- ### Command-line Interface Help Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/tutorials/en/lif_fc_mnist.md Displays the usage and available arguments for running the MNIST training script from the command line. This includes parameters for simulation time, device, batch size, epochs, data directory, and training options like mixed precision and optimizer. ```shell $ python -m spikingjelly.activation_based.examples.lif_fc_mnist --help ``` -------------------------------- ### spikingjelly.activation_based.cuda_kernel.auto_cuda.base.startswiths Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/APIs/spikingjelly.activation_based.cuda_kernel.auto_cuda.md Checks if a string starts with any of the provided prefixes. ```APIDOC ## spikingjelly.activation_based.cuda_kernel.auto_cuda.base.startswiths(x, prefixes) ### Description Checks if a string starts with any of the provided prefixes. ### Parameters #### Path Parameters - **x** (str) - Description not available - **prefixes** (tuple) - Description not available ``` -------------------------------- ### Instantiate Multi-Step LIFNode (New Version) Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/tutorials/en/migrate_from_legacy.md Shows how to use the LIFNode in multi-step mode in the new version by setting the `step_mode` attribute to 'm'. ```python from spikingjelly.activation_based import neuron lif = neuron.LIFNode(step_mode='m') ``` -------------------------------- ### Recommended Dataset and Utility Import Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/APIs/spikingjelly.datasets.md Demonstrates the recommended way to import dataset classes and utility functions from the spikingjelly.datasets package. ```python # Recommended ✅ from spikingjelly.datasets import DVS128Gesture from spikingjelly.datasets.utils import create_sub_dataset ``` -------------------------------- ### dynamo_hop_available Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/APIs/spikingjelly.activation_based.triton_kernel.flexsn.md Reports whether the FlexSN Dynamo HigherOrderOperator registration has been installed successfully. ```APIDOC ## dynamo_hop_available() ### Description Report whether the FlexSN Dynamo HOP registration succeeded. Returns `True` when the Dynamo compatibility shim for `flex_sn_scan` is registered; otherwise `False`. ### Returns `True` when the Dynamo compatibility shim for `flex_sn_scan` is registered; otherwise `False`. ### Return Type *bool* ``` -------------------------------- ### Rescale Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/APIs/spikingjelly.activation_based.examples.md A utility class for rescaling data, used in the speech commands example. ```APIDOC ## class spikingjelly.activation_based.examples.speechcommands.Rescale ### Description Utility class for rescaling data. ``` -------------------------------- ### Step-by-Step and Layer-by-Layer Propagation (Old Version) Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/tutorials/en/migrate_from_legacy.md Illustrates step-by-step and layer-by-layer propagation patterns using modules from `spikingjelly.clock_driven` in the old version. ```python import torch import torch.nn as nn from spikingjelly.clock_driven import neuron, layer, functional with torch.no_grad(): T = 4 N = 2 C = 4 H = 8 W = 8 x_seq = torch.rand([T, N, C, H, W]) # step-by-step net_sbs = nn.Sequential( nn.Conv2d(C, C, kernel_size=3, padding=1, bias=False), nn.BatchNorm2d(C), neuron.IFNode() ) y_seq = functional.multi_step_forward(x_seq, net_sbs) # y_seq.shape = [T, N, C, H, W] functional.reset_net(net_sbs) # layer-by-layer net_lbl = nn.Sequential( layer.SeqToANNContainer( nn.Conv2d(C, C, kernel_size=3, padding=1, bias=False), nn.BatchNorm2d(C), ), neuron.MultiStepIFNode() ) y_seq = net_lbl(x_seq) # y_seq.shape = [T, N, C, H, W] functional.reset_net(net_lbl) ``` -------------------------------- ### Pad Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/APIs/spikingjelly.activation_based.examples.md A utility class for padding data, used in the speech commands example. ```APIDOC ## class spikingjelly.activation_based.examples.speechcommands.Pad(size) ### Description Utility class for padding data. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * **size** (int) - The padding size. ``` -------------------------------- ### Create Log Directory Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/_static/tutorials/11_cext_neuron_with_lbl/log.txt Creates a directory for logging training results. The directory name is constructed based on various training parameters. ```shell Mkdir ./logs/T_4_b_128_SGD_lr_0.1_CosALR_64_amp_cupy. ``` -------------------------------- ### create_fb_matrix Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/APIs/spikingjelly.activation_based.examples.md Creates the filter bank matrix used for MelScale transformation in the speech commands example. ```APIDOC ## spikingjelly.activation_based.examples.speechcommands.create_fb_matrix(n_freqs, f_min, f_max, n_mels, sample_rate, dct_type='slaney') ### Description Creates the filter bank matrix for MelScale transformation. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * **n_freqs** (int) - The number of frequency bins. * **f_min** (float) - The minimum frequency. * **f_max** (float) - The maximum frequency. * **n_mels** (int) - The number of Mel bands. * **sample_rate** (int) - The sample rate of the audio. * **dct_type** (str | None) - The DCT type to use. Defaults to 'slaney'. ### Returns * Tensor: The filter bank matrix. ``` -------------------------------- ### hz_to_mel Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/APIs/spikingjelly.activation_based.examples.md Converts Hz frequencies to Mel frequencies. This function is part of the speech commands example. ```APIDOC ## spikingjelly.activation_based.examples.speechcommands.hz_to_mel(frequencies, dct_type) ### Description Converts Hz frequencies to Mel frequencies. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * **frequencies** (Tensor) - The Hz frequencies to convert. * **dct_type** (str) - The DCT type to use for conversion. ### Returns * Tensor: The converted Mel frequencies. ``` -------------------------------- ### Training Log Output (Epoch 0) Source: https://github.com/fangwei123456/spikingjelly/blob/master/docs/source/tutorials/en/conv_fashion_mnist.md This snippet shows the initial training log for epoch 0, including training and testing loss and accuracy, as well as the training and testing speed in images per second. The output directory and escape time are also recorded. ```shell Mkdir ./logs/T4_b256_sgd_lr0.1_c128_amp_cupy. Namespace(T=4, device='cuda:0', b=256, epochs=64, j=8, data_dir='/datasets/FashionMNIST/', out_dir='./logs', resume=None, amp=True, cupy=True, opt='sgd', momentum=0.9, lr=0.1, channels=128) ./logs/T4_b256_sgd_lr0.1_c128_amp_cupy epoch =0, train_loss = 0.0325, train_acc = 0.7875, test_loss = 0.0248, test_acc = 0.8543, max_test_acc = 0.8543 train speed = 7109.7899 images/s, test speed = 7936.2602 images/s escape time = 2022-05-24 21:42:15 ```