Chapter 14 · Machines
Case study: the racer
A spiking network drives a car around tracks it has never seen, with nothing but an event camera's output and its own speed to go on. It learned end to end, by gradients through its own spikes, through the car, and through the camera's pixels, whose thresholds are spikes too. This chapter takes it apart, shows how often such training failed, and what made it reliable.
The task
A car carries an event camera that looks at the road ahead. The road is dark, its verges are light, and a dashed line runs down its middle. The camera sends nothing about the road itself, only where its brightness changes as the car moves, and a car that stands still sees nothing at all. From those events and its own speed, a network of spiking neurons must steer and set the speed, around a track it has never seen.
Nobody shows it how. Event-camera steering has been learned by imitation: Maqueda et al. trained a conventional network to predict a human driver’s steering from a large recorded dataset. The racer has no driver to imitate, and learns from what happens when it drives.
The car and its camera
The car is a kinematic bicycle: a wheelbase of 0.4 m, steering of at most 0.6 rad, a top speed of 6 m/s. Every 20 ms the network’s two readout membranes and set the steering and a target speed, and the car moves:
The camera has 64 × 32 pixels, each the average of four points of the ground: rows from 0.6 m to 7 m ahead, spaced as a real camera’s rows fall on a flat road, each spanning 0.9 times its distance to either side. Each pixel is chapter 12’s: it sends an ON or OFF event when the logarithm of the brightness it sees has moved 0.15 from the level of its last event. The network reads 4,097 numbers each step, the 2,048 pixels’ ON events, their 2,048 OFF events and the car’s speed. Two convolutions of stride 2 look at the camera’s image, 16 filters of 5 × 5 pixels and then 32 of 3 × 3, each into a population of LIF neurons, 8,192 and 4,096 of them; a dense layer takes their spikes and the speed into 128 more LIF neurons, and those drive the two readout integrators. That is 12,416 spiking neurons and 530,258 weights.
events 0 · spikes 0 a step · …
Draw a track with the mouse, a loop around the car, or press “Draw a track” and draw it with a finger. Below the road are the camera’s pixels as they fire, the far rows at the top, orange for ON and blue for OFF; then each convolution’s neurons as a map, a position lit when any of its filters’ neurons fired; then the 128 neurons of the dense layer, and the steering and speed the readouts set. The track you draw is smoothed and scaled to the size of the tracks the network trained on, which have radii of 6 to 10 m. Draw corners much tighter than those and the car may leave the road; the figure then puts it back at the start.
Gradients through a camera
The car, the camera and the network are written in JAX, so a drive is one differentiable function of the network’s weights. Most of it is smooth: the car’s motion, the brightness of the ground, its logarithm. Two kinds of step are not, the neurons’ spikes and the pixels’ events, and both are sparx.spike, with chapter 5’s surrogate for a derivative. The pixels use a narrower one than the neurons, since their threshold is 0.15 rather than 1.
So the gradient of the loss reaches the weights by every path there is: through the readouts and the spikes, into the events, through each pixel’s threshold into the brightness it saw, through the ground into the car’s position and heading, and back through every earlier step of the drive. Here is one pixel row’s piece of that chain:
import jaximport jax.numpy as jnp
import sparxfrom sparx.surrogate import ATan
threshold, event = 0.15, ATan(alpha=8.0) # the racer's pixels
def brightness(x): """A road's edge: dark road, then a light verge past 0.8 m.""" return 0.12 + 0.7 * jax.nn.sigmoid((x - 0.8) / 0.12)
def on_events(shift): """ON events from 9 pixels across the edge when the car moves `shift` metres toward the verge.""" pixels = jnp.linspace(0.0, 1.6, 9) # metres from the middle before = jnp.log(brightness(pixels)) after = jnp.log(brightness(pixels + shift)) return sparx.spike(after - before - threshold, event).sum()
for shift in (0.05, 0.1, 0.2, 0.4): count, slope = jax.value_and_grad(on_events)(shift) print(f"move {shift:.2f} m: {int(count)} ON events," f" gradient {float(slope):.1f} per m")move 0.05 m: 1 ON events, gradient 29.3 per mmove 0.10 m: 3 ON events, gradient 15.2 per mmove 0.20 m: 3 ON events, gradient 8.0 per mmove 0.40 m: 5 ON events, gradient 5.6 per mThe count of events is a staircase in the car’s position, flat between its steps, so its true derivative is zero almost everywhere. The surrogate’s is never zero: between 0.1 and 0.2 m the count stays at 3, and the gradient still says that moving further brings more events.
The loss
Each training step draws 32 tracks like those you see, puts a car on each at a random point, off the middle, askew and moving, and drives them all for 300 steps, 6 s. With the distance from the middle of the road, m its half width, the speed along the road and the share of steps on which neuron fired:
The first two terms keep the car on the road, the second hard once it is off. The third rewards progress, the fourth punishes driving the wrong way round, and the last keeps every neuron in use and none saturated, as in the drone. Adam follows the gradient, clipped to a norm of 1, with a learning rate that warms up and then decays.
Three more choices, with the longer drive and the fourth term, made this training reliable; the next section shows what they did together. The gradient reaches back at most 50 steps, 1 s: every 50 steps the drive’s state is cut from its past, as chapter 6’s streaming layers cut it between chunks. The tracks start gentle, at a bend strength of 0.3, and bend harder over the first half of training, to 1.3, sharper than the 0.9 of the tracks it is tested on first. And every 250 steps the network drives 32 tracks of its own for 20 s, never the tracks it is tested on, and the run keeps the weights that drove farthest.
How often it failed, and what fixed it
The first version of this training drove 150 steps, 3 s, with the gradient carried back through all of them, on tracks of one difficulty, and kept its last weights. Gradients carried back through 150 steps of a car, a camera and a network explode, as chapter 6 warned, and it did not survive them often. Each row is 3,000 training steps at a learning rate of 0.001 and a batch of 32, five seeds of each recipe, scored on 200 tracks it never trained on:
| Recipe | Seeds that finished every track | Unseen tracks finished, seeds 1 to 5 |
|---|---|---|
| The first: 150 steps, no cut, last weights | 1 of 5 | 200, 2, 3, 94 and 25 of 200 |
| 300 steps cut every 50, bends grown, wrong way punished | 3 of 5 | 200, 200, 134, 163 and 200 of 200 |
| The same, keeping the weights that drove farthest on held-out tracks | 5 of 5 | 200 of 200 each |
Under the first recipe the median gradient norm before clipping was 97 for seed 1 and between 1,600 and 6.8 million for the other four, and the largest reached . Clipping holds the step’s size, but its direction comes from whichever path exploded: seed 2 learned to drive backwards. Under the second recipe, which cuts the gradient every 50 steps, the median norm was between 0.1 and 38. A cut bounds the chain of factors a gradient is multiplied through, but the second recipe changed four things at once, and these runs do not say how much each did. Two of those five seeds then drove well halfway through training and worse by its end, once the tracks reached their hardest bends; choosing the weights on tracks of their own recovered both. Keeping the weights that score best on held-out data is ordinary practice, and here the held-out tracks are never the ones the network is tested on. These runs trained the small dense network of the chapter’s first version, on a coarser camera. The car on this page and the front page is the convolutional network above, trained with the last recipe: of five seeds, the one that drove farthest on its held-out tracks. Do the spikes matter? compares it with networks that do not spike.
How well it drives
Each of 200 tracks it never trained on, drawn by the same rule with another seed, starts the car at rest in the middle of the road, pointing along it, and gives it 30 s. A lap counts when the car has gone the track’s length without straying more than a metre beyond the road’s edge.
It finished all 200, in a median of 9.8 s for tracks of 41 to 80 m, at a median of 5.9 m/s, and its centre never left the road. Each step the camera sent 596 events, of 2,048 possible, since a pixel fires at most once a step, and 1,038 of the 12,416 neurons fired. Moved 0.3 m sideways 2 s into a drive, it steered back at once: its steering’s difference from the same drive unmoved reached half its largest in a median of 20 ms, one step.
Tracks with sharper bends test it further. At bend strengths 0.9, 1.2, 1.5 and 1.8, 200 tracks each, the last two harder than training’s hardest, it finished 100%, 100%, 100% and 99%. Its two failures of the 800 were both on bends tighter than a metre in radius.
No track failed and the car never left the road, so here is its slowest lap:
Track 141 of the 200, 79.7 m around, with the car’s path over 30 s in orange from the dot. It finished its first lap in 13.9 s and spent 0.0% of its 30 s off the road.
The browser drives the same network from the same weights, in double precision, with the same operations in the same order. Its test holds two drives of 10 s, on two unseen tracks, to sparx’s own float64 run. Every event and every spike must happen at the same step, and the readouts must agree to of their size. The car cannot agree exactly, since the two compute tanh, exp and the other functions with different libraries. So a script redoes each step of the car exactly and checks that each implementation rounded it no more than float64 allows. Those allowances, carried through the car’s motion, bound how far apart the two cars may drift. Over the two drives the bound grows to 21,650 units in the last place of the largest coordinate, about m, and the distance measured is at most 12.2, about m.
Do the spikes matter?
The racer above is network A of a comparison. To ask whether spiking earns its place, the same recipe trained the same convolutional network on the same camera five seeds at a time, changing only its units. A spikes as LIF neurons do. B uses the same leaky membrane read through a ReLU, sending a real value every step, on the same events. C is B on an ordinary camera’s frames, the log brightness of every pixel every step.
| Network | Seeds that finished all 200 unseen tracks | Lap | Units sending a step | Triggered multiply-adds a step |
|---|---|---|---|---|
| A, spiking, events | 5 of 5 | 9.8 s | 1,161 | 151,920 |
| B, graded, events | 4 of 5 | 10.8 s | 6,608 | 629,750 |
| C, graded, frames | 4 of 5 | 12.4 s | 5,545 | 652,209 |
In these five runs each, the three finished nearly as many tracks. A finished every track at every seed, B missed one track at one seed, and C, the weakest, finished 186 at one seed with a slow lap. A was also the most consistent: its median laps varied by 0.2 s from seed to seed, B’s by 1.6 s and C’s by 5.8 s.
The larger difference is in the work. A and B have the same 1.46 million connections, and C 1.27 million, since a frame has one channel where events have two. A GPU computes every connection every step, and a training step took about the same time for all three on the RTX 4080, 0.37 to 0.38 s. Hardware that computes only what a nonzero input or unit triggers, as neuromorphic chips do, would do about a quarter of B’s work for A: 9% of A’s units send in a step against 53% of B’s, and each sends one bit against B’s 32. That is a count of operations, not a measurement on such a chip.
The spiking network also steered back from a 0.3 m sideways nudge within one step, 20 ms, at every seed, where the graded ones took 170 and 216 ms on average to reach half their response. These runs do not say why. One candidate is the units: a graded unit passes on its membrane, which follows a change in its input over its time constant, while a LIF neuron fires as soon as its membrane crosses threshold.
Five more variants trained the same way. Spikes carrying 2 or 4 bits, as Loihi 2’s can, did worse: at one seed the 2-bit network finished 85 tracks and the 4-bit none. Sigma-delta units, which send a real value only when it has changed enough, matched A’s reliability and work but responded in about 110 ms. Neurons with four active dendrites matched or beat A at every measure of driving. Neurons whose thresholds rise each step they fire and fall each step they do not, holding them toward 3% of steps, drove as A did with 6.7% of their units sending a step against A’s 9.3%, when a threshold moved by 0.01 a spike; at 0.1 a spike no seed learned to drive every track. The study’s notes give every seed and measure.
Try this
- Every drive starts with the car at rest. What does the network see at the first step, and how does the car get moving?
- Draw a track with a hairpin much tighter than any in training. Why might the car fail there, when it drives the 200 unseen tracks without a fault?
- Why does the loss reward speed along the road, rather than speed itself?
- The last recipe keeps the weights that drove farthest on 32 tracks of their own. Why not choose them on the 200 tracks it is scored on?
Answers
- Nothing: a still camera sends no events, and the speed it reads is 0. But the target speed is , which is never zero, so the car starts to roll, and as it moves the dashes and the road’s edges start to send events.
- The 200 unseen tracks were drawn by the same rule as the training tracks, so their bends are no sharper than those it learned on. A hairpin asks for slowing and steering it never needed in training, and nothing in the loss taught it what to do there.
- Speed itself can be had by driving in circles, or straight off the track. Speed along the road counts only progress round the track.
- Weights chosen on the 200 tracks would be those that happened to suit those tracks, so their score would overstate how the network drives tracks it has never seen. Tracks of their own give the choice its own data, and the 200 stay unseen until the end, as a test set must.
Summary
A spiking network learned to drive from an event camera’s output and its own speed, by gradients through its spikes, the car’s motion and the pixels’ own thresholds, and finished every one of 200 tracks it had never seen. Its first recipe worked for one seed in five. Cutting the gradient every 50 steps, growing the bends over training and keeping the weights that drove farthest on held-out tracks made it work for all five. Against networks of graded units trained the same way, in five runs each, the spiking one finished as many tracks and more consistently, would need about a quarter of the work on event-driven hardware and none less on a GPU, and steered back from a nudge faster.
References
- A. I. Maqueda, A. Loquercio, G. Gallego, N. García and D. Scaramuzza, “Event-based vision meets deep learning on steering prediction for self-driving cars”, Conference on Computer Vision and Pattern Recognition, 2018, arXiv:1804.01310.
- G. Gallego et al., “Event-based vision: a survey”, IEEE Transactions on Pattern Analysis and Machine Intelligence 44, 2022, doi:10.1109/TPAMI.2020.3008413.
- E. O. Neftci, H. Mostafa and F. Zenke, “Surrogate gradient learning in spiking neural networks”, IEEE Signal Processing Magazine 36, 2019, doi:10.1109/MSP.2019.2931595.
- R. Pascanu, T. Mikolov and Y. Bengio, “On the difficulty of training recurrent neural networks”, International Conference on Machine Learning, 2013, arXiv:1211.5063.