%!TEX spellcheck = en_US
%!TEX program = lualatex
% To compile with lualatex (using "latexmk -lualatex" for example).
% Page limit: 6 pages

\chapter{N2S3, an Open-Source Scalable Spiking Neuromorphic Hardware Simulator}
\label{N2S3-ch}
\graphicspath{{images/}}


\begin{abstract}
  One of the most promising approaches to overcome the end of Moore's law is neuromorphic computing. 
  Indeed, neural networks already have a great impact on machine learning applications and offer very 
  nice properties to cope with the problems of nanoelectronics manufacturing, such as a good tolerance
  to device variability and circuit defects, and a low activity, leading to low energy consumption. We 
  present here N2S3 (for Neural Network Scalable Spiking Simulator), an open-source simulator that is 
  built to help design spiking neuromorphic circuits based on nanoelectronics. N2S3 is an event-based 
  simulator and its main properties are flexibility, extensibility, and scalability. One of our goals 
  with the release of N2S3 as open-source software is to promote the reproducibility of research on 
  neuromorphic hardware. We designed N2S3 to be used as a library, to be easily extended with new 
  models and to provide a user-friendly special purpose language to describe the simulations.
\end{abstract}

\section{Introduction} % PierreB, Philippe
\label{sec:introduction}

Neuromorphic computing is a suitable alternative for conventional computation to take the advantages of
low power computing for future computer architectures~\cite{merolla_million_2014}.
In fact, parallel neuro-inspired computing, by performing computation and storage on the
same devices, can overcome the von Neumann bottleneck. Several large projects are
based on neuromorphic systems, such as the EU Human Brain
Project~\cite{markram_human_2012}, the DARPA/IBM SYNAPSE
project~\cite{ananthanarayanan_cat_2009}, and deep learning research led by Google
and Facebook, among others.

Recently, emerging nano-scale devices have demonstrated novel properties for
producing new memories and unconventional processing units.  One of those is the
memristor, that was hypothetically presented by Leon Chua in
1971~\cite{chua_memristor-missing_1971}; after a few decades, HP was the
first to announce a successful memristor
fabrication~\cite{strukov_missing_2008}. The unique properties of memristors,
such as extreme scalability, flexibility thanks to their analog behavior, and their
ability to remember their last state, make memristors very promising
candidates to be used as synapses in Spiking Neural Networks
(SNN)~\cite{jo_nanoscale_2010}.

Given their potential of very low power execution and their capability to handle
natural signals, we focus on SNN. A comprehensive introduction and literature review
about SNN was published by Paugam-Moisy and Bohte in
2012~\cite{paugam-moisy_computing_2012}. The authors explore the computational
capabilities of SNN, their learning capabilities, and their simulation.

Brette \textit{et al.}~\cite{brette_simulation_2007} surveyed and discussed the
existing work on SNN simulation in 2007. All the simulators discussed in this
chapter as well as the more recent Brian~\cite{goodman_brian:_2008} target the
simulation of biological SNN. More recently, Bichler \textit{et
al.}~\cite{bichler_design_2013} proposed Xnet, a C++ event-driven simulator
dedicated to the simulation of hardware SNN. In our work, we share the goals of
Xnet: ``intermediate modeling level, between low-level hardware description
languages and high-level neural networks simulators used primarily in
neurosciences'', and ``the integration of synaptic memristive device modeling,
hardware constraints and any custom features required for the targeted
application''. In addition to these goals, we put an emphasis on
\emph{flexibility and usability} to allow the study of various kinds of hardware
designs (possibly by other researchers than us), \emph{scalability} to simulate
large hardware SNNs, and \emph{software engineering best practices} (robust and
extensible software architecture for maintainability, extensive test suite,
continuous integration, open-source distribution).

\begin{wrapfigure}{r}{2.5cm}
  \includegraphics[width=2.5cm]{n2s3.png}
  \caption{N2S3 Logo}
\end{wrapfigure}

In this chapter we present N2S3 (Neural Network Scalable Spiking Simulator,
pronounced ``Nessie'', hence its logo), an event-driven simulator dedicated to
the exploration of hardware SNN architectures \cite{shahsavari_n2s3_2015}. The internals of N2S3 are based
on the exchanges of messages between concurrent actors~\cite{wyatt_akka_2013},
mimicking the exchange of spikes between neurons. N2S3 has been developed from
the ground up for extensibility, allowing to model various kinds of neuron and
synapse models, various network topologies (especially, it is not restricted to
feed-forward networks), various learning procedures, various reporting facilities,
and to be user-friendly, with a domain specific language to easily express and
run new experiments. It is available as open-source software at
\url{https://sourcesup.renater.fr/wiki/n2s3} so that its users can share
their models and experimental settings to enable others to reproduce their results.
In this spirit, N2S3 is distributed with the implementation of two ``classical''
experiments: handwritten digit recognition on the MNIST
dataset~\cite{lecun_gradient-based_1998,lecun_convolutional_1995} and the
highway vehicle counting experiment~\cite{bichler_extraction_2012}.

In the remainder of this chapter, we will first detail the fundamental architectural
choices of N2S3, then the models that are already implemented in N2S3, and
finally the features that N2S3 provides to implement and run experiments.


\section{Event-Driven Simulation Architecture}
\label{sec:simulation}

\subsection{Event-Driven vs Clock-Driven Simulation} % PierreB
\label{sub:EDvsCD}

SNN are essentially defined by standard differential equations, but
because of the discontinuities caused by the spikes, designing an efficient
simulation of spiking neural networks is a non-trivial problem. There are two
families of simulation algorithms: event-based simulators and clock-based ones. Synchronous or
``clock-driven'' simulation simultaneously updates all the neurons at every tick
of a clock, and is easier to code, especially on GPUs, for getting an efficient
execution of data-parallel learning algorithms. Event-driven simulation behaves more
like hardware, in which conceptually concurrent components are activated by
incoming signals (or events).
% We refer the reader to
% \cite{brette_simulation_2007} for a review of simulation strategies and tools
% dating from 2007.

Event-driven execution is particularly suitable for untethered devices such as
neurons and synapses, since the nodes can be put into a sleep mode to preserve
energy when no interesting event is happening. Energy-aware simulation needs
information about active hardware units and event counters to establish the
energy usage of each spike and each component of the neural network.
Furthermore, as the learning mechanisms of spiking neural networks are based
on the timings of spikes, the choice of the clock period for a
clock-based simulation may lead either to imprecision or to a higher computational
cost.

There is also a fundamental gap between this event-driven execution model and
the clock-based one: the first one is independent on the hardware
architecture of computers on which it is running. So, event-driven simulators can
naturally run on a grid of computers, with the caveat of synchronization issues
in the management of event timings.


\subsection{Technological Choices: Scala and Akka} % PierreB
\label{sub:scalaAkka}
To address our concurrency and
distributability requirements (i.e., ability to scale out a simulation on several
computers to handle large networks) we have chosen to use the Scala programming
language~\cite{odersky_overview_2006} along with the Akka actor
library~\cite{wyatt_akka_2013}.

\subsection{Software Architecture} % PierreF, Guille
\label{sub:achitecture}

The architecture of N2S3 is organized in Akka actors. Our choice for an actor
model aims mainly for the distribution and scalability of large neural networks.
Each entity of the simulation is deployed in a concrete actor that is assigned to
a concrete machine at runtime. The core of a typical neural network simulation in 
N2S3 is composed mainly by the following network entities:

\paragraph{Containers} Containers are network entities in charge of organizing the
network structure. Their main responsibility is to contain network entities and dispatch messages to their children.
\paragraph{Neurons} The basic building blocks of a neural network. In N2S3, a neuron 
is composed of both its nucleus~(the soma) and its incoming connections~(dentrites and associated synapses).

Each N2S3 actor has a network container capable of containing one or multiple entities.
This particularity allows us to control how distribution and parallelism is managed in 
each simulation. Figure~\ref{fig:architecture_example} illustrates the architecture with
an example network. In this figure, the simulation is made of one input and a neural network 
organized in two layers. The input of the simulation resides in one actor, the hidden layer 
is split in one actor per neuron, and all neurons of the output layer reside in a single actor.
The reason why one would put several neurons in the same or a different actor is based on the
scalability and the synchronization choices for the experiment. The topology of the network 
(discussed in more details in Section~\ref{sub:topology}) is one of the major influence on these questions.
\begin{figure}
	\centering
    	\includegraphics[scale=0.7]{n2s3_arch}
    	\caption{\textbf{N2S3 Architecture.} A network is organized in actors that may 
    	contain one or more network entities. Such entities could
    	be for example, neurons, inputs or any other.\label{fig:architecture_example}}
\end{figure}


The communication between simulation entities requires to identify each object by a URI made of a pair 
\texttt{\string(actor, local\_identifier\string)}. The component \texttt{actor} is the Akka actor that 
contains the entity and \texttt{local\_identifier} is a path that identifies each object uniquely within its actor.

Since actors are inherently concurrent, one concern is how the temporal order of messages is guaranteed 
during the simulation. To do so, N2S3 allows to configure several levels of synchronization to be used by 
the simulation designer. On one end of the spectrum, N2S3 may make use of a unique synchronizer for the 
simulation, which will ensure that no causality issues happen but can create a bottleneck that will affect 
the performance of the simulation. On the other end of the spectrum, we can also configure N2S3 to use a 
synchronization mechanism which is local to each neuron. The latter policy enables better parallelism, but 
may cause some temporal consistency problems.

\begin{figure}
  \begin{center}
  	\resizebox{\columnwidth}{!}{
  		\tikzstyle{package}=[rectangle, draw, fill=blue!20, 
    text width=7em, text centered, minimum height=4em]
%\tikzstyle{dependence} = [-{>[width=4mm,length=4mm]}, dashed]
\tikzstyle{dependence} = [->, dashed]
%\tikzstyle{extend} = [-{Triangle[open, angle=45:10pt]}]
\tikzstyle{extend} = [->]
\begin{tikzpicture}[auto]
  \node [package] (core) {core};
  \node [below = 1cm of core] (s1) {};
  \node [package, left = 1.5cm of s1] (features) {features};
  \node [package, right = 1.5cm of s1] (models) {models};
  
  \draw[-,black, dashed] (-4.8,-3.3) to[|-|] (5.5,-3.3);
  \node[draw=none] at (4.4,-3) {N2S3 library};
  \node[draw=none] at (4.4,-3.6) {User application};
  
  \node [below = 3cm of s1] (s2) {};
  \node [package, left = 1.5cm of s2] (apps) {user.apps};
  \node [package, right = 1.5cm of s2] (umodels) {user.models};
  
  \draw[dependence] (features.north) -- node[fill=white, auto]{uses} (core.west);
  \draw[extend] (models.north) -- node[fill=white,pos=.3,above]{extends} (core.east);
  
  \draw[dependence] (apps.north) -- node[fill=white,pos=.3,auto]{uses} (features.south);
  \draw[dependence] (apps.45) -- node[fill=white, below]{uses} (core.south);
  \draw[dependence] (apps.east) -- node[fill=white]{uses} (umodels.west);
  \draw[extend] (umodels.north) -- node[fill=white,pos=.3]{extends} (models.south);
\end{tikzpicture}
  	}
  \end{center}
  \caption{N2S3 Packages}
\end{figure}
  

\section{Neuron, Synapse, Network Modeling}
\label{sec:modeling}

\subsection{Neuron Modeling} % Mahyar
\label{sub:neuron}

The neuron is a dynamic element and processing unit that emits output pulses
whenever the excitation exceeds some threshold. The resulting sequence of pulses
or spikes contains all the information that is transmitted from one neuron to
another one. As we have chosen event-driven simulation, the state of a
neuron is updated only when an event representing a spike is received.


% There are several models for neuron. The first spiking model of neuron in is
% Hodgkin-Huxley~\cite{hodgkin_quantitative_1952} which other models such as
% Integrate-and-Fire (I\&F) and Izhikevich~\cite{izhikevich_simple_2003} are
% inspired from this model.

%\subsubsection{Leaky-Integrate-and-Fire (LIF) neuron}

We provide by default a \textit{Leaky-Integrate-and-Fire (LIF)} neuron
model~\cite{LIF1999}.
% This model is
% well-studied model of the neuron that we used it as a first model in the
% simulation.
There are several reasons for using a LIF model:
\begin{itemize}
  \item CMOS technology fabrication of this model is available~\cite{chicca_vlsi_2003,liu_temporal_2004};
  % \item this LIF model is effectively well-suited in spiking neural network platforms~\cite{LIF1999}; \ptp{I don't understand this sentence.}
  \item it is fast enough to simulate, and in particular it is effective for
  large-scale network simulations~\cite{brette_simulation_2007}.
\end{itemize}

In LIF, neurons integrate the spikes coming from other neurons. These spikes can change
the internal potential of the neuron, which is known as the \textit{membrane
potential} or \textit{state variable} of the neuron. When the membrane potential reaches a
given threshold voltage after integrating enough input spikes, an action potential occurs;
in other words, the neuron fires, generating an output spike. This is done by updating
the membrane potential using the value computed when
receiving the last spike and the analytical solution to the differential
equations defining the behavior of the LIF neuron.

% The basic behavior of the model is introduced by the neuron membrane potential:
% \begin{align}
%  \tau_{n}\frac{\mathrm{d} v}{\mathrm{d} t}&= -v(t)+RI_{syn}(t) \label{eq1} \\
%  I_{syn}(t)&=\sum_{i}g_{ji}\sum_{post}\alpha(t-t_{i}^{(post)}) \label{eq2}
% \end{align}
% where, $v(t)$ represents the membrane potential at time t, $\tau_n=RC$ is the
% membrane time constant and $R$ is the resistance of the membrane.
% $I_{syn}(t)$ is an input current that follows from synapse to the neuron.
% The total input current injected to a neuron is
% the sum over all current pulses which is calculated in Equation~\ref{eq2}. Time
% $t_{i}^{(post)}$ represents the time of the $n_{th}$ spike of post-synaptic neuron
% $i$, and $g_{ji}$ is the conductance of synaptic efficacy between neuron $j$ and
% neuron $i$. Function $\alpha(t)=q\delta(t)$, where $q$ is the injected charge to
% the artificial synapse and $\delta(t)$ is the Dirac pulse function. If
% $I_{syn}(t)$ is big enough where action potential can pass the threshold
% voltage, neuron fires. It means there are enough input spikes in a short time
% window. When there is no or only a few spikes in a time window, the neuron is in
% the leaky phase and the state variable decreases exponentially. The duration of
% this time window depends on $\tau_n=RC$. The equation is analytically solvable
% and thus we use the answer of Equation~\ref{eq1} in the network simulation.
% \pp{Give this answer.}

In order to improve the learning capabilities of neural networks, we provide two
refinements of the neuron model: homeostasis and a refractory period.
Homeostasis~\cite{marder_variability_2006} dynamically adapts the threshold of
each neuron to the activity of this neuron to prevent a neuron from being
over-active or inactive. To allow more specialization of the neurons, we include
a refractory period after the firing of a neuron during which it ignores
incoming spikes.


% \subsubsection{Homeostasis}
% In STDP learning, the synaptic input of a neuron
% may strongly increase or decrease for a long time and as
% a result, the neural activity will be drifting to a extremely
% high or low level.
% Homeostasis is a neuron
% property that regulates the firing threshold to prevent a neuron to be
% hyperactive~\cite{marder_variability_2006} or disactive. This method produce an adaptive
% threshold for the membrane potential based on neuron activity. If the neuron is too active in a short
% time window the threshold grows gradually; accordingly, when a neuron is not active
% in a certain time window the threshold is reduced slightly.
% \begin{equation}
%  \frac{\mathrm{d}V_{th}} {\mathrm{d} x}=\gamma (fr_{mean}-fr_{target})
% \end{equation}
% where $fr_{mean}$ is the mean activity (or firing rate) of a neuron, $fr_{target}$ is
% the target activity, and $\gamma$ is a multiplicative positive constant.
% Consequently, the
% activity of the neuron is bounded in a homeostatic range
% to encode the synaptic input more effectively to improve the
% STDP learning~\cite{li_review_2016}.
% \subsubsection{Winner-take-all}
% In unsupervised learning using spike coding and plasticity learning,
% without competition, all the neurons would behave alike
% and no specialization occurs in the neurons.
% The theoretical analysis shows that winner-take-all is surprisingly powerful computational
% method compared with threshold gate (McCulloch-Pitts neuron) and sigmoidal gate
% \cite{maass_computational_2000}. In WTA, after the competition, only one neuron
% will be the most active for some inputs and the rest of the
% neurons will eventually become inactive for those inputs.
% WTA activates only the unit that fits the
% input pattern best and suppresses the others by sending
% inhibitory signals to all other neurons in the same layer through fixed inhibitory connections.

\subsection{Synapse modeling and learning} % Mahyar
\label{sub:synapse}

A synapse operates as a plastic controller between two neurons. This
plasticity is believed to be the origin of the learning and memorization capabilities of the
brain~\cite{martin_synaptic_2000}. Hence, hardware spiking neural networks
usually use some kind of synaptic plasticity to enable learning. In N2S3, we have
modeled and simulated hardware synapses and one standard learning behavior, namely
Spike Timing-Dependent Plasticity (STDP).

STDP is a local weight modification based on the timing difference between
presynaptic spikes and postsynaptic spikes. It increases the connectivity between
the neurons when there is a temporal causality between the spikes they emitted. This is
implemented by locally remembering the past spikes and updating the synaptic
weight according to the chosen STDP rule.

% Before the fabrication of memristor nanodevice, using the state-of-the-art
% technology, 12 transistors were applied to mimic the behavior of make a
% artificial synapse appropriate  to perform the STDP learning
% method~\cite{indiveri_neuromorphic_2003}. Therefore, using a two-terminal and
% scalable device such as memristor remarkable amount of power and cost will be
% effectively saved specially in modeling large scale Spiking Neural Network
% architectures.
Using a memristor as a nonvolatile synaptic memory has been
proposed in previous work~\cite{alibart_organic_2010,jo_nanoscale_2010,
kim_memristor_2012, indiveri_integration_2013-1}.
% It has been modelled in our
% previous work~\cite{shahsavari_unconventional_2015}.
The basic artificial synapse model that we provide is taken
from~\cite{querlioz_immunity_2013, querlioz_learning_2011}, which describes a
nonvolatile memristor suitable for event-driven and STDP computation. In
addition, we designed a new model of synapse which is able to forget unimportant
events and remember significant events by combining a nonvolatile memristor and a
volatile one~\cite{shahsavari_combining_2016}.



% There are various models of STDP e.g.\ pair-based, triplet, suppression, and
% Voltage dependence model~\cite{babadi_stability_2016}. The pair-based STDP model
% has been used in this simulation to modify the synaptic weight (conductance) and
% train the network as it is more simple and fast enough for
% learning~\cite{morrison_phenomenological_2008}. Pair-based STDP model is shown
% in Figure~\ref{STDP}.a, if there is an output spike in the pre-synaptic neuron
% and shortly after in the post-synaptic neuron, the conductance of the synapse
% between two neurons increases. On the other hand, if the post-synaptic neuron
% spikes shortly before the pre-synaptic neuron, the conductance of synapse will
% be decreased as it is shown in Figure~\ref{STDP}.b. The manner of how
% conductance of a synapse is modified in our modeling is depicted in
% Figure~\ref{modifys}.

%
% \begin{figure}[ht]
% \centering
%   \includegraphics{STDP-N2s3}
%  \caption{STDP learning. \textbf{a)} Pair-based STDP using local variables. The
% spikes of presynaptic neuron j leave a trace $x_{j}(t)$ and the spikes of the
% postsynaptic neuron i leave a trace $x_{i}(t)$. The update of the weight $W_{ji}$
% at the moment of a postsynaptic spike is proportional to the momentary
% value of the trace $x_{j}(t)$ (filled circles). This gives the amount of
% potentiation due to pre-before-post pairings. Analogously, the update
% of $W_{ji}$ on the occurrence of a presynaptic spike is proportional to the
% momentary value of the trace $x_{i}(t)$ (unfilled circles), which gives the
% amount of depression due to post-before-pre pairings.
%   \textbf{b)} Basic of spike-timing-dependent plasticity. The STDP function
% expresses the change of synaptic weight as a function of the relative timing of
% pre- and post-synaptic spikes.}
% \label{STDP}
% \end{figure}
%
% \begin{figure}
% \centering
%
% \tikzstyle{startstop} = [rectangle, rounded corners, minimum size=1cm,text centered, draw=black, fill=red!30]
% \tikzstyle{io} = [trapezium, trapezium left angle=70, trapezium right angle=110, minimum size=1cm, text centered, draw=black, fill=blue!30]
% \tikzstyle{process} = [rectangle, minimum width=3cm, minimum height=1cm, text centered, text width=3cm, draw=black, fill=orange!30]
% \tikzstyle{decision} = [diamond, minimum width=1.5cm, minimum height=0.5cm, text centered,text width=2cm, draw=black, fill=green!30]
% \tikzstyle{arrow} = [thick,->,>=stealth]
%
%
% \begin{tikzpicture}%[node distance=1.7cm]
%
% \node (start) [startstop,minimum size=.5cm] {Start};
% \node (in1) [io, below of=start, minimum size=.4cm] {\tiny{waite for input spikes}};
% \node (pro1) [process, below of=in1,minimum size=.5cm,scale=0.7] {$\Delta t=t_{i}^{post}-t_{j}^{pre}$};
% \node (dec1) [decision, below of=pro1, yshift=-0.5cm,scale=0.6,node distance=1.3cm] {If preSynaptic Spike?};
% \node (pro2a) [decision, below of=dec1, yshift=-0.5cm, scale=0.6,node distance=1.9cm] {If postSynaptic Spike?};
% \node (pro2b) [decision, right of=dec1, xshift=2cm, scale=0.6, node distance=1cm] {If postSynaptic Spike?};
% \node (out1) [decision, below of=pro2a,scale=0.6,node distance=2.3cm] {\small{$0<\Delta t<stdpwin$}};
% \node (out2) [decision, below of=pro2b,scale=0.6,node distance=2.3cm] {\small{\textbf{$-stdpwin<\Delta t<0$}}};
% \node (stop) [process, below of=out1, scale=0.6, node distance=1.6cm] {Increase synapse conductance $g$};
% \node (stop1) [process, below of=out2, scale=0.6, node distance=1.6cm] {Decrease synapse conductance $g$};
% \node (in2) [io, below of=stop1, minimum size=.4cm] {\tiny{waite for input spikes}};
% \draw [arrow] (start) --(in1);
% \draw [arrow] (in1) -- (pro1);
% \draw [arrow] (pro1) -- (dec1);
% \draw [arrow] (dec1) -- node[anchor=east] {yes} (pro2a);
% \draw [arrow] (dec1) -- node[anchor=south] {no} (pro2b);
% \draw [arrow,distance=0.5cm] (pro2b) |- node[anchor=south] {no}(in1);
% \draw [arrow] (pro2a) -- node[anchor=east] {yes}(out1);
% \draw [arrow] (pro2b) -- node[anchor=east] {yes}(out2);
% \draw [arrow] (pro2a.east)  -|  (in2.west);
% \draw [arrow] (out1) -- node[anchor=east] {yes}(stop);
% \draw [arrow] (out1.east)  --  node[anchor=south]{no}(in2.west);
% \draw [arrow] (out2) -- node[anchor=east] {yes}(stop1);
% \draw [arrow] (stop1) -- (in2);
% \draw [arrow] (out2.west)  -|  node[anchor=south]{no}(in2.west);
% \draw [arrow] (stop.east) -| (in2.south);
%
% \end{tikzpicture}
% \caption{\textbf{Algorithm for Synapse modification in N2S3.} Stdpwin value
% depends on synapse parameters such as maximum and minimum conductance.}
% \label{modifys}
% \end{figure}


\subsection{Network Topologies} % PierreF
\label{sub:topology}

Another purpose of N2S3 is to allow an easy exploration of different neural network configurations. 
In order to facilitate the network construction, neurons are gathered into groups.
Those groups, which usually represent neuron layers, are organized into a specified shape. 
This information allows the automation of the builder and of the visualization creation.

Inside a group one can specify the use of lateral inhibition to implement the
winner-take-all rule~\cite{maass_computational_2000} that states that a spiking
neuron inhibits its neighbors during a short time period to allow the
specialization of the neurons and hence to enhance unsupervised learning.

Neuron groups can be connected to each other. Connection patterns are managed 
by different connection policies. By default, N2S3 uses unidirectional full 
connections (each neuron of the input group is connected to every neuron of 
the output group). Other policies are also bundled with N2S3, such as the 
\emph{one-to-one} policy, which connects each neuron of the input group to 
only the corresponding neuron of the output group, and a random policy, 
which creates connections according to a given probability distribution, 
e.g. to enable Reservoir Computing~\cite{verstraeten_experimental_2007} modeling.

Several builders have the responsibility to create different topologies. 
They can use the information of an existing layer (e.g., the shape of the neuron group), 
if there is one, to construct a new one. Builders bundled with N2S3 include, for instance, 
builders for the typical layers of  Convolutional Neural Networks~\cite{lecun_convolutional_1995}.
%This last, highly inspired by the primary visual cortex (V1), use stacked layers.

\section{N2S3 Features}
\label{sec:experiments}

\subsection{Input Processing} % PierreF, Guille
\label{sub:IO}

The simulator uses a piped stream system to provide stimuli to the network entities. 
The input process typically starts by an input reader, which reads data from files 
or any external source, followed by a number of streams that filter the input 
data before feeding it to the network.

Input readers provided with N2S3 allow to read data in a variety of format, 
including standard formats such as Address Event Representation (AER), 
a data format used by spike-based cameras, or MNIST, used in a standard 
dataset for handwritten digit recognition (see Section~\ref{sub:experiments} for details about the data).

Subsequent filter streams available in N2S3 include coding streams, which convert raw numerical data into
sequences of spike timings, spike presentation streams (e.g., repeating input spike over a given period, 
or shuffling spikes), and modifier streams, that alter the input spikes (e.g., by adding noise). 
Users are free to use one or multiple input readers and to combine any number of filter streams 
in any order; they may also easily create their own readers and filters.
\subsection{Visualization tools}
\label{sub:viz}

Users may observe simulation outputs (spikes, weight values\ldots) through network observers.
Network observers follow the observer pattern by subscribing to events in the simulation 
(e.g., when a spike happens), perform some calculations on such events, and make them 
visible to the user. Examples of such observers range from textual loggers to dynamic 
visualizations of the spikes of each neuron. Concretely, N2S3 provides a spike activity 
map of the network, a synaptic weight evolution visualizer, and the calculation of 
evaluation metrics (e.g. recognition rates, confusion matrices\ldots).
\subsection{Experiment Specification Language} % Guille
\label{subs:ESL}

N2S3 includes a dedicated internal Domain Specific Language~(DSL) that aims to simplify the creation of simulations. At a higher level of abstraction, users can design experiments (network topology, neuron and synapse properties, observation units\ldots) without having to deal with core features such as synchronization or actor policies. The snippet of code below illustrates (1) the creation of a layer with 18 standard LIF neurons with a 20 mV threshold, (2) its full connection to an existing layer (named \texttt{outputLayer}), and the addition of two observers: (3) a standard weight observer and (4) a custom (i.e., defined by the user) spike observer. The DSL also allows the definition of different stages for the simulation (e.g., splitting the simulation into a training phase and a test phase).

\begin{lstlisting}
val hiddenLayer =
  n2s3.createNeuronGroup() // (1)
    .setNumberOfNeurons(18)
    .setDefaultNeuronConstructor( () => {
      new QBGNeuron()
        .setProperty(NeuronThreshold, 20 millivolts)
    })
hiddenLayer.connectTo(outputLayer) // (2)
n2s3.createSynapseWeightGraphOn(hiddenLayer, outputLayer) // (3)
n2s3.addNetworkObserver(new SpikeLogger) // (4)
\end{lstlisting}
At a lower level, the DSL can specify how neurons are organized within actors 
(e.g., deploying all neurons within the same actor or each neuron in a specific actor).
Users may use pre-existing policies or define their own. 

\subsection{Software Engineering Practices} % PierreF, Guille
\label{sub:engineering}

To ensure the quality of the simulator's core and its surrounding features,
we follow several good practices of sofware engineering. We use automated 
tests~(unit tests and integration tests) to validate the basic behavior of 
the simulator. These unit tests are run both locally and within a continuous 
integration environment to detect regressions in the project as soon as possible. 
N2S3's codebase is open-source, and its developers perform periodic code reviews on it.

\subsection{Standard experiments} % PierreT, Mahyar
\label{sub:experiments}

N2S3 comes with a set of pre-implemented experiments from the literature. 
These implementations both demonstrate the features and simulation accuracy of 
N2S3 and provide code snippets to allow users to understand the ESL and help 
them designing their own simulations. Specifically, experiments for two standard 
tasks are provided with N2S3: handwritten digit recognition on the MNIST dataset 
and car counting on the Freeway dataset. In both cases N2S3 provide simulation 
results comparable to those reported in the literature.

\begin{figure}
  \centering
  \includegraphics[width=0.8\linewidth]{images/30.png}
  \caption{Heat map of the synaptic weights after learning the MNIST data base with 30 neurons on the output layer.}
  \label{fig:mnist}
\end{figure}
MNIST~\cite{lecun_gradient-based_1998} is a standard dataset for automatic handwritten digit recognition. 
Since its creation, it has been extensively used to evaluate algorithms for machine learning, 
computer vision and document recognition.  The task is to learn to identify which digit is 
represented on each image. The dataset includes 60,000 greyscale images of size \(28 \times 28\) 
pixels, divided into a training set (50,000 images) and a test set (10,000). The implementation of 
SNN learning on MNIST provided with N2S3 follows the experimental settings from~\cite{querlioz_bioinspired_2012}: 
\(28 \times 28\) inputs (1/pixel) and one output layer (10 to 300 neurons). Figure~\ref{fig:mnist} shows the 
result of learning the MNIST database with 30 neurons on the output layer and winner-take-all activated to 
enhance learning. The initial synaptic weights are random.

\begin{figure}
  \centering
  \includegraphics[scale=0.5]{images/spikes.png}
  \caption{Input data for the freeway experiment comming from a spike-based camera. The spikes represent a variation of 
  intensity for a given pixel and are generated asynchronously.}
  \label{fig:freeway_input}
\end{figure}
\begin{figure}
  \centering
  \includegraphics[scale=0.17]{images/freeway_all.png}
  \caption{Heatmap showing the reconstruction of the contribution of each input pixel to the activity of the 10 output
  neurons for the freeway experiment. One can see that some neurons have clearly specialized to detect vehicles on a particular lane.}
  \label{fig:freeway_output}
\end{figure}
Freeway~\cite{boahen_point--point_2000} is an AER (Address Event Representation) video of a Freeway in Pasadena. 
It is a standard video demonstrating the capabilities of spike-based cameras. The task here is to
count the number of vehicles passing over each of the six lanes of the freeway. The input data is
the spikes recorded by a spiking camera of resolution $28 \times 28$ pixels over a sequence of 
78.5 seconds (5.2 millions spikes). 
The implementation of the Freeway experiment provided with N2S3 reproduces the experimental 
architecture and settings described in~\cite{bichler_extraction_2012}: 
\(2 \times 28 \times 28\) inputs, one hidden layer (60 neurones), 
one output layer (10 neurons), with lateral inhibition on every layer. 
Figure~\ref{fig:freeway_input} represents the input of the neural network and 
figure~\ref{fig:freeway_output} shows the result of the unsupervised learning process.

\section{Conclusion} % PierreB, Philippe[width=\linewidth]
\label{sec:conclusion}

We have presented in this chapter a spiking neural network simulator, N2S3. This
simulator is dedicated to nanoelectronics-based hardware neural networks.
Its simulation strategy is event-based, for precision and flexibility sakes. As one of
the main goals of N2S3 is to promote open research data, i.e. the open
distribution of models and experiments for research result reproducibility, it
is distributed as open-source software (at
\url{https://sourcesup.renater.fr/wiki/n2s3}), and a lot of effort has
been put in the software architecture, maintainability and enabling features
(I/O, graphical outputs, logging, domain specific language). N2S3 is distributed
with a few neuron and synapse models. It can simulate not only feed-forward
networks but also convolutional or even recurrent networks. Finally, it is extensively
extensible.

We are planning to periodically release new versions in a timeboxed fashion and
to perform more quality measurements in the near future, for example: continuous
benchmarking and performance testing to detect non-functional regressions,
adding automated code quality and architectural rules enforcement to detect potential
bugs, ensuring the respect for architectural and programming idioms, and agilizing
the code reviews.

On the functional side, future work include new models of neurons and synapses,
new learning algorithms and procedures, noise modeling, energy modeling,
automatic design space exploration, and new applications.