Skip to main content

spikecraft

Write a spiking neuron as equations, get a trainable PyTorch layer. Or pick one from the zoo — every zoo model is itself just an equation preset you can open and edit.

pip install spikecraft        # after publishing; for now: pip install -e .

1. Your own neuron from equations

import spikecraft as sf

neuron = sf.Neuron(
    equations="""
        dv/dt = (-(v - v_rest) + R*I) / tau
        da/dt = -a / tau_a
    """,
    threshold="v > v_th + beta*a",
    reset="v = v_reset; a += 1",
    params=dict(tau=10.0, tau_a=100.0, beta=0.2, R=1.0, v_rest=0.0, v_reset=0.0, v_th=1.0),
    learnable=["tau", "beta"], per_neuron=True,   # trained by backprop, one value per neuron
    surrogate="atan",                             # fast_sigmoid | sigmoid | atan | triangle | gaussian
    solver="exp_euler",                           # exact exponential step where the ODE allows
)
layer = sf.SpikingLinear(neuron, in_features=128, out_features=64)
spikes = layer(x)          # x: [T, B, 128] -> [T, B, 64]
  • I is the input current (rename with input="..."). dt and time constants share one unit (ms by convention).
  • Functions: exp, log, sqrt, tanh, sin, cos, abs, max, min, sigmoid, heaviside.
  • Reset supports =, +=, -=, *=. Leave reset=None and use threshold_mode="rising" for models without reset (e.g. Hodgkin–Huxley).
  • Mistakes are caught early: unknown symbols, malformed equations, resets on missing states.

2. Model zoo

from spikecraft import zoo
zoo.list_models()
# ['ALIF', 'AdEx', 'EIF', 'HodgkinHuxley', 'Izhikevich', 'LIF', 'QIF',
#  'ResonateAndFire', 'SynapticLIF', 'TwoCompartment']

lif  = zoo.LIF(tau=20.0, learnable=["tau"])
izh  = zoo.Izhikevich(firing="chattering")   # regular_spiking, intrinsically_bursting, fast_spiking, ...
hh   = zoo.HodgkinHuxley()
print(zoo.AdEx())                             # shows its equations and params

Edit a preset's equations directly:

spec = zoo.spec("LIF")
spec["equations"] = "dv/dt = (-(v - v_rest) + R*I + bias) / tau"
spec["params"]["bias"] = 0.1
my_lif = sf.Neuron(**spec)
Model States Notes
LIF v leaky integrate-and-fire
SynapticLIF i, v current-based synapse filter
ALIF v, a adaptive threshold
QIF / EIF v quadratic / exponential IF
Izhikevich v, u 6 named firing types
AdEx v, w Brette–Gerstner, pA / mV
HodgkinHuxley v, m, h, n gates integrated exactly (Rush–Larsen)
ResonateAndFire x, y damped oscillator
TwoCompartment vd, vs dendrite input, somatic spike

3. Layers and utilities

  • SpikingLinear, SpikingConv2d, and Spiking(any_module, neuron) for any synapse. recurrent=True adds recurrent weights.
  • Neurons are plain nn.Modules: neuron.step(I, state) for custom loops, neuron(I_seq, return_states=True) for traces.
  • sf.encoding.rate, latency, repeat, spike_count.
  • torch.save / state_dict work as usual.

How it works

Equations are parsed with SymPy and compiled to PyTorch. For each state x the solver computes A = ∂f/∂x; if A doesn't depend on x, the step is the exact exponential update x ← x·e^{AΔt} + (e^{AΔt}−1)/A · (f − Ax), otherwise forward Euler. Spikes use a Heaviside forward pass with a surrogate gradient backward pass.

Development

pip install -e ".[dev]"
pytest
python examples/train_custom_vs_zoo.py
python -m build && twine upload dist/*

License

MIT

Metadata

Release files for spikecraft 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for spikecraft 0.1.0
File Size Uploaded Provenance Actions
spikecraft-0.1.0.tar.gz 14.4 kB Without provenance Details for spikecraft-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for spikecraft 0.1.0
File Interpreter ABI Platform Actions
spikecraft-0.1.0-py3-none-any.whl Python 3 none any Details for spikecraft-0.1.0-py3-none-any.whl

Total release size: 30.5 kB

Release files / spikecraft-0.1.0.tar.gz

Download URL spikecraft-0.1.0.tar.gz
Size 14.4 kB
Tags Source
SHA-256 checksum
How to use checksums
304a0476dba25b36909f3b8bfb63497d1dc5a5365c88ac96c8ee921d3ee0ea4e
BLAKE2b-256 checksum
How to use checksums
f6006a3a37173168b6f58fbc24175214cf9faead1ca4c499f0c2bc667ab04896
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.18

Release files / spikecraft-0.1.0-py3-none-any.whl

Download URL spikecraft-0.1.0-py3-none-any.whl
Size 16.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ace77d60850030b6fa9a7d4bd5d257371a5f831676f9e6454d3b2d0327868780
BLAKE2b-256 checksum
How to use checksums
7bbc85dbb6eece0ca369dac4ba68b3437329d9ce208e6fe1c4bac7c292191f95
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.18

Security for spikecraft 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page