Generating Artificial Spike Trains for Spiking Neural Networks

A spiking neural network (SNN) is one where the artificial neurons more closely model biological neurons than the neurons in a traditional artificial neural network (ANN). An SNN accepts a stream of 0s and 1s called a spike train, as opposed to ANNs which accept floating-point values such as 0.3827.

So one of the keys to understanding SNNs is to understand spike trains. When I was learning about spike trains, one of the most useful investigations was writing a tiny demo program (in Python) to generate artificial spike trains. Without further ado, here’s the code:

# make_spikes.py
import random

print("\nBegin spike generation \n")

T = 0.050  # total time in sec
dt = 0.005  # time interval in sec
fr = 80  # firing rate in Hz

random.seed(9)

bins = int(T / dt)  # 10 bins
for i in range(bins):
  p = random.random()
  if p [less-than] fr * dt:
    print("  1", end="")  # spike
  else:
    print("  0", end="")

print("\n\nDone")

Running the program generated a spike train of:

  0  1  1  0  1  0  0  1  0  0

The dt is a time interval, arbitrarily set to 5 milliseconds = 0.005 seconds. I wanted 10 time intervals/bins so I set total time = 10 * 0.005 = 0.050 seconds = 50 milliseconds. I set the firing rate (fr) to 80 Hz = 80 spikes per second. Therefore, for T = 50 milliseconds I expect to get on average 50 ms * 80 Hz = 4 spikes. (Dealing with the seconds to milliseconds conversions is a bit tricky).

For each of the 10 time intervals the program generates a random value, or probability, between [0.0, 1.0). If that value is less than the product of fr and dt, then a spike, 1, is emitted, otherwise a 0 is emitted.

When a random sequence is generated in this manner, the resulting values are said to follow a Poisson distribution. If the demo is run multiple times with different seed values, the average number of spikes in the generated sequences will be T * fr = 50 ms * 80 Hz = 4 but because the process is random sequences can have anywhere from zero through 10 spikes.

If you’re new to spiking neural networks, hopefully this blog post helped you understand spike trains.



I love trains. When I was young I used to ride the train with my brother and sister from Fullerton, CA to San Diego. And I loved setting up HO scale model trains — similar in some ways to computer networks. Here are three posters for the old Santa Fe Railway (1859 – 1996).

This entry was posted in Machine Learning. Bookmark the permalink.