# Temporally Efficient Deep Learning with Spikes



A couple of weeks ago I got to present Temporally Efficient Deep Learning with Spikes by O’Connor et al, 2018 in a reading group at MILA. I loved the modular way in which it presents its method. It has little boxes like this
that describe the stateful modules that make up the algorithms. Here, I want to look in detail at the mathematical assumptions that have to be made for the method to be valid. While most (but not all) of the math here can also be found scattered throughout the paper, I am trying to present it in a more linear, proof-like manner.

## Why spiking neural networks?

Spiking neural networks are interesting in two ways. 1) The brain uses spikes and we want to understand how it works. 2) Spikes are binary and therefore are cheaper to communicate and store and also have the potential to reduce the costly weight multipication in neural networks to a cheap sum of integer weights.

In the brain, each neuron has on average 7000 connections to other neurons. Therefore it makes sense to trade computation and bandwidth in the connections for computation on the neuron level, i.e. spend computation on encoding the activations.

To recap, a typical, non-spiking neural activation is computed as

where $x$$x$ are the activations of the previous layer (or the network inputs) and $h$$h$ is a non-linear function, e.g. $h\left(z\right)=max\left(z,0\right)$$h(z) = \max(z, 0)$.

Usually floating point numbers are used to represent weights and activations. Floating point numbers are divided into an exponent and a mantissa. To multiply them we have to integer-add the exponent and integer-multiply the mantissa. This is implemented in hardware but it still requires a lot of chip space and energy. However, if $x$$x$ would be binary (i.e. a spike), we can compute $z$$z$ with a sparse sum. We could even use integers to represent $w$$w$ and then the multiplication would be as simple and computationally cheap as it gets.

In their paper O’Connor et al, 2018 introduce an encoding scheme that uses “integer spikes” in the forward pass, backward pass and for the weight updates without loss in accuracy compared to non-spiking networks.

One thing about the paper I found somewhat missleading is that that the authors call their method spiking, when they actually use “integer spikes”. Using integers instead of binary values to communicate activations is still much cheaper than floats but it requires integer multiplications to compute the inner product with the weights. So a more appropriate name would have been “Temporally Efficient Deep Learning with Integer Activations”. Nevertheless the paper is very insightful and it would probably be possible to tweak the method in certain ways to allow it to work with only binary spikes.

Below we see the dataflow from one neuron to another neuron in standard neural network. In the next section we will focus on the axon part, i.e., communicating the activations and trying to find a bandwidth saving encoding.

## Predictive Coding   x→enc→a→dec→x^$\ \ x → \text{enc} → a → \text{dec} → \hat x$

In predictive coding the sender and receiver share a model for the temporal evolution of the signal between them. Instead of communicating the original signal, only the model error is communicated and therefore only the model error is affected by channel noise which results in a higher signal-to-noise ratio.

Predictive coding is usually not used for neuron-to-neuron communication because the channel is not noisy (we usually use float32 to communicate the activations). Since we want to save bandwidth however, we will have to quantize the signal and therefore introduce quantization noise (see next section).

The neuron-to-neuron communication in a standard neural network without predictive coding can be framed as predictive coding with the model ${x}_{t}=0+{a}_{t}$$x_t = 0 \color{grey}{ + a_t}$ with the error ${a}_{t}={x}_{t}$$a_t = x_t$ that has to be communicated. Another very simple model would be to assume the signal stays constant, i.e. ${x}_{t}={x}_{t-1}+{a}_{t}$$x_t = x_{t-1} \color{grey}{ + a_t}$, then we would only transmit activation changes.

O’Connor et. al use a similar decaying model:

${x}_{t}=\frac{{k}_{d}}{{k}_{p}+{k}_{d}}{x}_{t-1}+\frac{1}{{k}_{p}+{k}_{d}}{a}_{t}$

Note that the error is ${a}_{t}$$a_t$ scaled by the factor $\frac{1}{{k}_{p}+{k}_{d}}$$\frac 1{k_p + k_d}$. We can rewrite the model equation as an encoder-decoder pair:

$\begin{array}{rl}\text{enc:}& \phantom{\rule{1em}{0ex}}{a}_{t}={k}_{p}{x}_{t}+{k}_{d}\left({x}_{t}-{x}_{t-1}\right)\\ \\ \text{dec:}& \phantom{\rule{1em}{0ex}}{\stackrel{^}{x}}_{t}={x}_{t}=\frac{{a}_{t}+{k}_{d}{x}_{t-1}}{{k}_{p}+{k}_{d}}\end{array}$

We also can unroll the ${x}_{t-1}$$x_{t-1}$ in this expression (useful for later):

## Sigma-Delta modulation   a→Q→s→Q−1→a^$\ \ a → Q → s → Q^{-1} → \hat a$

Sigma-Delta modulation is a quantization scheme and a form of noise shaping for converting high bit-count, low frequency signals into low bit-count, high frequency signals. Let’s look at how that works:

Because quantization $s=\text{round}\left(a\right)\phantom{\rule{thinmathspace}{0ex}}$$s = \round(a) \,$ looses information, we store the “leftover”, $\varphi =a-s\phantom{\rule{thinmathspace}{0ex}}$$\phi = a - s \,$ and add it at the next timestep $s=\text{round}\left(\varphi +a\right)$$s = \round(\phi + a)$.

So, starting with ${\varphi }_{0}=0$$\phi_0 = 0$, we have

$\begin{array}{rl}& {s}_{t}=\text{round}\left({\varphi }_{t}+{a}_{t}\right)\\ & {\varphi }_{t+1}=\left({\varphi }_{t}+{a}_{t}\right)-{s}_{t}\end{array}$

Note: In general we round to the next integer. To ensure that we get binary spikes, i.e. $s\in \left\{0,1\right\}$$s \in \{0, 1\}$ we need ${\varphi }_{t}+{a}_{t}\in \left[0.5,1.5\right]$$\phi_t + a_t ∈ [0.5, 1.5]$ and because ${\varphi }_{t}\in \left[-0.5,0.5\right]$$ϕ_t ∈ [-0.5, 0.5]$ we want ${a}_{t}\in \left[0,1\right]$$a_t ∈ [0, 1]$ which we can insure by increasing the temporal resolution and tweaking ${k}_{p}$$k_p$ and ${k}_{d}$$k_d$ (see previous section).

But how can we reconstruct $a$$a$ from this? The get a a relation between $s$$s$ and $a$$a$ we can unroll the expression for ${\varphi }_{t+1}$$\phi_{t+1}$ for $n$$n$ steps

This gives us a relation between $\sum a$$∑a$ and $\sum s$$∑s$ which is a good starting point.

To get ${a}_{t}$$a_t$ we have to assume ${a}_{t}=\text{constant}$$a_t = \text{constant}$ over a series of timesteps $\left\{t-n,...,t\right\}$$\{t-n, ..., t\}$. Then, can write

For $n\to \infty$$n \to ∞$ we therefore have ${a}_{t}=\underset{n\to \infty }{lim}\frac{1}{n}\sum _{i=t-n}^{t}{s}_{i}$$a_t = \lim_{n \to ∞} \frac 1 n \sum_{i=t-n}^t s_i$. Since ${\varphi }_{t}\in \left[-0.5,0.5\right]$$ϕ_t ∈ [-0.5, 0.5]$ and $𝔼\left[{\varphi }_{t}\right]=0$$𝔼[ϕ_t] = 0$ we can assume that error term is small even for small $n$$n$. The scale of the sum, on the other hand, is (up to the error term) proportional to $a$$a$. That means the signal-to-noise ratio of the reconstruction depends heavily on which scaling constant $\frac{1}{{k}_{p}+{k}_{d}}$$\frac 1 {k_p + k_d}$ we use for $a$$a$.

Furthermore, the requirement for ${a}_{t}$$a_t$ to be constant across many timesteps is not a real limitation. We can just increase time resolution and increase $n$$n$ proportionally to make the error term small. So if $x$$x$ changes to quickly we can just make our timesteps smaller.

To “decode” the quantization we therefore have to average the quantized signal. Conveniently the decoding scheme from the previous section already does this implicity (approximately):

${x}_{t}\phantom{\rule{negativethinmathspace}{0ex}}=\phantom{\rule{negativethinmathspace}{0ex}}c\phantom{\rule{negativethinmathspace}{0ex}}\sum _{i=0}^{t}{\left(\frac{{k}_{d}}{{k}_{p}+{k}_{d}}\right)}^{t-i}\phantom{\rule{negativethinmathspace}{0ex}}\phantom{\rule{negativethinmathspace}{0ex}}\phantom{\rule{negativethinmathspace}{0ex}}\phantom{\rule{negativethinmathspace}{0ex}}\phantom{\rule{negativethinmathspace}{0ex}}{a}_{i}\approx c\phantom{\rule{negativethinmathspace}{0ex}}\phantom{\rule{negativethinmathspace}{0ex}}\sum _{i=t-n}^{t}\phantom{\rule{negativethinmathspace}{0ex}}{a}_{i}=c\phantom{\rule{negativethinmathspace}{0ex}}\phantom{\rule{negativethinmathspace}{0ex}}\sum _{i=t-n}^{t}\phantom{\rule{negativethinmathspace}{0ex}}\frac{1}{n}\phantom{\rule{negativethinmathspace}{0ex}}\phantom{\rule{negativethinmathspace}{0ex}}\sum _{j=t-n}^{t}\phantom{\rule{negativethinmathspace}{0ex}}\phantom{\rule{negativethinmathspace}{0ex}}{s}_{j}=c\phantom{\rule{negativethinmathspace}{0ex}}\phantom{\rule{negativethinmathspace}{0ex}}\sum _{i=t-n}^{t}{s}_{i}\approx c\phantom{\rule{negativethinmathspace}{0ex}}\phantom{\rule{negativethinmathspace}{0ex}}\sum _{i=0}^{t}{\left(\frac{{k}_{d}}{{k}_{p}+{k}_{d}}\right)}^{t-i}\phantom{\rule{negativethinmathspace}{0ex}}\phantom{\rule{negativethinmathspace}{0ex}}\phantom{\rule{negativethinmathspace}{0ex}}\phantom{\rule{negativethinmathspace}{0ex}}\phantom{\rule{negativethinmathspace}{0ex}}{s}_{i}=:{\stackrel{^}{x}}_{t}$

Therefore we don’t need a decoder ${Q}^{-1}$$Q^{-1}$ for the quantization such that we end up with the following pipeine.

$\stackrel{x}{⟶}\text{enc}⟶\text{Q}\stackrel{s}{⟶}\text{dec}\stackrel{\stackrel{^}{x}}{⟶}$

Below we can see how the combined signals look like for different encoding parameters.

## Integer weight multiplication

Right now we have established more efficient communication between the neurons but still not incorporated the weight multiplication.

So we have

Considering that ${\stackrel{^}{x}}_{t}$$\hat x_t$ is just a weighted sum, if we assume ${w}_{t}=\text{constant}$$w_t = \text{constant}$, we can pull it inside the sum

${z}_{t}\approx c\sum _{i=0}^{t}{\left(\frac{{k}_{d}}{{k}_{p}+{k}_{d}}\right)}^{t-i}{s}_{i}{w}_{t}:={\stackrel{^}{z}}_{t}$

Because ${s}_{i}$$s_i$ is integer we have achieved our goal of replacing the floating point multiplication with a cheaper sparse integer multiplication! The approximation error we make with the assumption ${w}_{t}=\text{constant}$$w_t = \text{constant}$ depends on how fast we decay the weights inside the sum, i.e. how large ${k}_{p}$$k_p$ is. Below is the final pipeline and a plot of the reconstruction for different ${k}_{p}$$k_p$.

## Learning the weights

To learn the weights we can apply the same coding scheme for backpropagation (by making the same assumptions). The symmetric backward pass through the transposed weights is not really biologically plausible but there is orthogonal work on biologically plausible backpropagation.

This leads to an efficient backward pass but in order to update the weights with gradient descend we need we neet to compute the outer product between the activations and the next pre-activation gradients: ${\nabla }_{w}L=x\otimes e$$∇_w L = x ⊗ e$ (where $e={\nabla }_{z}L$$e = ∇_z L$) which we both do not have access to.

The simplest solution would be to decode $\stackrel{^}{x}=\text{dec}\left(s\right)$$\hat x = \text{dec}(s)$ and $\stackrel{^}{e}=\text{dec}\left(\overline{e}\right)$$\hat e = \text{dec}(\bar e)$ before the inner product.

${\stackrel{^}{{\nabla }_{w}L}}_{\text{recon}}=\stackrel{^}{x}\otimes \stackrel{^}{e}$

Then we still have an expensive floating point multiplication, however. Instead we can use the fact that the result of the decoder $s\to \text{dec}\to \stackrel{^}{x}$$s → \text{dec} → \hat x$

decays exponentially in absence of spikes (i.e. ${s}_{t}=0$$s_t = 0$). Therefore we can calculate the sum over time between two spikes (pre-synaptic or post-synapic) analytically as a sum over a geometric series. It is fine sum the gradients over time and apply it as an update later, because that is what SGD does anyway.

Here, $t-n$$t-n$ is the time at which the last spike occured. If another spike occurs for either $\overline{x}$$\bar x$ or $\overline{e}$$\bar e$ we just add that sum to the corresponding weight (multiplied by the learning rate). That is called “past updates” in the paper.

## Summary

We looked at the forward pass, backward pass and weight updates from the paper and legitimized every step with solid math (most of which can also be found in the paper). This revealed the assumptions that had to be made and requirements on the hyperparameters as well as possible extension points to the method.