What you will learn
Train or adapt a small SNN and compare it with a matched non-spiking baseline.
Before you begin: Computational neuroscience; From neurons to brains
START WITH A QUESTION
Picture this.
So far we have picked every weight ourselves. Learning begins when a program changes a parameter because a prediction was wrong. Let's make one such change by hand before introducing the extra challenge posed by spikes.
- Loss
- A numerical measure of a specified prediction error.
- Gradient
- The direction and rate of local change of a function with respect to its parameters.
- Learning rate
- The multiplier controlling the size of a parameter update.
Read for the idea first. Work through the numbers next. Try answering the questions before opening the explanations.
Prediction, mistake, correction
Take one input x = 2 and a model y_hat = w*x. The target is y = 1. Start with w = 0.5: prediction is perfect. Instead start at w = 0; the prediction is zero. Define a loss L = (y_hat−y)²/2, a number that measures this mismatch. At w = 0 the loss is 0.5. The hat means 'predicted.' A loss is a design choice: a model learns to reduce the mistake you specify, which may only approximate what you actually care about.
A gradient tells us which way is uphill
For this loss, dL/dw = (w*x−y)*x. The extra x appears because changing w changes the prediction by x times as much: this is a small example of the chain rule. At w = 0, the gradient is −2. Gradient descent moves against that slope: w_next = w−η*dL/dw. With learning rate η = 0.3, w becomes 0.6; prediction becomes 1.2 and loss becomes 0.02. We overshot the perfect weight 0.5 but still improved. A larger learning rate can overshoot badly enough to increase the loss.
Where spiking makes learning harder
A spiking layer keeps a membrane state and emits events when it crosses a threshold. Small parameter changes often leave the discrete spike output unchanged, while a boundary crossing changes it abruptly. The usual derivative is zero away from the threshold and undefined at it. A surrogate-gradient method deliberately substitutes a useful smooth derivative during learning while retaining spikes in the forward calculation. It is an approximation, not the exact derivative of the hard threshold. Local timing-based plasticity rules use a different information pathway.
Connect the matrix to the state
In u_next = βu + Ws, s is a vector of source events and W has target rows, source columns. Each target sums its incoming weighted events, then adds retained state. For passive decay, β = exp(−dt/τ). Threshold, reset and event timing still need rules. This matrix uses the opposite storage orientation from Part 4's A; W = A-transpose for the same directed weights. Draw one asymmetric connection to verify the direction. The executable example below isolates learning in a smooth scalar model before a full SNN.
Learning is more than fitting this example
Training data changes weights. Validation data helps choose settings such as the learning rate. Held-out test data estimates performance after those choices are fixed. Repeatedly adjusting a model after seeing test answers turns the test into more training feedback. A real SNN experiment must also account for input encoding, output decoding and time windows. Fewer spikes do not guarantee lower whole-system energy. First ask whether the network solves the same task at the required quality and deadline.
SEE THE RELATIONSHIPS
From structure to behavior.
MAKE THE IDEA PRECISE
The mathematics.
| Symbol | Meaning |
|---|---|
| u | Membrane-state vector in an illustrative discrete model |
| beta | Retention factor between zero and one |
| W | Weight matrix with target rows and source columns here |
| s | Binary source spike vector |
Build the equation, step by step
- Represent each presynaptic spike in the current bin by a vector entry. Decide whether an entry is binary or a count and keep that convention explicit.
- Multiply W by the spike vector to sum contributions to each target. Here W stores targets in rows, unlike the source-row adjacency used in the connectomics part.
- Add the decayed previous state βu. For an exact passive decay over a bin, β = exp(−dt/τ); a full spiking model still needs explicit threshold, reset and timing conventions.
WORK THROUGH THE NUMBERS
Worked example
For u = [0.2, 0.4], beta = 0.9, s = [1, 0], and W rows [0.5, -0.1], [0.2, 0.3], the new state is [0.68, 0.56]. Notice that the matrix convention differs from the graph example; declare it explicitly.
A small Python experiment.
Python 3 · Standard library only · Synthetic educational example. Run this in your own notebook or a local file.
x, target, weight, learning_rate = 2.0, 1.0, 0.0, 0.3
prediction = weight * x
loss_before = (prediction - target)**2 / 2
gradient = (prediction - target) * x
weight -= learning_rate * gradient
loss_after = (weight * x - target)**2 / 2
print(f"weight: {weight:.2f}; prediction: {weight*x:.2f}")
print(f"loss: {loss_before:.2f} -> {loss_after:.2f}")Read the code as a story
The code saves loss_before, calculates the gradient at the old weight, updates the weight once, then recomputes loss_after. Reusing the old prediction for the final loss would hide whether learning helped. This is a smooth one-weight model, not an SNN trainer. It gives you a reference calculation for understanding what a surrogate-gradient SNN training loop tries to approximate.
Expected output
weight: 0.60; prediction: 1.20 loss: 0.50 -> 0.02
Change one quantity, predict the result, and then run the code again. Explain any difference before adding complexity.
Test your understanding.
Try it: Set both input spikes to zero and trace state decay for five steps.
Self-check: Why can swapping a matrix transpose silently break a neural simulation?
Both orientations may have compatible shapes in a square network, so the code runs while reversing connection direction. Test a tiny asymmetric graph with a single active source and a hand-calculated expected target before scaling up.
Where this model stops: Neither sparsity nor biological resemblance guarantees accuracy, energy savings or general intelligence.
A research question: Does timing preserve useful information that a matched rate baseline loses?
Record evidence in your mastery worksheet →PREDICT / TRY / EXPLAIN
Find a learning rate that fails
Use paper, a calculator or the Python example. Write your prediction before checking the result.
- Work out the η = 0.3 update before running the code.
- Repeat from w = 0 with η = 1. Keep the same input and target. Compare losses.
- Return to the spiking state example u = [0.2, 0.4], β = 0.9, s = [1, 0], W = [[0.5, −0.1], [0.2, 0.3]]. Calculate both new states by hand.
Compare with a worked solution
The first update gives w = 0.6 and loss 0.02. At η = 1, w becomes 2, the prediction becomes 4, and loss becomes 4.5: learning moved too far. The separate network-state calculation gives [0.68, 0.56]. Keep learning updates to weights distinct from time updates to membrane states.
CLOSE YOUR NOTES
Can you explain it back?
These questions test the reasoning, not just a remembered phrase. Say your answer out loud or write a sentence first.
01Is a lower training loss sufficient evidence of better performance on new examples?
Reveal the reasoning for question 1
No. The model may fit training-specific patterns or noise. Evaluate on data that did not guide fitting or tuning, under a split appropriate to the task.
02Does a surrogate derivative remove the hard threshold during prediction?
Reveal the reasoning for question 2
Not in the usual surrogate-gradient setup described here. The forward model remains spiking, while the backward calculation uses an approximate derivative. Other relaxations exist, so state the convention.
TAKE THE IDEA WITH YOU
Three things to remember.
- Learning changes parameters to reduce a chosen loss.
- A membrane update and a weight update solve different problems.
- Hard thresholds complicate gradients; surrogate derivatives are an explicit approximation.
We now have cells, wiring, state and a first learning rule. Next we give the system something concrete to interpret: an object growing in its visual field.
Continue to Part 8 →THE NEXT LAYER
Chapter depth map.
This introduction provides a first model. The following chapters are outlines for deeper future editions; their full treatments have not yet been published.
Representation and inference
7.1 ANNs, CNNs, RNNs, Transformers and SNNs · Outline
- 7.1.1 Activation values versus discrete events
- 7.1.2 Membrane states and temporal recurrence
- 7.1.3 Spatial convolution, recurrent state and attention
- 7.1.4 Latency, memory and operation-count tradeoffs
- 7.1.5 Why spikes do not guarantee energy savings
- 7.1.6 Conventional learning primer: losses, supervised learning, training/validation splits and optimization
7.2 Encoding and decoding · Outline
- 7.2.1 Rate, latency and temporal encodings
- 7.2.2 Population codes and event-based input
- 7.2.3 Spike counts, first-spike and filtered-rate decoding
- 7.2.4 Input scaling, clipping and observation windows
- 7.2.5 Encode-decode tests with known synthetic signals
Learning and large sparse systems
7.3 Learning with discontinuous spikes · Outline
- 7.3.1 Backpropagation through time and state unrolling
- 7.3.2 Surrogate gradients and estimator bias
- 7.3.3 Hebbian rules, STDP and local learning
- 7.3.4 Reward-modulated STDP and eligibility traces
- 7.3.5 Homeostasis and stability during learning
7.4 Implement, profile and compare · Outline
- 7.4.1 Pure Python baseline before framework abstraction
- 7.4.2 PyTorch-oriented Norse and snnTorch options
- 7.4.3 Dense versus sparse updates at different spike rates
- 7.4.4 Scaling from 1 to 1000 cells and beyond
- 7.4.5 Fair accuracy, latency, energy and robustness benchmarks
Read with the original sources.
These readings support the concepts and deeper investigation. The worked numbers and illustrations above are teaching examples.
Research tutorial / author manuscript
Neftci, Mostafa & Zenke (2019): surrogate gradient learning ↗
A deeper reading after the learning primer in Part 7. Compare the spiking forward rule with the approximate derivative used during training; the book’s scalar gradient example is an original smooth-model exercise.
Optional PyTorch-oriented tool
Norse — official documentation ↗
An option to examine for differentiable spiking models. Benchmark the required model and workload before selecting it.
Optional PyTorch-oriented tool
snnTorch — official documentation ↗
An option for spiking learning tutorials and experiments. Dependencies belong in an approved experiment environment, not the website runtime.
Primary paper
Rumelhart, Hinton & Williams (1986): Learning representations by back-propagating errors ↗
Further reading after the weighted-unit and gradient primers in Parts 3 and 7. Study how errors are propagated through intermediate representations. The one-weight example in this book is an original teaching calculation, not a reproduction of the paper.