snntoolbox.simulation

On the output side of the toolbox, the following simulators are currently implemented:

pyNN_target_sim Building and simulating spiking neural networks using pyNN.
brian2_target_sim Building and simulating spiking neural networks using Brian2.
MegaSim_target_sim MegaSim asynchronous spiking simulator.
INI_temporal_mean_rate_target_sim INI simulator with temporal mean rate code.
INI_temporal_pattern_target_sim INI simulator with temporal pattern code.
INI_ttfs_target_sim INI simulator with time-to-first-spike code.
INI_ttfs_dyn_thresh_target_sim INI simulator with time-to-first-spike code and a dynamic threshold.
INI_ttfs_corrective_target_sim INI simulator with time-to-first-spike code and corrective spikes.
snntoolbox.simulation.target_simulators.loihi_target_sim
spiNNaker_target_sim Building and simulating spiking neural networks using SpiNNaker.

The abstract base class AbstractSNN for the simulation tools above is contained here:

snntoolbox.simulation.utils Common functions for spiking simulators.

See Extending the toolbox on how to extend the toolbox by another simulator.

The backends for our built-in simulator INIsim and the custom simulator MegaSim are included here:

temporal_mean_rate_tensorflow INI temporal mean rate simulator with Tensorflow backend.
snntoolbox.simulation.backends.inisim.temporal_mean_rate_theano
temporal_pattern INI temporal pattern simulator backend.
ttfs INI time-to-first-spike simulator backend.
ttfs_dyn_thresh INI time-to-first-spike simulator backend with dynamic threshold.
ttfs_corrective INI time-to-first-spike simulator backend with corrective spikes.
megasim MegaSim spiking neuron simulator.

Finally, utility functions for plotting are contained in

snntoolbox.simulation.plotting Various functions to visualize connectivity, activity and accuracy of the network.

snntoolbox.simulation.utils

Common functions for spiking simulators.

Most notably, this module defines the abstract base class AbstractSNN used to create spiking neural networks. This class has to be inherited from when another simulator is added to the toolbox (see Extending the toolbox).

@author: rbodo

class snntoolbox.simulation.utils.AbstractSNN(config, queue=None)[source]

Bases: object

Abstract base class for creating spiking neural networks.

This class provides the basic structure to compile and simulate a spiking neural network. It has to be instantiated as in target_simulators.pyNN_target_sim with concrete methods tailored to the target simulator.

Core methods that usually do not have to be overwritten include:

build Assemble a spiking neural network to prepare for simulation.
run Simulate a spiking network.
get_recorded_vars Retrieve neuron variables recorded during simulation.

Relevant methods that in most cases will have to be overwritten include:

add_input_layer Add input layer.
add_layer Do anything that concerns adding any layer independently of its type.
build_dense Build spiking fully-connected layer.
build_convolution Build spiking convolutional layer.
build_pooling Build spiking pooling layer.
build_flatten Build flatten layer.
compile Compile the spiking network.
simulate Simulate a spiking network for a certain duration, and record any variables of interest (spike trains, membrane potentials, …)

Notes

In the attribute definitions below, we use suffixes to denote variable shape. In spiketrains_n_b_l_t for instance, n represents a dimension across the network (i.e. indexing the layers); b is an index for a batch of samples; l stands for the layer dimensions; t indicates the time axis.

config

Settings.

Type:configparser.ConfigParser
queue

Used to detect stop signals from user to abort simulation.

Type:Queue.Queue
parsed_model

The parsed model.

Type:keras.models.Model
is_built

Whether or not the SNN has been built.

Type:bool
batch_size

The batch size for parallel testing of multiple samples.

Type:int
spiketrains_n_b_l_t

Spike trains of a batch of samples of all neurons in the network over the whole simulation time. Each entry in spiketrains_batch contains a tuple (spiketimes, label) for each layer of the network. spiketimes is an array where the last index contains the spike times of the specific neuron, and the first indices run over the number of neurons in the layer: (batch_size, n_chnls, n_rows, n_cols, duration). label is a string specifying both the layer type and the index, e.g. '03Conv2D_32x64x64'.

Type:list[tuple[np.array, str]]
activations_n_b_l

Activations of the ANN.

Type:list[tuple[np.array, str]]
mem_n_b_l_t

Membrane potentials of the SNN.

Type:list[tuple[np.array, str]]
input_b_l_t

Input to the SNN over time.

Type:ndarray
top1err_b_t

Top-1 error of SNN over time. Shape: (batch_size, _num_timesteps).

Type:ndarray
top5err_b_t

Top-5 error of SNN over time. Shape: (batch_size, _num_timesteps).

Type:ndarray
synaptic_operations_b_t

Number of synaptic operations of SNN over time. Shape: (batch_size, _num_timesteps)

Type:ndarray
neuron_operations_b_t

Number of updates of state variables of SNN over time, e.g. caused by leak / bias. Shape: (batch_size, _num_timesteps)

Type:ndarray
operations_ann

Number of operations of ANN.

Type:float
top1err_ann

Top-1 error of ANN.

Type:float
top5err_ann

Top-5 error of ANN.

Type:float
num_neurons

Number of neurons in the network (one entry per layer).

Type:List[int]
num_neurons_with_bias

Number of neurons with bias in the network (one entry per layer).

fanin

Number of synapses targeting a neuron (one entry per layer).

Type:List[int]
fanout

Number of outgoing synapses. Usually one entry (integer) per layer. If the post- synaptic layer is a convolution layer with stride > 1, the fanout varies between neurons.

Type:List[Union[int, ndarray]]
rescale_fac

Scales spike probability when using Poisson input.

Type:float
num_classes

Number of classes of the data set.

Type:int
top_k

By default, the toolbox records the top-1 and top-k classification errors.

Type:int
sim

Module containing utility functions of spiking simulator. Result of calling snntoolbox.bin.utils.initialize_simulator(). For instance, if using Brian simulator, this initialization would be equivalent to import pyNN.brian as sim.

Type:Simulator
flatten_shapes

List containing the (name, shape) tuples for each Flatten layer.

Type:list[(str, list)]
is_parallelizable

Whether or not the simulator is able to test multiple samples in parallel.

add_input_layer(input_shape)[source]

Add input layer.

Parameters:input_shape (list | tuple) – Input shape to the network, including the batch size as first dimension.
add_layer(layer)[source]

Do anything that concerns adding any layer independently of its type.

Parameters:layer (keras.layers.Layer | keras.layers.Conv) – Layer
build_dense(layer)[source]

Build spiking fully-connected layer.

Parameters:layer (keras.layers.Dense) – Layer
build_convolution(layer)[source]

Build spiking convolutional layer.

Parameters:layer (keras.layers.Layer) – Layer
build_pooling(layer)[source]

Build spiking pooling layer.

Parameters:layer (keras.layers.pooling._Pooling2D) – Layer
build_flatten(layer)[source]

Build flatten layer.

May not be needed depending on the simulator.

Parameters:layer (keras.layers.Layer) – Layer
compile()[source]

Compile the spiking network.

simulate(**kwargs)[source]

Simulate a spiking network for a certain duration, and record any variables of interest (spike trains, membrane potentials, …)

Returns:output_b_l_t – Array of shape (batch_size, num_classes, num_timesteps), containing the number of output spikes of the neurons in the final layer, for each sample and for each time step during the simulation.
Return type:ndarray
reset(sample_idx)[source]

Reset network variables.

Parameters:sample_idx (int) – Index of sample that has just been simulated. In certain applications (video data), we may want to turn off reset between samples.
end_sim()[source]

Clean up after simulation.

save(path, filename)[source]

Write model architecture and parameters to disk.

Parameters:
  • path (string) – Path to directory where to save model.
  • filename (string) – Name of file to write model to.
load(path, filename)[source]

Load model architecture and parameters from disk.

Parameters:
  • path (str) – Path to directory where to load model from.
  • filename (str) – Name of file to load model from.
init_cells()[source]

Set cellparameters of neurons in each layer and initialize membrane potential.

get_spiketrains(**kwargs)[source]

Get spike trains of a layer.

Returns:spiketrains_b_l_t – Spike trains.
Return type:ndarray
get_spiketrains_input()[source]

Get spike trains of input layer.

Returns:spiketrains_b_l_t – Spike trains of input.
Return type:ndarray
get_spiketrains_output()[source]

Get spike trains of output layer.

Returns:spiketrains_b_l_t – Spike trains of output.
Return type:ndarray
get_vmem(**kwargs)[source]

Get membrane potentials of a layer.

Returns:mem_b_l_t – Membrane potentials of layer.
Return type:ndarray
build(parsed_model, **kwargs)[source]

Assemble a spiking neural network to prepare for simulation.

Parameters:parsed_model (keras.models.Model) – Parsed input model.
run(x_test=None, y_test=None, dataflow=None, **kwargs)[source]

Simulate a spiking network.

This methods takes care of preparing the dataset for batch-wise processing, and allocates variables for quantities measured during the simulation. The simulate method (overwritten by a concrete target simulator) is responsible for actually simulating the network over a given duration, and reporting the measured quantities. The run method then deals with evaluating this data to print statistics, plot figures, etc.

Parameters:
  • x_test (float32 array) – The input samples to test. With data of the form (channels, num_rows, num_cols), x_test has dimension (num_samples, channels*num_rows*num_cols) for a multi-layer perceptron, and (num_samples, channels, num_rows, num_cols) for a convolutional net.
  • y_test (float32 array) – Ground truth of test data. Has dimension (num_samples, num_classes)
  • dataflow (keras.DataFlowGenerator) – Loads images from disk and processes them on the fly.
  • kwargs (Optional[dict]) –

    Optional keyword arguments, for instance

    • path: Optional[str]
      Where to store the output plots. If no path given, this value is taken from the settings dictionary.
Returns:

top1acc_total – Number of correctly classified samples divided by total number of test samples.

Return type:

float

setup_layers(batch_shape)[source]

Iterates over all layers to instantiate them in the simulator

adjust_batchsize()[source]

Reduce batch size to single sample if necessary.

Not every simulator is able to simulate multiple samples in parallel. If this is the case (indicated by is_parallelizable), set batch_size to 1.

restore_snn()[source]

Restore both the spiking and the parsed network from disk.

This method works for spiking Keras models.

init_log_vars()[source]

Initialize variables to record during simulation.

reset_log_vars()[source]

Reset variables to record during simulation.

set_connectivity()[source]

Set connectivity statistics needed to compute the number of operations in the network. This includes e.g. the members fanin, fanout, num_neurons, num_neurons_with_bias.

get_recorded_vars(layers)[source]

Retrieve neuron variables recorded during simulation.

If recorded, spike trains and membrane potentials will be inserted into the respective class members spiketrains_n_b_l_t and mem_n_b_l_t. In any case, this function must return an array containing the output spikes for each sample and time step.

Parameters:layers – List of SNN layers.
Returns:output_b_l_t – The output spikes. Shape: (batch_size, layer_shape, num_timesteps)
Return type:ndarray
reset_container_counters()[source]
set_mem_stats(mem, v_thresh)[source]

Write recorded membrane potential out and plot it.

set_spiketrain_stats_input()[source]

Count number of operations based on the input spike activity during simulation.

set_spiketrain_stats(spiketrains_b_l_t)[source]

Count number of operations based on the spike activity during simulation.

Parameters:spiketrains_b_l_t (ndarray) – A batch of spikes for a layer over the simulation time. Shape: (batch_size, layer_shape, num_timesteps)
reshape_flattened_spiketrains(spiketrains, shape, is_list=True)[source]

Convert list of spike times into array where nonzero entries (indicating spike times) are properly spread out across array. Then reshape the flat array into original layer shape.

Parameters:
  • spiketrains (ndarray) – Spike times.
  • shape – Layer shape.
  • is_list (Optional[bool]) – If True (default), spiketrains is a list of spike times. In this case, we distribute the spike times across a numpy array. If False, spiketrains is already a 2D array of shape (num_neurons, num_timesteps).
Returns:

spiketrains_b_l_t – A batch of spikes for a layer over the simulation time. Shape: (batch_size, shape, num_timesteps)

Return type:

ndarray

get_avg_rate_from_trains()[source]

Compute spike rate of neurons averaged over batches, the neurons in the network, and the simulation time.

preprocessing(**kwargs)[source]
Parameters:kwargs
snntoolbox.simulation.utils.get_samples_from_list(x_test, y_test, dataflow, config)[source]

If user specified a list of samples to test with config.get('simulation', 'sample_idxs_to_test'), this function extracts them from the test set.

snntoolbox.simulation.utils.build_1d_convolution(layer, delay)[source]

Build convolution layer.

Parameters:
  • layer (keras.layers.Conv1D) – Parsed model layer.
  • delay (float) – Synaptic delay.
Returns:

  • connections (List[tuple]) – A list where each entry is a tuple containing the source neuron index, the target neuron index, the connection strength (weight), and the synaptic delay.
  • i_offset (ndarray) – Flattened array containing the biases of all neurons in the layer.

snntoolbox.simulation.utils.build_convolution(layer, delay, transpose_kernel=False)[source]

Build convolution layer.

Parameters:
  • layer (keras.layers.Conv2D) – Parsed model layer.
  • delay (float) – Synaptic delay.
  • transpose_kernel (bool) – Whether or not to convert kernels from Tensorflow to Theano format (correlation instead of convolution).
Returns:

  • connections (List[tuple]) – A list where each entry is a tuple containing the source neuron index, the target neuron index, the connection strength (weight), and the synaptic delay.
  • i_offset (ndarray) – Flattened array containing the biases of all neurons in the layer.

snntoolbox.simulation.utils.build_depthwise_convolution(layer, delay, transpose_kernel=False)[source]

Build convolution layer.

Parameters:
  • layer (keras.layers.DepthwiseConv2D) – Parsed model layer.
  • delay (float) – Synaptic delay.
  • transpose_kernel (bool) – Whether or not to convert kernels from Tensorflow to Theano format (correlation instead of convolution).
Returns:

  • connections (List[tuple]) – A list where each entry is a tuple containing the source neuron index, the target neuron index, the connection strength (weight), and the synaptic delay.
  • i_offset (ndarray) – Flattened array containing the biases of all neurons in the layer.

snntoolbox.simulation.utils.build_pooling(layer, delay)[source]

Build average pooling layer.

Parameters:
  • layer (keras.layers.Pool2D) – Parsed model layer.
  • delay (float) – Synaptic delay.
Returns:

connections – A list where each entry is a tuple containing the source neuron index, the target neuron index, the connection strength (weight), and the synaptic delay. The weight is given by \(\frac{1}{k_x k_y}\), where \(k_x, k_y\) are the dimensions of the pooling kernel.

Return type:

List[tuple]

snntoolbox.simulation.utils.spikecounts_to_rates(spikecounts_n_b_l_t)[source]

Convert spiketrains to spikerates.

The output will have the same shape as the input except for the last dimension, which is removed by replacing a sequence of spiketimes by a single rate value.

Parameters:spikecounts_n_b_l_t (list[tuple[np.array, str]]) –
Returns:spikerates_n_b_l
Return type:list[tuple[np.array, str]]
snntoolbox.simulation.utils.spiketrains_to_rates(spiketrains_n_b_l_t, duration, spike_code)[source]

Convert spiketrains to spikerates.

The output will have the same shape as the input except for the last dimension, which is removed by replacing a sequence of spiketimes by a single rate value.

Parameters:
  • spiketrains_n_b_l_t (list[tuple[np.array, str]]) –
  • duration (int) – Duration of simulation.
  • spike_code (str) – String specifying the spike encoding mechanism. For instance, with ‘ttfs’, the spike rates are computed using the time to first spike.
Returns:

spikerates_n_b_l

Return type:

list[tuple[np.array, str]]

snntoolbox.simulation.utils.get_sample_activity_from_batch(activity_batch, idx=0)[source]

Return layer activity for sample idx of an activity_batch.

snntoolbox.simulation.utils.get_spiking_outbound_layers(layer, config)[source]

Iterate until spiking outbound layers are found.

Parameters:
Returns:

List of outbound layers.

Return type:

list

snntoolbox.simulation.utils.get_layer_synaptic_operations(spiketrains_b_l, fanout)[source]

Return total number of synaptic operations in the layer for a batch of samples.

Parameters:
  • spiketrains_b_l (ndarray) – Batch of spiketrains of a layer. Shape: (batch_size, layer_shape)
  • fanout (Union[int, ndarray]) – Number of outgoing connections per neuron. Can be a single integer, or an array of the same shape as the layer, if the fanout varies from neuron to neuron (as is the case in convolution layers with stride > 1).
Returns:

layer_ops – The total number of operations in the layer for a batch of samples.

Return type:

int

snntoolbox.simulation.utils.get_ann_ops(num_neurons, num_neurons_with_bias, fanin)[source]

Compute number of operations performed by an ANN in one forward pass.

Parameters:
  • num_neurons (list[int]) – Number of neurons per layer, starting with input layer.
  • num_neurons_with_bias (list[int]) – Number of neurons with bias.
  • fanin (list[int]) – List of fan-in of neurons in Conv, Dense and Pool layers. Input and Pool layers have fan-in 0 so they are not counted.
Returns:

Number of operations.

Return type:

int

snntoolbox.simulation.utils.estimate_snn_ops(activations_n_b_l, fanouts_n, num_timesteps)[source]
snntoolbox.simulation.utils.is_spiking(layer, config)[source]

Test if layer is going to be converted to a layer that spikes.

Parameters:
Returns:

True if converted layer will have spiking neurons.

Return type:

bool

snntoolbox.simulation.utils.get_shape_from_label(label)[source]

Extract the output shape of a flattened pyNN layer from the layer name generated during parsing.

Parameters:label (str) – Layer name containing shape information after a ‘_’ separator.
Returns:The layer shape.
Return type:list

Example

>>> get_shape_from_label('02Conv2D_16x32x32')
[16, 32, 32]
snntoolbox.simulation.utils.get_weights(layer)[source]

Get weights of Keras layer, possibly applying sparsifying mask.

Parameters:layer (keras.layers.Layer) –
Returns:The layer weights and biases.
Return type:tuple
snntoolbox.simulation.utils.remove_name_counter(name_in)[source]

Tensorflow adds a counter to layer names, e.g. <name>/kernel:0 -> <name>_0/kernel:0. Need to remove this _0. The situation gets complicated because SNN toolbox assigns layer names that contain the layer shape, e.g. 00Conv2D_3x32x32. In addition, we may get another underscore in the parameter name, e.g. 00DepthwiseConv2D_3X32x32_0/depthwise_kernel:0.

snntoolbox.simulation.utils.convert_kernel(kernel)[source]

Converts a Numpy kernel matrix from Theano format to TensorFlow format.

Also works reciprocally, since the transformation is its own inverse.

This is used for converting legacy Theano-saved model files.

Parameters:kernel – Numpy array (3D, 4D or 5D).
Returns:The converted kernel.
Raises:ValueError – in case of invalid kernel shape or invalid data_format.

snntoolbox.simulation.plotting

Various functions to visualize connectivity, activity and accuracy of the network.

@author: rbodo

snntoolbox.simulation.plotting.output_graphs(plot_vars, config, path=None, idx=0, data_format=None)[source]

Wrapper function to display / save a number of plots.

Parameters:
  • plot_vars (dict) –

    Example items:

    • spiketrains_n_b_l_t: list[tuple[np.array, str]]
      Each entry in spiketrains_batch contains a tuple (spiketimes, label) for each layer of the network (for the first batch only, and excluding Flatten layers). spiketimes is an array where the last index contains the spike times of the specific neuron, and the first indices run over the number of neurons in the layer: (batch_size, n_chnls, n_rows, n_cols, duration) label is a string specifying both the layer type and the index, e.g. '03Dense'.
    • activations_n_b_l: list[tuple[np.array, str]]
      Activations of the ANN.
    • spikecounts_n_b_l: list[tuple[np.array, str]]
      Spikecounts of the SNN. Used to compute spikerates.
  • config (configparser.ConfigParser) – Settings.
  • path (Optional[str]) – If not None, specifies where to save the resulting image. Else, display plots without saving.
  • idx (int) – The index of the sample to display. Defaults to 0.
  • data_format (Optional[str]) – One of ‘channels_first’ or ‘channels_last’.
snntoolbox.simulation.plotting.plot_layer_summaries(plot_vars, config, path=None, data_format=None)[source]

Display or save a number of plots for a specific layer.

Parameters:
  • plot_vars (dict) –

    Example items:

    • spikerates: list[tuple[np.array, str]]
      Each entry in spikerates contains a tuple (rates, label) for each layer of the network (for the first batch only, and excluding Flatten layers).

      rates contains the average firing rates of all neurons in a layer. It has the same shape as the original layer, e.g. (n_features, n_rows, n_cols) for a convolution layer.

      label is a string specifying both the layer type and the index, e.g. '03Dense'.

    • activations: list[tuple[np.array, str]]
      Contains the activations of a net. Same structure as spikerates.
    • spiketrains: list[tuple[np.array, str]]
      Each entry in spiketrains contains a tuple (spiketimes, label) for each layer of the network (for the first batch only, and excluding Flatten layers).

      spiketimes is an array where the last index contains the spike times of the specific neuron, and the first indices run over the number of neurons in the layer: (n_chnls, n_rows, n_cols, duration)

      label is a string specifying both the layer type and the index, e.g. '03Dense'.

  • config (configparser.ConfigParser) – Settings.
  • path (Optional[str]) – If not None, specifies where to save the resulting image. Else, display plots without saving.
  • data_format (Optional[str]) – One of ‘channels_first’ or ‘channels_last’.
snntoolbox.simulation.plotting.plot_layer_activity(layer, title, path=None, limits=None, data_format=None)[source]

Visualize a layer by arranging the neurons in a line or on a 2D grid.

Can be used to show average firing rates of individual neurons in an SNN, or the activation function per layer in an ANN. The activity is encoded by color.

Parameters:
  • layer (tuple[np.array, str]) –

    (activity, label).

    activity is an array of the same shape as the original layer, containing e.g. the spikerates or activations of neurons in a layer.

    label is a string specifying both the layer type and the index, e.g. '3Dense'.

  • title (str) – Figure title.
  • path (Optional[str]) – If not None, specifies where to save the resulting image. Else, display plots without saving.
  • limits (Optional[tuple]) – If not None, the colormap of the resulting image is limited by this tuple.
  • data_format (Optional[str]) – One of ‘channels_first’ or ‘channels_last’.
snntoolbox.simulation.plotting.plot_activations(model, x_test, path, data_format=None)[source]

Plot activations of a network.

Parameters:
  • model (keras.models.Model) – Keras model.
  • x_test (ndarray) – The samples.
  • path (str) – Where to save plot.
  • data_format (Optional[str]) – One of ‘channels_first’ or ‘channels_last’.
snntoolbox.simulation.plotting.plot_activations_minus_rates(activations, rates, label, path=None, data_format=None)[source]

Plot spikerates minus activations for a specific layer.

Spikerates and activations are each normalized before subtraction. The neurons in the layer are arranged in a line or on a 2D grid, depending on layer type.

Activity is encoded by color.

Parameters:
  • activations (ndarray) – The activations of a layer. The shape is that of the original layer, e.g. (32, 28, 28) for 32 feature maps of size 28x28.
  • rates (ndarray) – The spikerates of a layer. The shape is that of the original layer, e.g. (32, 28, 28) for 32 feature maps of size 28x28.
  • label (str) – Layer label.
  • path (Optional[str]) – If not None, specifies where to save the resulting image. Else, display plots without saving.
  • data_format (Optional[str]) – One of ‘channels_first’ or ‘channels_last’.
snntoolbox.simulation.plotting.plot_layer_correlation(rates, activations, title, config, path=None, same_xylim=True)[source]

Plot correlation between spikerates and activations of a specific layer, as 2D-dot-plot.

Parameters:
  • rates (np.array) – The spikerates of a layer, flattened to 1D.
  • activations (Union[ndarray, Iterable]) – The activations of a layer, flattened to 1D.
  • title (str) – Plot title.
  • config (configparser.ConfigParser) – Settings.
  • path (Optional[str]) – If not None, specifies where to save the resulting image. Else, display plots without saving.
  • same_xylim (Optional[bool]) – Whether to use the same axis limit on the rates and activations. If True, the maximum is chosen. Default: True.
snntoolbox.simulation.plotting.plot_correlations(a, b, path=None, filename=None)[source]
snntoolbox.simulation.plotting.plot_network_correlations(spikerates, layer_activations)[source]

Plot the correlation between SNN spiketrains and ANN activations.

For each layer, the method draws a scatter plot, showing the correlation between the average firing rate of neurons in the SNN layer and the activation of the corresponding neurons in the ANN layer.

Parameters:
  • spikerates (list of tuples (spikerate, label).) –

    spikerate is a 1D array containing the mean firing rates of the neurons in a specific layer.

    label is a string specifying both the layer type and the index, e.g. '3Dense'.

  • layer_activations (list of tuples (activations, label)) –

    Each entry represents a layer in the ANN for which an activation can be calculated (e.g. Dense, Conv2D).

    activations is an array of the same dimension as the corresponding layer, containing the activations of Dense or Convolution layers.

    label is a string specifying the layer type, e.g. 'Dense'.

snntoolbox.simulation.plotting.plot_pearson_coefficients(spikerates_batch, activations_batch, config, path=None)[source]

Plot the Pearson correlation coefficients for each layer, averaged over one mini batch.

Parameters:
  • spikerates_batch (list[tuple[np.array, str]]) –

    Each entry in spikerates_batch contains a tuple (spikerates, label) for each layer of the network (for the first batch only, and excluding Flatten layers).

    spikerates contains the average firing rates of all neurons in a layer. It has the same shape as the original layer, e.g. (batch_size, n_features, n_rows, n_cols) for a convolution layer.

    label is a string specifying both the layer type and the index, e.g. '03Dense'.

  • activations_batch (list[tuple[np.array, str]]) – Contains the activations of a net. Same structure as spikerates_batch.
  • config (configparser.ConfigParser) – Settings.
  • path (Optional[str]) – Where to save the output.
snntoolbox.simulation.plotting.plot_hist(h, title=None, layer_label=None, path=None, scale_fac=None)[source]

Plot a histogram over two datasets.

Parameters:
  • h (dict) – Dictionary of datasets to plot in histogram.
  • title (string, optional) – Title of histogram.
  • layer_label (string, optional) – Label of layer from which data was taken.
  • path (string, optional) – If not None, specifies where to save the resulting image. Else, display plots without saving.
  • scale_fac (float, optional) – The value with which parameters are normalized (maximum of activations or parameter value of a layer). If given, will be insterted into plot title.
snntoolbox.simulation.plotting.plot_activ_hist(h, title=None, layer_label=None, path=None, scale_fac=None)[source]

Plot a histogram over all activities of a network.

Parameters:
  • h (dict) – Dictionary of datasets to plot in histogram.
  • title (string, optional) – Title of histogram.
  • layer_label (string, optional) – Label of layer from which data was taken.
  • path (string, optional) – If not None, specifies where to save the resulting image. Else, display plots without saving.
  • scale_fac (float, optional) – The value with which parameters are normalized (maximum of activations or parameter value of a layer). If given, will be insterted into plot title.
snntoolbox.simulation.plotting.plot_max_activ_hist(h, title=None, layer_label=None, path=None, scale_fac=None)[source]

Plot a histogram over the maximum activations.

Parameters:
  • h (dict) – Dictionary of datasets to plot in histogram.
  • title (string, optional) – Title of histogram.
  • layer_label (string, optional) – Label of layer from which data was taken.
  • path (string, optional) – If not None, specifies where to save the resulting image. Else, display plots without saving.
  • scale_fac (float, optional) – The value with which parameters are normalized (maximum of activations or parameter value of a layer). If given, will be insterted into plot title.
snntoolbox.simulation.plotting.plot_hist_combined(data, path=None)[source]

Plot a histogram over several datasets.

Parameters:
  • data (dict) – Dictionary of datasets to plot in histogram.
  • path (string, optional) – If not None, specifies where to save the resulting image. Else, display plots without saving.
snntoolbox.simulation.plotting.plot_param_sweep(results, n, params, param_name, param_logscale)[source]

Plot accuracy versus parameter.

Parameters:
  • results (list[float]) – The accuracy or loss for a number of experiments, each of which used different parameters.
  • n (int) – The number of test samples used for each experiment.
  • params (list[float]) – The parameter values that changed during each experiment.
  • param_name (str) – The name of the parameter that varied.
  • param_logscale (bool) – Whether to plot the parameter axis in log-scale.
snntoolbox.simulation.plotting.plot_spiketrains(layer, dt, path=None, data_format=None)[source]

Plot which neuron fired at what time during the simulation.

Parameters:
  • layer (tuple[np.array, str]) –

    (spiketimes, label).

    spiketimes is a 2D array where the first index runs over the number of neurons in the layer, and the second index contains the spike times of the specific neuron.

    label is a string specifying both the layer type and the index, e.g. '3Dense'.

  • dt (float) – Time resolution of simulation.
  • path (Optional[str]) – If not None, specifies where to save the resulting image. Else, display plots without saving.
  • data_format (Optional[str]) – One of ‘channels_first’ or ‘channels_last’.
snntoolbox.simulation.plotting.plot_potential(times, layer, config, v_thresh=None, show_legend=False, path=None)[source]

Plot the membrane potential of a layer.

Parameters:
  • times (np.array) – The time values where the potential was sampled.
  • layer (tuple[np.array, str]) –

    (vmem, label).

    vmem is a 2D array where the first index runs over the number of neurons in the layer, and the second index contains the membrane potential of the specific neuron.

    label is a string specifying both the layer type and the index, e.g. '3Dense'.

  • config (configparser.ConfigParser) – Settings.
  • v_thresh (float) – Threshold.
  • show_legend (bool) – If True, shows the legend indicating the neuron indices and lines like v_thresh, v_rest, v_reset. Recommended only for layers with few neurons.
  • path (Optional[str]) – If not None, specifies where to save the resulting image. Else, display plots without saving.
snntoolbox.simulation.plotting.plot_confusion_matrix(y_test, y_pred, path=None, class_labels=None)[source]
Parameters:
  • y_test (list) –
  • y_pred (list) –
  • path (Optional[str]) – Where to save the output.
  • class_labels (Optional[list]) – List of class labels.
snntoolbox.simulation.plotting.plot_error_vs_time(top1err_d_t, top5err_d_t, duration, dt, top1err_ann=None, top5err_ann=None, path=None)[source]

Plot classification error over time.

Parameters:
  • top1err_d_t (np.array) – Batch of top-1 errors over time. Shape: (num_samples, duration). Data type: boolean (correct / incorrect classification).
  • top5err_d_t (np.array) – Batch of top-5 errors over time. Shape: (num_samples, duration). Data type: boolean (correct / incorrect classification).
  • duration (int) – Simulation duration.
  • dt (float) – Simulation time resolution.
  • top1err_ann (Optional[float]) – The top-1 error of the ANN.
  • top5err_ann (Optional[float]) – The top-5 error of the ANN.
  • path (Optional[str]) – Where to save the output.
snntoolbox.simulation.plotting.plot_ops_vs_time(operations_b_t, duration, dt, path=None)[source]

Plot total number of operations over time.

Parameters:
  • operations_b_t (ndarray) – Number of operations. Shape: (batch_size, num_timesteps)
  • duration (int) – Simulation duration.
  • dt (float) – Simulation time resolution.
  • path (Optional[str]) – Where to save the output.
snntoolbox.simulation.plotting.plot_spikecount_vs_time(spiketrains_n_b_l_t, duration, dt, path=None)[source]

Plot total spikenumber over time.

Parameters:
  • spiketrains_n_b_l_t
  • duration (int) – Simulation duration.
  • dt (float) – Simulation time resolution.
  • path (Optional[str]) – Where to save the output.
snntoolbox.simulation.plotting.plot_input_image(x, label, path=None, data_format=None, filename=None)[source]

Show an input image.

Parameters:
  • x (ndarray) – The sample to plot.
  • label (int) – Class label (index) of sample.
  • path (Optional[str]) – Where to save the image.
  • data_format (Optional[str]) – One of ‘channels_first’ or ‘channels_last’.
  • filename (Optional[str]) – Name of file to save.
snntoolbox.simulation.plotting.plot_history(h)[source]

Plot the training and validation loss and accuracy at each epoch.

Parameters:h (Keras history object) – Contains the training and validation loss and accuracy at each epoch during training.
snntoolbox.simulation.plotting.plot_probe(probe, path, filename)[source]
snntoolbox.simulation.plotting.plot_weight_distribution(path, model)[source]
snntoolbox.simulation.plotting.plot_execution_time_probe(path, probe)[source]
snntoolbox.simulation.plotting.plot_energy_probe(path, probe)[source]
snntoolbox.simulation.plotting.plot_power_probe(path, probe)[source]
snntoolbox.simulation.plotting.plot_parameter_histogram(path, filename, weights, biases, bins=32)[source]

snntoolbox.simulation.backends

inisim

temporal_mean_rate_tensorflow

INI temporal mean rate simulator with Tensorflow backend.

This module defines the layer objects used to create a spiking neural network for our built-in INI simulator INI_temporal_mean_rate_target_sim.

The coding scheme underlying this conversion is that the analog activation value is represented by the average over number of spikes that occur during the simulation duration.

@author: rbodo

class snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.SpikeLayer(**kwargs)[source]

Bases: keras.engine.base_layer.Layer

Base class for layer with spiking neurons.

reset(sample_idx)[source]

Reset layer variables.

class_name

Get class name.

update_neurons()[source]

Update neurons according to activation function.

update_payload(residuals, spikes)[source]

Update payloads.

Uses the residual of the membrane potential after spike.

linear_activation(mem)[source]

Linear activation.

binary_sigmoid_activation(mem)[source]

Binary sigmoid activation.

binary_tanh_activation(mem)[source]

Binary tanh activation.

softmax_activation(mem)[source]

Softmax activation.

quantized_activation(mem, m, f)[source]

Activation with precision reduced to fixed point format Qm.f.

get_new_mem()[source]

Add input to membrane potential.

set_reset_mem(mem, spikes)[source]

Reset membrane potential mem array where spikes array is nonzero.

get_new_thresh()[source]

Get new threshhold.

get_time()[source]

Get simulation time variable.

Returns:time – Current simulation time.
Return type:float
set_time(time)[source]

Set simulation time variable.

Parameters:time (float) – Current simulation time.
init_membrane_potential(output_shape=None, mode='zero')[source]

Initialize membrane potential.

Helpful to avoid transient response in the beginning of the simulation. Not needed when reset between frames is turned off, e.g. with a video data set.

Parameters:
  • output_shape (Optional[tuple]) – Output shape
  • mode (str) –

    Initialization mode.

    • 'uniform': Random numbers from uniform distribution in [-thr, thr].
    • 'bias': Negative bias.
    • 'zero': Zero (default).
Returns:

init_mem – A tensor of self.output_shape (same as layer).

Return type:

ndarray

reset_spikevars[source]

Reset variables present in spiking layers. Can be turned off for instance when a video sequence is tested.

init_neurons[source]

Init layer neurons.

get_layer_idx()[source]

Get index of layer.

get_clamp_idx()[source]

Get time step when to stop clamping membrane potential.

Returns:Time step when to stop clamping.
Return type:int
update_avg_variance(spikes)[source]

Keep a running average of the spike-rates and the their variance.

Parameters:spikes – Output spikes.
update_b[source]

Get a new value for the bias, relaxing it over time to the true value.

snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.add_payloads(prev_layer, input_spikes)[source]

Get payloads from previous layer.

snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.spike_call(call)[source]
snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.get_isi_from_impulse(impulse, epsilon)[source]
class snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.SpikeConcatenate(axis, **kwargs)[source]

Bases: keras.layers.merging.concatenate.Concatenate

Spike merge layer

static get_time()[source]
static reset(sample_idx)[source]

Reset layer variables.

class_name

Get class name.

class snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.SpikeFlatten(**kwargs)[source]

Bases: keras.layers.reshaping.flatten.Flatten

Spike flatten layer.

call(x, mask=None)[source]

This is where the layer’s logic lives.

The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,

in __init__(), or in the build() method that is

called automatically before call() executes for the first time.

Parameters:
  • inputs

    Input tensor, or dict/list/tuple of input tensors. The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero

    arguments, and inputs cannot be provided via the default value of a keyword argument.
    • NumPy array or Python scalar values in inputs get cast as tensors.
    • Keras mask metadata is only collected from inputs.
    • Layers are built (build(input_shape) method) using shape info from inputs only.
    • input_spec compatibility is only checked against inputs.
    • Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
    • The SavedModel input specification is generated using inputs only.
    • Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
  • *args – Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above.
  • **kwargs

    Additional keyword arguments. May contain tensors, although this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating

    whether the call is meant for training or inference.
    • mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
Returns:

A tensor or list/tuple of tensors.

static get_time()[source]
static reset(sample_idx)[source]

Reset layer variables.

class_name

Get class name.

class snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.SpikeZeroPadding2D(**kwargs)[source]

Bases: keras.layers.reshaping.zero_padding2d.ZeroPadding2D

Spike ZeroPadding2D layer.

call(x, mask=None)[source]

This is where the layer’s logic lives.

The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,

in __init__(), or in the build() method that is

called automatically before call() executes for the first time.

Parameters:
  • inputs

    Input tensor, or dict/list/tuple of input tensors. The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero

    arguments, and inputs cannot be provided via the default value of a keyword argument.
    • NumPy array or Python scalar values in inputs get cast as tensors.
    • Keras mask metadata is only collected from inputs.
    • Layers are built (build(input_shape) method) using shape info from inputs only.
    • input_spec compatibility is only checked against inputs.
    • Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
    • The SavedModel input specification is generated using inputs only.
    • Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
  • *args – Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above.
  • **kwargs

    Additional keyword arguments. May contain tensors, although this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating

    whether the call is meant for training or inference.
    • mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
Returns:

A tensor or list/tuple of tensors.

static get_time()[source]
static reset(sample_idx)[source]

Reset layer variables.

class_name

Get class name.

class snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.SpikeReshape(**kwargs)[source]

Bases: keras.layers.reshaping.reshape.Reshape

Spike Reshape layer.

call(x, mask=None)[source]

This is where the layer’s logic lives.

The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,

in __init__(), or in the build() method that is

called automatically before call() executes for the first time.

Parameters:
  • inputs

    Input tensor, or dict/list/tuple of input tensors. The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero

    arguments, and inputs cannot be provided via the default value of a keyword argument.
    • NumPy array or Python scalar values in inputs get cast as tensors.
    • Keras mask metadata is only collected from inputs.
    • Layers are built (build(input_shape) method) using shape info from inputs only.
    • input_spec compatibility is only checked against inputs.
    • Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
    • The SavedModel input specification is generated using inputs only.
    • Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
  • *args – Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above.
  • **kwargs

    Additional keyword arguments. May contain tensors, although this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating

    whether the call is meant for training or inference.
    • mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
Returns:

A tensor or list/tuple of tensors.

static get_time()[source]
static reset(sample_idx)[source]

Reset layer variables.

class_name

Get class name.

class snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.SpikeDense(units, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, **kwargs)[source]

Bases: keras.layers.core.dense.Dense, snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.SpikeLayer

Spike Dense layer.

build(input_shape)[source]

Creates the layer neurons and connections.

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call[source]
class snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.SpikeConv1D(filters, kernel_size, strides=1, padding='valid', data_format='channels_last', dilation_rate=1, groups=1, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, **kwargs)[source]

Bases: keras.layers.convolutional.conv1d.Conv1D, snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.SpikeLayer

Spike 1D Convolution.

build(input_shape)[source]

Creates the layer weights. Must be implemented on all layers that have weights.

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call[source]
class snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.SpikeConv2D(filters, kernel_size, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1), groups=1, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, **kwargs)[source]

Bases: keras.layers.convolutional.conv2d.Conv2D, snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.SpikeLayer

Spike 2D Convolution.

build(input_shape)[source]

Creates the layer weights. Must be implemented on all layers that have weights.

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call[source]
class snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.SpikeDepthwiseConv2D(kernel_size, strides=(1, 1), padding='valid', depth_multiplier=1, data_format=None, dilation_rate=(1, 1), activation=None, use_bias=True, depthwise_initializer='glorot_uniform', bias_initializer='zeros', depthwise_regularizer=None, bias_regularizer=None, activity_regularizer=None, depthwise_constraint=None, bias_constraint=None, **kwargs)[source]

Bases: keras.layers.convolutional.depthwise_conv2d.DepthwiseConv2D, snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.SpikeLayer

Spike 2D DepthwiseConvolution.

build(input_shape)[source]

Creates the layer weights. Must be implemented on all layers that have weights.

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call[source]
class snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.SpikeConv2DTranspose(filters, kernel_size, strides=(1, 1), padding='valid', output_padding=None, data_format=None, dilation_rate=(1, 1), activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, **kwargs)[source]

Bases: keras.layers.convolutional.conv2d_transpose.Conv2DTranspose, snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.SpikeLayer

Spike 2D transpose convolution.

build(input_shape)[source]

Creates the layer weights. Must be implemented on all layers that have weights.

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call[source]
class snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.SpikeUpSampling2D(size=(2, 2), data_format=None, interpolation='nearest', **kwargs)[source]

Bases: keras.layers.reshaping.up_sampling2d.UpSampling2D, snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.SpikeLayer

Spike upsampling layer.

build(input_shape)[source]

Creates the layer weights. Must be implemented on all layers that have weights.

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call[source]
class snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.SpikeAveragePooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None, **kwargs)[source]

Bases: keras.layers.pooling.average_pooling2d.AveragePooling2D, snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.SpikeLayer

Spike Average Pooling.

build(input_shape)[source]

Creates the layer weights. Must be implemented on all layers that have weights.

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call[source]
class snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.SpikeMaxPooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None, **kwargs)[source]

Bases: keras.layers.pooling.max_pooling2d.MaxPooling2D, snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow.SpikeLayer

Spike Max Pooling.

build(input_shape)[source]

Creates the layer neurons and connections..

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call[source]

temporal_mean_rate_theano

temporal_pattern

INI temporal pattern simulator backend.

This module defines the layer objects used to create a spiking neural network for our built-in INI simulator INI_temporal_pattern_target_sim.

The coding scheme underlying this conversion is that the analog activation value is transformed into a binary representation of spikes.

This simulator works only with Keras backend set to Tensorflow.

@author: rbodo

class snntoolbox.simulation.backends.inisim.temporal_pattern.SpikeLayer(**kwargs)[source]

Bases: keras.engine.base_layer.Layer

Base class for layer with spiking neurons.

class_name

Get class name.

get_time()[source]
static reset(sample_idx)[source]

Reset layer variables.

init_neurons(input_shape)[source]

Init layer neurons.

spike_call[source]
to_binary[source]

Transform an array of floats into binary representation.

Parameters:x (tf.Tensor) – Input tensor containing float values. The first dimension has to be of length 1.
Returns:x_binary – Output boolean array. The first dimension of x is expanded to length num_bits. The binary representation of each value in x is distributed across the first dimension of x_binary.
Return type:tf.Variable
class snntoolbox.simulation.backends.inisim.temporal_pattern.SpikeConcatenate(axis, **kwargs)[source]

Bases: keras.layers.merging.concatenate.Concatenate

Spike merge layer

static get_time()[source]
static reset(sample_idx)[source]

Reset layer variables.

class_name

Get class name.

class snntoolbox.simulation.backends.inisim.temporal_pattern.SpikeFlatten(**kwargs)[source]

Bases: keras.layers.reshaping.flatten.Flatten

Spike flatten layer.

static get_time()[source]
static reset(sample_idx)[source]

Reset layer variables.

class_name

Get class name.

class snntoolbox.simulation.backends.inisim.temporal_pattern.SpikeReshape(target_shape, **kwargs)[source]

Bases: keras.layers.reshaping.reshape.Reshape

Spike reshape layer.

static get_time()[source]
static reset(sample_idx)[source]

Reset layer variables.

class snntoolbox.simulation.backends.inisim.temporal_pattern.SpikeZeroPadding2D(*args, **kwargs)[source]

Bases: keras.layers.reshaping.zero_padding2d.ZeroPadding2D

Spike padding layer.

static get_time()[source]
static reset(sample_idx)[source]

Reset layer variables.

class snntoolbox.simulation.backends.inisim.temporal_pattern.SpikeDense(units, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, **kwargs)[source]

Bases: keras.layers.core.dense.Dense, snntoolbox.simulation.backends.inisim.temporal_pattern.SpikeLayer

Spike Dense layer.

build(input_shape)[source]

Creates the layer neurons and connections.

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call(x, **kwargs)[source]

This is where the layer’s logic lives.

The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,

in __init__(), or in the build() method that is

called automatically before call() executes for the first time.

Parameters:
  • inputs

    Input tensor, or dict/list/tuple of input tensors. The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero

    arguments, and inputs cannot be provided via the default value of a keyword argument.
    • NumPy array or Python scalar values in inputs get cast as tensors.
    • Keras mask metadata is only collected from inputs.
    • Layers are built (build(input_shape) method) using shape info from inputs only.
    • input_spec compatibility is only checked against inputs.
    • Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
    • The SavedModel input specification is generated using inputs only.
    • Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
  • *args – Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above.
  • **kwargs

    Additional keyword arguments. May contain tensors, although this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating

    whether the call is meant for training or inference.
    • mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
Returns:

A tensor or list/tuple of tensors.

class snntoolbox.simulation.backends.inisim.temporal_pattern.SpikeConv2D(filters, kernel_size, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1), groups=1, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, **kwargs)[source]

Bases: keras.layers.convolutional.conv2d.Conv2D, snntoolbox.simulation.backends.inisim.temporal_pattern.SpikeLayer

Spike 2D Convolution.

build(input_shape)[source]

Creates the layer weights. Must be implemented on all layers that have weights.

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call(x, mask=None)[source]

This is where the layer’s logic lives.

The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,

in __init__(), or in the build() method that is

called automatically before call() executes for the first time.

Parameters:
  • inputs

    Input tensor, or dict/list/tuple of input tensors. The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero

    arguments, and inputs cannot be provided via the default value of a keyword argument.
    • NumPy array or Python scalar values in inputs get cast as tensors.
    • Keras mask metadata is only collected from inputs.
    • Layers are built (build(input_shape) method) using shape info from inputs only.
    • input_spec compatibility is only checked against inputs.
    • Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
    • The SavedModel input specification is generated using inputs only.
    • Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
  • *args – Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above.
  • **kwargs

    Additional keyword arguments. May contain tensors, although this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating

    whether the call is meant for training or inference.
    • mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
Returns:

A tensor or list/tuple of tensors.

class snntoolbox.simulation.backends.inisim.temporal_pattern.SpikeDepthwiseConv2D(kernel_size, strides=(1, 1), padding='valid', depth_multiplier=1, data_format=None, dilation_rate=(1, 1), activation=None, use_bias=True, depthwise_initializer='glorot_uniform', bias_initializer='zeros', depthwise_regularizer=None, bias_regularizer=None, activity_regularizer=None, depthwise_constraint=None, bias_constraint=None, **kwargs)[source]

Bases: keras.layers.convolutional.depthwise_conv2d.DepthwiseConv2D, snntoolbox.simulation.backends.inisim.temporal_pattern.SpikeLayer

Spike 2D depthwise-separable Convolution.

build(input_shape)[source]

Creates the layer weights. Must be implemented on all layers that have weights.

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call(x, mask=None)[source]

This is where the layer’s logic lives.

The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,

in __init__(), or in the build() method that is

called automatically before call() executes for the first time.

Parameters:
  • inputs

    Input tensor, or dict/list/tuple of input tensors. The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero

    arguments, and inputs cannot be provided via the default value of a keyword argument.
    • NumPy array or Python scalar values in inputs get cast as tensors.
    • Keras mask metadata is only collected from inputs.
    • Layers are built (build(input_shape) method) using shape info from inputs only.
    • input_spec compatibility is only checked against inputs.
    • Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
    • The SavedModel input specification is generated using inputs only.
    • Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
  • *args – Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above.
  • **kwargs

    Additional keyword arguments. May contain tensors, although this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating

    whether the call is meant for training or inference.
    • mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
Returns:

A tensor or list/tuple of tensors.

class snntoolbox.simulation.backends.inisim.temporal_pattern.SpikeAveragePooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None, **kwargs)[source]

Bases: keras.layers.pooling.average_pooling2d.AveragePooling2D, snntoolbox.simulation.backends.inisim.temporal_pattern.SpikeLayer

Spike Average Pooling layer.

build(input_shape)[source]

Creates the layer weights. Must be implemented on all layers that have weights.

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call(x, mask=None)[source]

This is where the layer’s logic lives.

The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,

in __init__(), or in the build() method that is

called automatically before call() executes for the first time.

Parameters:
  • inputs

    Input tensor, or dict/list/tuple of input tensors. The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero

    arguments, and inputs cannot be provided via the default value of a keyword argument.
    • NumPy array or Python scalar values in inputs get cast as tensors.
    • Keras mask metadata is only collected from inputs.
    • Layers are built (build(input_shape) method) using shape info from inputs only.
    • input_spec compatibility is only checked against inputs.
    • Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
    • The SavedModel input specification is generated using inputs only.
    • Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
  • *args – Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above.
  • **kwargs

    Additional keyword arguments. May contain tensors, although this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating

    whether the call is meant for training or inference.
    • mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
Returns:

A tensor or list/tuple of tensors.

class snntoolbox.simulation.backends.inisim.temporal_pattern.SpikeMaxPooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None, **kwargs)[source]

Bases: keras.layers.pooling.max_pooling2d.MaxPooling2D, snntoolbox.simulation.backends.inisim.temporal_pattern.SpikeLayer

Spike Max Pooling.

build(input_shape)[source]

Creates the layer neurons and connections..

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call(x, mask=None)[source]

This is where the layer’s logic lives.

The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,

in __init__(), or in the build() method that is

called automatically before call() executes for the first time.

Parameters:
  • inputs

    Input tensor, or dict/list/tuple of input tensors. The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero

    arguments, and inputs cannot be provided via the default value of a keyword argument.
    • NumPy array or Python scalar values in inputs get cast as tensors.
    • Keras mask metadata is only collected from inputs.
    • Layers are built (build(input_shape) method) using shape info from inputs only.
    • input_spec compatibility is only checked against inputs.
    • Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
    • The SavedModel input specification is generated using inputs only.
    • Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
  • *args – Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above.
  • **kwargs

    Additional keyword arguments. May contain tensors, although this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating

    whether the call is meant for training or inference.
    • mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
Returns:

A tensor or list/tuple of tensors.

ttfs

INI time-to-first-spike simulator backend.

This module defines the layer objects used to create a spiking neural network for our built-in INI simulator INI_ttfs_target_sim.

The coding scheme underlying this conversion is that the instantaneous firing rate is given by the inverse time-to-first-spike.

This simulator works only with Keras backend set to Tensorflow.

@author: rbodo

class snntoolbox.simulation.backends.inisim.ttfs.SpikeLayer(**kwargs)[source]

Bases: keras.engine.base_layer.Layer

Base class for layer with spiking neurons.

reset(sample_idx)[source]

Reset layer variables.

class_name

Get class name.

update_neurons()[source]

Update neurons according to activation function.

linear_activation(mem)[source]

Linear activation.

softmax_activation(mem)[source]

Softmax activation.

get_new_mem()[source]

Add input to membrane potential.

set_reset_mem(mem, spikes)[source]

Reset membrane potential mem array where spikes array is nonzero.

get_psp(output_spikes)[source]
get_time()[source]

Get simulation time variable.

Returns:time – Current simulation time.
Return type:float
set_time(time)[source]

Set simulation time variable.

Parameters:time (float) – Current simulation time.
init_membrane_potential(output_shape=None, mode='zero')[source]

Initialize membrane potential.

Helpful to avoid transient response in the beginning of the simulation. Not needed when reset between frames is turned off, e.g. with a video data set.

Parameters:
  • output_shape (Optional[tuple]) – Output shape
  • mode (str) –

    Initialization mode.

    • 'uniform': Random numbers from uniform distribution in [-thr, thr].
    • 'bias': Negative bias.
    • 'zero': Zero (default).
Returns:

init_mem – A tensor of self.output_shape (same as layer).

Return type:

ndarray

reset_spikevars[source]

Reset variables present in spiking layers. Can be turned off for instance when a video sequence is tested.

init_neurons[source]

Init layer neurons.

get_layer_idx()[source]

Get index of layer.

snntoolbox.simulation.backends.inisim.ttfs.spike_call(call)[source]
class snntoolbox.simulation.backends.inisim.ttfs.SpikeConcatenate(axis, **kwargs)[source]

Bases: keras.layers.merging.concatenate.Concatenate

Spike merge layer

static get_time()[source]
static reset(sample_idx)[source]

Reset layer variables.

class_name

Get class name.

class snntoolbox.simulation.backends.inisim.ttfs.SpikeZeroPadding2D(*args, **kwargs)[source]

Bases: keras.layers.reshaping.zero_padding2d.ZeroPadding2D

Spike padding layer

static get_time()[source]
static reset(sample_idx)[source]

Reset layer variables.

class_name

Get class name.

class snntoolbox.simulation.backends.inisim.ttfs.SpikeReshape(*args, **kwargs)[source]

Bases: keras.layers.reshaping.reshape.Reshape

Spike reshape layer

static get_time()[source]
static reset(sample_idx)[source]

Reset layer variables.

class_name

Get class name.

class snntoolbox.simulation.backends.inisim.ttfs.SpikeFlatten(**kwargs)[source]

Bases: keras.layers.reshaping.flatten.Flatten

Spike flatten layer.

static get_time()[source]
reset(sample_idx)[source]

Reset layer variables.

class_name

Get class name.

class snntoolbox.simulation.backends.inisim.ttfs.SpikeDense(units, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, **kwargs)[source]

Bases: keras.layers.core.dense.Dense, snntoolbox.simulation.backends.inisim.ttfs.SpikeLayer

Spike Dense layer.

build(input_shape)[source]

Creates the layer neurons and connections.

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call[source]
class snntoolbox.simulation.backends.inisim.ttfs.SpikeConv2D(filters, kernel_size, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1), groups=1, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, **kwargs)[source]

Bases: keras.layers.convolutional.conv2d.Conv2D, snntoolbox.simulation.backends.inisim.ttfs.SpikeLayer

Spike 2D Convolution.

build(input_shape)[source]

Creates the layer weights. Must be implemented on all layers that have weights.

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call[source]
class snntoolbox.simulation.backends.inisim.ttfs.SpikeDepthwiseConv2D(kernel_size, strides=(1, 1), padding='valid', depth_multiplier=1, data_format=None, dilation_rate=(1, 1), activation=None, use_bias=True, depthwise_initializer='glorot_uniform', bias_initializer='zeros', depthwise_regularizer=None, bias_regularizer=None, activity_regularizer=None, depthwise_constraint=None, bias_constraint=None, **kwargs)[source]

Bases: keras.layers.convolutional.depthwise_conv2d.DepthwiseConv2D, snntoolbox.simulation.backends.inisim.ttfs.SpikeLayer

Spike 2D depthwise-separable convolution.

build(input_shape)[source]

Creates the layer weights. Must be implemented on all layers that have weights.

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call[source]
class snntoolbox.simulation.backends.inisim.ttfs.SpikeAveragePooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None, **kwargs)[source]

Bases: keras.layers.pooling.average_pooling2d.AveragePooling2D, snntoolbox.simulation.backends.inisim.ttfs.SpikeLayer

Average Pooling.

build(input_shape)[source]

Creates the layer weights. Must be implemented on all layers that have weights.

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call[source]
class snntoolbox.simulation.backends.inisim.ttfs.SpikeMaxPooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None, **kwargs)[source]

Bases: keras.layers.pooling.max_pooling2d.MaxPooling2D, snntoolbox.simulation.backends.inisim.ttfs.SpikeLayer

Spiking Max Pooling.

build(input_shape)[source]

Creates the layer neurons and connections..

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call(x, mask=None)[source]

Layer functionality.

ttfs_dyn_thresh

INI time-to-first-spike simulator backend with dynamic threshold.

This module defines the layer objects used to create a spiking neural network for our built-in INI simulator INI_ttfs_dyn_thresh_target_sim.

The coding scheme underlying this conversion is that the instantaneous firing rate is given by the inverse time-to-first-spike. In contrast to INI_ttfs_target_sim, this one features a threshold that adapts dynamically to the amount of input a neuron has received.

This simulator works only with Keras backend set to Tensorflow.

@author: rbodo

class snntoolbox.simulation.backends.inisim.ttfs_dyn_thresh.SpikeLayer(**kwargs)[source]

Bases: keras.engine.base_layer.Layer

Base class for layer with spiking neurons.

reset(sample_idx)[source]

Reset layer variables.

class_name

Get class name.

update_neurons()[source]

Update neurons according to activation function.

linear_activation(mem)[source]

Linear activation.

static softmax_activation(mem)[source]

Softmax activation.

get_new_mem()[source]

Add input to membrane potential.

set_reset_mem(mem, spikes)[source]

Reset membrane potential mem array where spikes array is nonzero.

get_psp(output_spikes)[source]
get_time()[source]

Get simulation time variable.

Returns:time – Current simulation time.
Return type:float
set_time(time)[source]

Set simulation time variable.

Parameters:time (float) – Current simulation time.
init_membrane_potential(output_shape=None, mode='zero')[source]

Initialize membrane potential.

Helpful to avoid transient response in the beginning of the simulation. Not needed when reset between frames is turned off, e.g. with a video data set.

Parameters:
  • output_shape (Optional[tuple]) – Output shape
  • mode (str) –

    Initialization mode.

    • 'uniform': Random numbers from uniform distribution in [-thr, thr].
    • 'bias': Negative bias.
    • 'zero': Zero (default).
Returns:

init_mem – A tensor of self.output_shape (same as layer).

Return type:

ndarray

reset_spikevars(sample_idx)[source]

Reset variables present in spiking layers. Can be turned off for instance when a video sequence is tested.

init_neurons(input_shape)[source]

Init layer neurons.

get_layer_idx()[source]

Get index of layer.

snntoolbox.simulation.backends.inisim.ttfs_dyn_thresh.spike_call(call)[source]
class snntoolbox.simulation.backends.inisim.ttfs_dyn_thresh.SpikeFlatten(**kwargs)[source]

Bases: keras.layers.reshaping.flatten.Flatten

Spike flatten layer.

call(x, mask=None)[source]

This is where the layer’s logic lives.

The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,

in __init__(), or in the build() method that is

called automatically before call() executes for the first time.

Parameters:
  • inputs

    Input tensor, or dict/list/tuple of input tensors. The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero

    arguments, and inputs cannot be provided via the default value of a keyword argument.
    • NumPy array or Python scalar values in inputs get cast as tensors.
    • Keras mask metadata is only collected from inputs.
    • Layers are built (build(input_shape) method) using shape info from inputs only.
    • input_spec compatibility is only checked against inputs.
    • Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
    • The SavedModel input specification is generated using inputs only.
    • Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
  • *args – Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above.
  • **kwargs

    Additional keyword arguments. May contain tensors, although this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating

    whether the call is meant for training or inference.
    • mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
Returns:

A tensor or list/tuple of tensors.

static get_time()[source]
reset(sample_idx)[source]

Reset layer variables.

class_name

Get class name.

class snntoolbox.simulation.backends.inisim.ttfs_dyn_thresh.SpikeDense(units, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, **kwargs)[source]

Bases: keras.layers.core.dense.Dense, snntoolbox.simulation.backends.inisim.ttfs_dyn_thresh.SpikeLayer

Spike Dense layer.

build(input_shape)[source]

Creates the layer neurons and connections.

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call(x)[source]
class snntoolbox.simulation.backends.inisim.ttfs_dyn_thresh.SpikeConv2D(filters, kernel_size, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1), groups=1, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, **kwargs)[source]

Bases: keras.layers.convolutional.conv2d.Conv2D, snntoolbox.simulation.backends.inisim.ttfs_dyn_thresh.SpikeLayer

Spike 2D Convolution.

build(input_shape)[source]

Creates the layer weights. Must be implemented on all layers that have weights.

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call(x)[source]
class snntoolbox.simulation.backends.inisim.ttfs_dyn_thresh.SpikeDepthwiseConv2D(kernel_size, strides=(1, 1), padding='valid', depth_multiplier=1, data_format=None, dilation_rate=(1, 1), activation=None, use_bias=True, depthwise_initializer='glorot_uniform', bias_initializer='zeros', depthwise_regularizer=None, bias_regularizer=None, activity_regularizer=None, depthwise_constraint=None, bias_constraint=None, **kwargs)[source]

Bases: keras.layers.convolutional.depthwise_conv2d.DepthwiseConv2D, snntoolbox.simulation.backends.inisim.ttfs_dyn_thresh.SpikeLayer

Spike 2D depthwise-separable convolution.

build(input_shape)[source]

Creates the layer weights. Must be implemented on all layers that have weights.

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call(x)[source]
class snntoolbox.simulation.backends.inisim.ttfs_dyn_thresh.SpikeAveragePooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None, **kwargs)[source]

Bases: keras.layers.pooling.average_pooling2d.AveragePooling2D, snntoolbox.simulation.backends.inisim.ttfs_dyn_thresh.SpikeLayer

Average Pooling.

build(input_shape)[source]

Creates the layer weights. Must be implemented on all layers that have weights.

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call(x)[source]
class snntoolbox.simulation.backends.inisim.ttfs_dyn_thresh.SpikeMaxPooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None, **kwargs)[source]

Bases: keras.layers.pooling.max_pooling2d.MaxPooling2D, snntoolbox.simulation.backends.inisim.ttfs_dyn_thresh.SpikeLayer

Spiking Max Pooling.

build(input_shape)[source]

Creates the layer neurons and connections..

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call(x, mask=None)[source]

Layer functionality.

ttfs_corrective

INI time-to-first-spike simulator backend with corrective spikes.

This module defines the layer objects used to create a spiking neural network for our built-in INI simulator INI_ttfs_corrective_target_sim.

The coding scheme underlying this conversion is that the instantaneous firing rate is given by the inverse time-to-first-spike. In contrast to INI_ttfs_target_sim, this one allows corrective spikes to be fired to improve the first guess made by ttfs.

This simulator works only with Keras backend set to Tensorflow.

@author: rbodo

class snntoolbox.simulation.backends.inisim.ttfs_corrective.SpikeLayer(**kwargs)[source]

Bases: keras.engine.base_layer.Layer

Base class for layer with spiking neurons.

reset(sample_idx)[source]

Reset layer variables.

class_name

Get class name.

update_neurons()[source]

Update neurons according to activation function.

get_spikes(new_mem)[source]

Linear activation.

get_new_mem()[source]

Add input to membrane potential.

get_psp(output_spikes)[source]
get_time()[source]

Get simulation time variable.

Returns:time – Current simulation time.
Return type:float
set_time(time)[source]

Set simulation time variable.

Parameters:time (float) – Current simulation time.
init_membrane_potential(output_shape=None, mode='zero')[source]

Initialize membrane potential.

Helpful to avoid transient response in the beginning of the simulation. Not needed when reset between frames is turned off, e.g. with a video data set.

Parameters:
  • output_shape (Optional[tuple]) – Output shape
  • mode (str) –

    Initialization mode.

    • 'uniform': Random numbers from uniform distribution in [-thr, thr].
    • 'bias': Negative bias.
    • 'zero': Zero (default).
Returns:

init_mem – A tensor of self.output_shape (same as layer).

Return type:

ndarray

reset_spikevars(sample_idx)[source]

Reset variables present in spiking layers. Can be turned off for instance when a video sequence is tested.

init_neurons(input_shape)[source]

Init layer neurons.

get_layer_idx()[source]

Get index of layer.

snntoolbox.simulation.backends.inisim.ttfs_corrective.spike_call(call)[source]
class snntoolbox.simulation.backends.inisim.ttfs_corrective.SpikeConcatenate(axis, **kwargs)[source]

Bases: keras.layers.merging.concatenate.Concatenate

Spike merge layer

static get_time()[source]
static reset(sample_idx)[source]

Reset layer variables.

class_name

Get class name.

class snntoolbox.simulation.backends.inisim.ttfs_corrective.SpikeFlatten(**kwargs)[source]

Bases: keras.layers.reshaping.flatten.Flatten

Spike flatten layer.

static get_time()[source]
reset(sample_idx)[source]

Reset layer variables.

class_name

Get class name.

class snntoolbox.simulation.backends.inisim.ttfs_corrective.SpikeDense(units, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, **kwargs)[source]

Bases: keras.layers.core.dense.Dense, snntoolbox.simulation.backends.inisim.ttfs_corrective.SpikeLayer

Spike Dense layer.

build(input_shape)[source]

Creates the layer neurons and connections.

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call(x)[source]
class snntoolbox.simulation.backends.inisim.ttfs_corrective.SpikeConv2D(filters, kernel_size, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1), groups=1, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, **kwargs)[source]

Bases: keras.layers.convolutional.conv2d.Conv2D, snntoolbox.simulation.backends.inisim.ttfs_corrective.SpikeLayer

Spike 2D Convolution.

build(input_shape)[source]

Creates the layer weights. Must be implemented on all layers that have weights.

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call(x)[source]
class snntoolbox.simulation.backends.inisim.ttfs_corrective.SpikeAveragePooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None, **kwargs)[source]

Bases: keras.layers.pooling.average_pooling2d.AveragePooling2D, snntoolbox.simulation.backends.inisim.ttfs_corrective.SpikeLayer

Average Pooling.

build(input_shape)[source]

Creates the layer weights. Must be implemented on all layers that have weights.

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call(x)[source]
class snntoolbox.simulation.backends.inisim.ttfs_corrective.SpikeMaxPooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None, **kwargs)[source]

Bases: keras.layers.pooling.max_pooling2d.MaxPooling2D, snntoolbox.simulation.backends.inisim.ttfs_corrective.SpikeLayer

Spiking Max Pooling.

build(input_shape)[source]

Creates the layer neurons and connections..

Parameters:input_shape (Union[list, tuple, Any]) – Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
call(x)[source]

megasim

MegaSim spiking neuron simulator.

A collection of helper functions used to get MegaSim’s path and executable.

the configuration file will be stored at $HOME/.snntoolbox/preferences/megasim_config.json

Assumes that have write access to the home folder.

@author: evan

snntoolbox.simulation.backends.megasim.megasim.megasim_path()[source]

snntoolbox.simulation.target_simulators

INI_target_sim

INI_temporal_mean_rate_target_sim

INI simulator with temporal mean rate code.

@author: rbodo

class snntoolbox.simulation.target_simulators.INI_temporal_mean_rate_target_sim.SNN(config, queue=None)[source]

Bases: snntoolbox.simulation.utils.AbstractSNN

The compiled spiking neural network, using layers derived from Keras base classes (see snntoolbox.simulation.backends.inisim.temporal_mean_rate_tensorflow).

Aims at simulating the network on a self-implemented Integrate-and-Fire simulator using a timestepped approach.

snn

Keras model. This is the output format of the compiled spiking model because INI simulator runs networks of layers that are derived from Keras layer base classes.

Type:keras.models.Model
is_parallelizable

Whether or not the simulator is able to test multiple samples in parallel.

add_input_layer(input_shape)[source]

Add input layer.

Parameters:input_shape (list | tuple) – Input shape to the network, including the batch size as first dimension.
add_layer(layer)[source]

Do anything that concerns adding any layer independently of its type.

Parameters:layer (keras.layers.Layer | keras.layers.Conv) – Layer
build_dense(layer)[source]

Build spiking fully-connected layer.

Parameters:layer (keras.layers.Dense) – Layer
build_convolution(layer)[source]

Build spiking convolutional layer.

Parameters:layer (keras.layers.Layer) – Layer
build_pooling(layer)[source]

Build spiking pooling layer.

Parameters:layer (keras.layers.pooling._Pooling2D) – Layer
compile()[source]

Compile the spiking network.

simulate(**kwargs)[source]

Simulate a spiking network for a certain duration, and record any variables of interest (spike trains, membrane potentials, …)

Returns:output_b_l_t – Array of shape (batch_size, num_classes, num_timesteps), containing the number of output spikes of the neurons in the final layer, for each sample and for each time step during the simulation.
Return type:ndarray
reset(sample_idx)[source]

Reset network variables.

Parameters:sample_idx (int) – Index of sample that has just been simulated. In certain applications (video data), we may want to turn off reset between samples.
end_sim()[source]

Clean up after simulation.

save(path, filename)[source]

Write model architecture and parameters to disk.

Parameters:
  • path (string) – Path to directory where to save model.
  • filename (string) – Name of file to write model to.
load(path, filename)[source]

Load model architecture and parameters from disk.

Parameters:
  • path (str) – Path to directory where to load model from.
  • filename (str) – Name of file to load model from.
get_poisson_frame_batch(x_b_l)[source]

Get a batch of Poisson input spikes.

Parameters:x_b_l (ndarray) – The input frame. Shape: (batch_size, layer_shape).
Returns:input_b_l – Array of Poisson input spikes, with same shape as x_b_l.
Return type:ndarray
set_time(t)[source]

Set the simulation time variable of all layers in the network.

Parameters:t (float) – Current simulation time.
set_spiketrain_stats_input()[source]

Count number of operations based on the input spike activity during simulation.

get_spiketrains_input()[source]

Get spike trains of input layer.

Returns:spiketrains_b_l_t – Spike trains of input.
Return type:ndarray
scale_first_layer_parameters(t, input_b_l, tau=1)[source]

INI_temporal_pattern_target_sim

INI simulator with temporal pattern code.

@author: rbodo

class snntoolbox.simulation.target_simulators.INI_temporal_pattern_target_sim.SNN(config, queue=None)[source]

Bases: snntoolbox.simulation.target_simulators.INI_temporal_mean_rate_target_sim.SNN

The compiled spiking neural network, using layers derived from Keras base classes (see snntoolbox.simulation.backends.inisim.temporal_pattern).

Aims at simulating the network on a self-implemented Integrate-and-Fire simulator using a timestepped approach.

snn

Keras model. This is the output format of the compiled spiking model because INI simulator runs networks of layers that are derived from Keras layer base classes.

Type:keras.models.Model
compile()[source]

Compile the spiking network.

simulate(**kwargs)[source]

Simulate a spiking network for a certain duration, and record any variables of interest (spike trains, membrane potentials, …)

Returns:output_b_l_t – Array of shape (batch_size, num_classes, num_timesteps), containing the number of output spikes of the neurons in the final layer, for each sample and for each time step during the simulation.
Return type:ndarray
load(path, filename)[source]

Load model architecture and parameters from disk.

Parameters:
  • path (str) – Path to directory where to load model from.
  • filename (str) – Name of file to load model from.
set_spiketrains(spiketrains_b_l_t, i)[source]
set_spikerates(spikerates_b_l, i)[source]
set_neuron_operations(i)[source]
set_synaptic_operations(spiketrains_b_l_t, i)[source]
snntoolbox.simulation.target_simulators.INI_temporal_pattern_target_sim.to_binary_numpy(x, num_bits)[source]

Transform an array of floats into binary representation.

Parameters:
  • x (ndarray) – Input array containing float values. The first dimension has to be of length 1.
  • num_bits (int) – The fixed point precision to be used when converting to binary.
Returns:

y – Output array with same shape as x except that an axis is added to the last dimension with size num_bits. The binary representation of each value in x is distributed across the last dimension of y.

Return type:

ndarray

INI_ttfs_target_sim

INI simulator with time-to-first-spike code.

@author: rbodo

class snntoolbox.simulation.target_simulators.INI_ttfs_target_sim.SNN(config, queue=None)[source]

Bases: snntoolbox.simulation.target_simulators.INI_temporal_mean_rate_target_sim.SNN

The compiled spiking neural network, using layers derived from Keras base classes (see snntoolbox.simulation.backends.inisim.ttfs).

Aims at simulating the network on a self-implemented Integrate-and-Fire simulator using a timestepped approach.

simulate(**kwargs)[source]

Simulate a spiking network for a certain duration, and record any variables of interest (spike trains, membrane potentials, …)

Returns:output_b_l_t – Array of shape (batch_size, num_classes, num_timesteps), containing the number of output spikes of the neurons in the final layer, for each sample and for each time step during the simulation.
Return type:ndarray
load(path, filename)[source]

Load model architecture and parameters from disk.

Parameters:
  • path (str) – Path to directory where to load model from.
  • filename (str) – Name of file to load model from.

INI_ttfs_dyn_thresh_target_sim

INI simulator with time-to-first-spike code and a dynamic threshold.

@author: rbodo

class snntoolbox.simulation.target_simulators.INI_ttfs_dyn_thresh_target_sim.SNN(config, queue=None)[source]

Bases: snntoolbox.simulation.target_simulators.INI_temporal_mean_rate_target_sim.SNN

The compiled spiking neural network, using layers derived from Keras base classes (see snntoolbox.simulation.backends.inisim.ttfs_dyn_thresh).

Aims at simulating the network on a self-implemented Integrate-and-Fire simulator using a timestepped approach.

simulate(**kwargs)[source]

Simulate a spiking network for a certain duration, and record any variables of interest (spike trains, membrane potentials, …)

Returns:output_b_l_t – Array of shape (batch_size, num_classes, num_timesteps), containing the number of output spikes of the neurons in the final layer, for each sample and for each time step during the simulation.
Return type:ndarray
load(path, filename)[source]

Load model architecture and parameters from disk.

Parameters:
  • path (str) – Path to directory where to load model from.
  • filename (str) – Name of file to load model from.

INI_ttfs_corrective_target_sim

INI simulator with time-to-first-spike code and corrective spikes.

@author: rbodo

pyNN_target_sim

Building and simulating spiking neural networks using pyNN.

@author: rbodo

class snntoolbox.simulation.target_simulators.pyNN_target_sim.SNN(config, queue=None)[source]

Bases: snntoolbox.simulation.utils.AbstractSNN

Class to hold the compiled spiking neural network.

Represents the compiled spiking neural network, ready for testing in a spiking simulator.

layers

Each entry represents a layer, i.e. a population of neurons, in form of pyNN Population objects.

Type:list[pyNN.Population]
connections

pyNN Projection objects representing the connections between individual layers.

Type:list[pyNN.Projection]
cellparams

Neuron cell parameters determining properties of the spiking neurons in pyNN simulators.

Type:dict
is_parallelizable

Whether or not the simulator is able to test multiple samples in parallel.

add_input_layer(input_shape)[source]

Add input layer.

Parameters:input_shape (list | tuple) – Input shape to the network, including the batch size as first dimension.
add_layer(layer)[source]

Do anything that concerns adding any layer independently of its type.

Parameters:layer (keras.layers.Layer | keras.layers.Conv) – Layer
build_dense(layer)[source]
Parameters:layer (keras.layers.Dense) –
build_convolution(layer)[source]

Build spiking convolutional layer.

Parameters:layer (keras.layers.Layer) – Layer
build_pooling(layer)[source]

Build spiking pooling layer.

Parameters:layer (keras.layers.pooling._Pooling2D) – Layer
compile()[source]

Compile the spiking network.

simulate(**kwargs)[source]

Simulate a spiking network for a certain duration, and record any variables of interest (spike trains, membrane potentials, …)

Returns:output_b_l_t – Array of shape (batch_size, num_classes, num_timesteps), containing the number of output spikes of the neurons in the final layer, for each sample and for each time step during the simulation.
Return type:ndarray
reset(sample_idx)[source]

Reset network variables.

Parameters:sample_idx (int) – Index of sample that has just been simulated. In certain applications (video data), we may want to turn off reset between samples.
end_sim()[source]

Clean up after simulation.

save(path, filename)[source]

Write model architecture and parameters to disk.

Parameters:
  • path (string) – Path to directory where to save model.
  • filename (string) – Name of file to write model to.
load(path, filename)[source]

Load model architecture and parameters from disk.

Parameters:
  • path (str) – Path to directory where to load model from.
  • filename (str) – Name of file to load model from.
init_cells()[source]

Set cellparameters of neurons in each layer and initialize membrane potential.

set_biases(biases)[source]

Set biases.

Notes

This assumes no leak.

get_vars_to_record()[source]

Get variables to record during simulation.

Returns:vars_to_record – The names of variables to record during simulation.
Return type:list[str]
get_spiketrains(**kwargs)[source]

Get spike trains of a layer.

Returns:spiketrains_b_l_t – Spike trains.
Return type:ndarray
get_spiketrains_input()[source]

Get spike trains of input layer.

Returns:spiketrains_b_l_t – Spike trains of input.
Return type:ndarray
get_spiketrains_output()[source]

Get spike trains of output layer.

Returns:spiketrains_b_l_t – Spike trains of output.
Return type:ndarray
get_vmem(**kwargs)[source]

Get membrane potentials of a layer.

Returns:mem_b_l_t – Membrane potentials of layer.
Return type:ndarray
save_assembly(path, filename)[source]

Write layers of neural network to disk.

The size, structure, labels of all the population of an assembly are stored in a dictionary such that one can load them again using the load_assembly function.

The term “assembly” refers to pyNN internal nomenclature, where Assembly is a collection of layers (Populations), which in turn consist of a number of neurons (cells).

Parameters:
  • path (str) – Path to directory where to save layers.
  • filename (str) – Name of file to write layers to.
save_connections(path)[source]

Write parameters of a neural network to disk.

The parameters between two layers are saved in a text file. They can then be used to connect pyNN populations e.g. with sim.Projection(layer1, layer2, sim.FromListConnector(filename)), where sim is a simulator supported by pyNN, e.g. Brian, NEURON, or NEST.

Parameters:path (str) – Path to directory where connections are saved.
Returns:
  • Text files containing the layer connections. Each file is named
  • after the layer it connects to, e.g. layer2.txt if connecting
  • layer1 to layer2.
save_biases(path)[source]

Write biases of a neural network to disk.

Parameters:path (str) – Path to directory where connections are saved.
load_assembly(path, filename)[source]

Load the populations in an assembly.

Loads the populations in an assembly that was saved with the save_assembly function.

The term “assembly” refers to pyNN internal nomenclature, where Assembly is a collection of layers (Populations), which in turn consist of a number of neurons (cells).

Parameters:
  • path (str) – Path to directory where to load model from.
  • filename (str) – Name of file to load model from.
Returns:

layers – List of pyNN Population objects.

Return type:

list[pyNN.Population]

set_spiketrain_stats_input()[source]

Count number of operations based on the input spike activity during simulation.

class snntoolbox.simulation.target_simulators.pyNN_target_sim.MyProgressBar(interval, t_stop)[source]

Bases: object

A callback which draws a progress bar in the terminal.

brian2_target_sim

Building and simulating spiking neural networks using Brian2.

@author: rbodo

class snntoolbox.simulation.target_simulators.brian2_target_sim.SNN(config, queue=None)[source]

Bases: snntoolbox.simulation.utils.AbstractSNN

Represents the compiled spiking neural network, ready for testing in a spiking simulator.

layers

Each entry represents a layer, i.e. a population of neurons, in form of Brian2 NeuronGroup objects.

Type:list[brian2.NeuronGroup]
connections

Brian2 Synapses objects representing the connections between individual layers.

Type:list[brian2.Synapses]
threshold

Defines spiking threshold.

Type:str
v_reset

Defines reset potential.

Type:str
eqs

Differential equation for membrane potential.

Type:str
spikemonitors

Brian2 SpikeMonitor s for each layer that records spikes.

Type:list[brian2.SpikeMonitor]
statemonitors

Brian2 StateMonitor s for each layer that records membrane potential.

Type:list[brian2.StateMonitor]
snn

The spiking network.

Type:brian2.Network
is_parallelizable

Whether or not the simulator is able to test multiple samples in parallel.

add_input_layer(input_shape)[source]

Add input layer.

Parameters:input_shape (list | tuple) – Input shape to the network, including the batch size as first dimension.
add_layer(layer)[source]

Do anything that concerns adding any layer independently of its type.

Parameters:layer (keras.layers.Layer | keras.layers.Conv) – Layer
build_dense(layer, weights=None)[source]

Build spiking fully-connected layer.

Parameters:layer (keras.layers.Dense) – Layer
build_convolution(layer, weights=None)[source]

Build spiking convolutional layer.

Parameters:layer (keras.layers.Layer) – Layer
build_pooling(layer, weights=None)[source]

Build spiking pooling layer.

Parameters:layer (keras.layers.pooling._Pooling2D) – Layer
compile()[source]

Compile the spiking network.

simulate(**kwargs)[source]

Simulate a spiking network for a certain duration, and record any variables of interest (spike trains, membrane potentials, …)

Returns:output_b_l_t – Array of shape (batch_size, num_classes, num_timesteps), containing the number of output spikes of the neurons in the final layer, for each sample and for each time step during the simulation.
Return type:ndarray
reset(sample_idx)[source]

Reset network variables.

Parameters:sample_idx (int) – Index of sample that has just been simulated. In certain applications (video data), we may want to turn off reset between samples.
end_sim()[source]

Clean up after simulation.

save(path, filename)[source]

Write model architecture and parameters to disk.

Parameters:
  • path (string) – Path to directory where to save model.
  • filename (string) – Name of file to write model to.
load(path, filename)[source]

Load model architecture and parameters from disk.

Parameters:
  • path (str) – Path to directory where to load model from.
  • filename (str) – Name of file to load model from.
init_cells()[source]

Set cellparameters of neurons in each layer and initialize membrane potential.

get_spiketrains(**kwargs)[source]

Get spike trains of a layer.

Returns:spiketrains_b_l_t – Spike trains.
Return type:ndarray
get_spiketrains_input()[source]

Get spike trains of input layer.

Returns:spiketrains_b_l_t – Spike trains of input.
Return type:ndarray
get_spiketrains_output()[source]

Get spike trains of output layer.

Returns:spiketrains_b_l_t – Spike trains of output.
Return type:ndarray
get_vmem(**kwargs)[source]

Get membrane potentials of a layer.

Returns:mem_b_l_t – Membrane potentials of layer.
Return type:ndarray
set_spiketrain_stats_input()[source]

Count number of operations based on the input spike activity during simulation.

set_biases(biases)[source]

Set biases.

MegaSim_target_sim

MegaSim asynchronous spiking simulator.

@author: Evangelos Stromatias

class snntoolbox.simulation.target_simulators.MegaSim_target_sim.Megasim_base[source]

Bases: abc.ABC

Class that holds the common attributes and methods for the MegaSim modules.

Attributes set to -1 must be set by each subclass, the rest can be used as default values
Attributes common to all MegaSim modules
n_in_ports

Number of input ports

Type:int
n_out_ports

Number of output ports

Type:int
delay_to_process

Delay to process an input event

Type:int
delay_to_ack

Delay to acknoldege an event

Type:int
fifo_depth

Depth of input fifo

Type:int
n_repeat
Type:int
delay_to_repeat
Type:int
#Parameters for the convolutional module and avgerage pooling module
Nx_array

X dimensions of the feature map

Type:int
Ny_array

Y dimensions of the feature map

Type:int
Xmin

start counting from Xmin (=0)

Type:int
Ymin

start counting from Ymin (=0)

Type:int
THplus

Positive threshold

THplusInfo

Flag to enable spikes when reaching the positive threshold

THminus

Negative threshold

THminusInfo

Flag to enable spikes with negative polarity when reaching the negative threshold

Reset_to_reminder

After reaching the threshold if set it will set the membrane to the difference

MembReset

Resting potential (=0)

Type:int
TLplus

Linear leakage slope from the positive threshold

Type:int
TLminus

Linear leakage slope from the negative threshold

Type:int
Tmin

minimum time between 2 spikes

Type:int
T_Refract

Refractory period

Type:int
# Parameters for the output
crop_xmin

Xmin crop of the feature map

Type:int
crop_xmax
Type:int
crop_ymin
Type:int
crop_ymax
Type:int
xshift_pre

X shift before subsampling

Type:int
yshift_pre

Y shift before subsampling

Type:int
x_subsmp

Subsampling (=1 if none)

Type:int
y_subsmp
Type:int
xshift_pos

X shift after subsampling

Type:int
yshift_pos
Type:int
rectify

Flag that if set will force all spikes to have positive polarity

Type:int
# The fully connected module has population_size instead of Nx_array
population_size

Number of neurons in the fully connected module

Type:int
Nx_array_pre

Number of neurons in the previous layer

Type:int
# Needed by the state file
time_busy_initial

Initial state of the module (=0)

Type:int
# Scaling factor
scaling_factor

Scaling factor for the parameters since MegaSim works with integers

Type:int
build_state_file:

Input parameters: a string with the full path to the megasim SNN directory

This method is similar to all MegaSim modules. It generates an initial state file per module based on the time_busy_initial.

build_parameter_file:

Input parameters: a string with the full path to the megasim SNN directory

This method generates the module’s parameter file based on its attributes set by the sub-class. This method depends on the MegaSim module and will raise error if not implemented.

n_in_ports = -1
n_out_ports = 1
delay_to_process = 0
delay_to_ack = 0
fifo_depth = 0
n_repeat = 1
delay_to_repeat = 15
Nx_array = -1
Ny_array = 1
Xmin = 0
Ymin = 0
THplus = 0
THplusInfo = 1
THminus = -2147483646
THminusInfo = 0
Reset_to_reminder = 0
MembReset = 0
TLplus = 0
TLminus = 0
Tmin = 0
T_Refract = 0
crop_xmin = -1
crop_xmax = -1
crop_ymin = -1
crop_ymax = -1
xshift_pre = 0
yshift_pre = 0
x_subsmp = 1
y_subsmp = 1
xshift_pos = 0
yshift_pos = 0
rectify = 0
population_size = -1
Nx_array_pre = -1
time_busy_initial = 0
scaling_factor = 1
build_state_file(dirname)[source]

dirname = the full path of the

build_parameter_file(dirname)[source]
class snntoolbox.simulation.target_simulators.MegaSim_target_sim.module_input_stimulus(label, pop_size)[source]

Bases: object

A dummy class for the input stimulus.

Parameters:
  • label (string) – String to hold the module’s name.
  • pop_size (int) – Integer to store the population size.
label
Type:string
pop_size
Type:int
input_stimulus_file

String to hold the filename of the input stimulus

Type:string
module_string

String that holds the module name for megasim

Type:string
evs_files

List of strings of the event filenames that will generated when a megasim simulation is over.

Type:list
class snntoolbox.simulation.target_simulators.MegaSim_target_sim.module_flatten(layer_params, input_ports, fm_size)[source]

Bases: snntoolbox.simulation.target_simulators.MegaSim_target_sim.Megasim_base

A class for the flatten megasim module. The flatten module is used to connect a 3D population to a 1D population. eg A convolutional layer to a fully connected one.

Parameters:
  • layer_params (Keras layer) – Layer from parsed input model.
  • input_ports (int) – Number of input ports (eg feature maps from the previous layer)
  • fm_size (tuple) – Tuple of integers that holds the size of the feature maps from the previous layer
module_string

String that holds the module name for megasim

Type:string
output_shapes

Tuple that holds the shape of the output of the module. Used for the plotting.

Type:tuple
evs_files

List of strings of the event filenames that will generated when a megasim simulation is over.

Type:list
build_parameter_file(dirname)[source]
class snntoolbox.simulation.target_simulators.MegaSim_target_sim.Module_average_pooling(layer_params, neuron_params, reset_input_event=False, scaling_factor=10000000)[source]

Bases: snntoolbox.simulation.target_simulators.MegaSim_target_sim.Megasim_base

duplicate code with the module_conv class - TODO: merge them layer_params Attributes: [‘label’, ‘layer_num’, ‘padding’, ‘layer_type’, ‘strides’, ‘input_shape’, ‘output_shape’, ‘get_activ’, ‘pool_size’]

build_parameter_file(dirname)[source]
class snntoolbox.simulation.target_simulators.MegaSim_target_sim.Module_conv(layer_params, neuron_params, flip_kernels=True, reset_input_event=False, scaling_factor=10000000)[source]

Bases: snntoolbox.simulation.target_simulators.MegaSim_target_sim.Megasim_base

A class for the convolutional megasim module.

Parameters:
  • layer_params (Keras layer) – Layer from parsed input model.
  • neuron_params (dictionary) – This is the settings dictionary that is set in the config.py module
  • flip_kernels (boolean) – If set will flip the kernels upside down.
  • scaling_factor (int) – An integer that will be used to scale all parameters.
module_string

String that holds the module name for megasim

Type:string
output_shapes

Tuple that holds the shape of the output of the module. Used for the plotting.

Type:tuple
evs_files

List of strings of the event filenames that will generated when a megasim simulation is over.

Type:list
num_of_FMs

Number of feature maps in this layer

Type:int
w

list of weights

Type:list
padding

String with the border mode used for the convolutional layer. So far only the valid mode is implemented

Type:string

layer_params Attributes: [‘kernel_size’, ‘activation’, ‘layer_type’, ‘layer_num’, ‘filters’, ‘output_shape’, ‘input_shape’, ‘label’, ‘parameters’, ‘padding’]

build_parameter_file(dirname)[source]
class snntoolbox.simulation.target_simulators.MegaSim_target_sim.Module_fully_connected(layer_params, neuron_params, scaling_factor=10000000, reset_input_event=False, enable_softmax=True)[source]

Bases: snntoolbox.simulation.target_simulators.MegaSim_target_sim.Megasim_base

A class for the fully connected megasim module.

Parameters:
  • layer_params (Keras layer) – Layer from parsed input model.
  • neuron_params (dictionary) – This is the settings dictionary that is set in the config.py module
  • scaling_factor (int) – An integer that will be used to scale all parameters.
  • enable_softmax (Boolean) – A flag that if set will use (if the ann uses it) softmax for the output layer. If not set a population of LIF neurons will be used instead.
module_string

String that holds the module name for megasim

Type:string
output_shapes

Tuple that holds the shape of the output of the module. Used for the plotting.

Type:tuple
evs_files

List of strings of the event filenames that will generated when a megasim simulation is over.

Type:list
num_of_FMs

Number of feature maps in this layer

Type:int
w

list of weights

Type:list
padding

String with the border mode used for the convolutional layer. So far only the valid mode is implemented

Type:string

layer_params Attributes: [‘kernel_size’, ‘activation’, ‘layer_type’, ‘layer_num’, ‘filters’, ‘output_shape’, ‘input_shape’, ‘label’, ‘parameters’, ‘padding’]

build_parameter_file(dirname)[source]
build_softmax_conrol_events(megadirname, duration, dt, input_rate, softmax_clockrate=300)[source]
class snntoolbox.simulation.target_simulators.MegaSim_target_sim.SNN(config, queue=None)[source]

Bases: snntoolbox.simulation.utils.AbstractSNN

Represents the compiled spiking neural network, ready for testing in a spiking simulator.

layers

Each entry represents a layer.

Type:list
connections

The connections between layers.

Type:list
megasim_path

The path to megasim installation directory.

Type:str
megaschematic

String that holds megasim main schmatic file that is needed to run a simulation

Type:str
megadirname

String that holds the full path where the generated files for a megasim simulation are stored. These files include the stimulus, parameter, state and schematic files. The event files will be generated in the same folder.

Type:str
input_stimulus_file

Filename of input stimulus.

Type:str
cellparams

Neuron cell parameters determining properties of the spiking neurons.

Type:dict
use_biases

Whether or not to use biases.

Type:bool
is_parallelizable

Whether or not the simulator is able to test multiple samples in parallel.

add_input_layer(input_shape)[source]

Add input layer.

Parameters:input_shape (list | tuple) – Input shape to the network, including the batch size as first dimension.
add_layer(layer)[source]

Do anything that concerns adding any layer independently of its type.

Parameters:layer (keras.layers.Layer | keras.layers.Conv) – Layer
build_dense(layer)[source]

Build spiking fully-connected layer.

Parameters:layer (keras.layers.Dense) – Layer
build_convolution(layer)[source]

Build spiking convolutional layer.

Parameters:layer (keras.layers.Layer) – Layer
build_pooling(layer)[source]

Build spiking pooling layer.

Parameters:layer (keras.layers.pooling._Pooling2D) – Layer
build_flatten(layer)[source]

Build flatten layer.

May not be needed depending on the simulator.

Parameters:layer (keras.layers.Layer) – Layer
compile()[source]

Compile the spiking network.

simulate(**kwargs)[source]

Simulate a spiking network for a certain duration, and record any variables of interest (spike trains, membrane potentials, …)

Returns:output_b_l_t – Array of shape (batch_size, num_classes, num_timesteps), containing the number of output spikes of the neurons in the final layer, for each sample and for each time step during the simulation.
Return type:ndarray
reset(sample_idx)[source]

Reset network variables.

Parameters:sample_idx (int) – Index of sample that has just been simulated. In certain applications (video data), we may want to turn off reset between samples.
end_sim()[source]

Clean up after simulation.

save(path, filename)[source]

Write model architecture and parameters to disk.

Parameters:
  • path (string) – Path to directory where to save model.
  • filename (string) – Name of file to write model to.
load(path, filename)[source]

Load model architecture and parameters from disk.

Parameters:
  • path (str) – Path to directory where to load model from.
  • filename (str) – Name of file to load model from.
get_spiketrains(**kwargs)[source]

Get spike trains of a layer.

Returns:spiketrains_b_l_t – Spike trains.
Return type:ndarray
get_spiketrains_input()[source]

Get spike trains of input layer.

Returns:spiketrains_b_l_t – Spike trains of input.
Return type:ndarray
get_spiketrains_output()[source]

Get spike trains of output layer.

Returns:spiketrains_b_l_t – Spike trains of output.
Return type:ndarray
get_vmem(**kwargs)[source]

Get membrane potentials of a layer.

Returns:mem_b_l_t – Membrane potentials of layer.
Return type:ndarray
get_spikes()[source]

Method that fetches all the events from all layers after a simulation is over.

Returns:
Return type:A list of all the events from all the layers.
get_output_spikes_batch()[source]

Method that fetches the events from the output layer.

Returns:
Return type:A numpy array of the output events.
static spike_count_histogram(events, pop_size=10)[source]

This method first creates a histogram based on the size of the layer and then returns the argmax of the neuron that fired the most spikes for that particular stimulus.

If there are no spikes it will return -1.

Parameters:
  • events (list) – List of megasim events of a particular layer
  • pop_size (int) – Size of the fully connected module
Returns:

Return type:

pop_spike_hist

static check_megasim_output(megalog)[source]

A method that checks the prints of MegaSim for errors

Parameters:megalog (str) – String returned from executing megasim.
poisson_spike_generator_megasim(mnist_digit)[source]
Parameters:mnist_digit (ndarray) – A 1d or 2d numpy array of an mnist digit (normalised 0-1).
Returns:
  • It will store the generated spike trains to a stimulus file in the
  • megasim sim folder.
poisson_spike_generator_batchmode_megasim(mnist_digits)[source]
Parameters:mnist_digits (ndarray) – A 1d or 2d numpy array of an mnist digit (normalised 0-1).
Returns:
  • It will store the generated spike trains to a stimulus file in the
  • megasim sim folder.
generate_bias_clk(timestamp_batches)[source]

An external periodic (per timestep) event is used to trigger the biases, since megasim simulator is not a time-stepped simulator.

Parameters:timestamp_batches (List[list]) – List that includes the first and last timestamps of the input source, e.g. [[start0, stop0], [start1, stop1]].
Returns:
Return type:Generates a megasim stimulus file in the experiment folder.
build_schematic_updated()[source]

This method generates the main MegaSim schematic file

TODO: this method is quite ugly! need to refactor it 99.20 non normalised first 100 samples = 100% reset to zero ——-

clean_megasim_sim_data()[source]

A method that removes the previous stimulus file and generated event files before testing a new sample.

set_spiketrain_stats_input()[source]

Count number of operations based on the input spike activity during simulation.

loihi_target_sim

spiNNaker_target_sim

Building and simulating spiking neural networks using SpiNNaker.

Dependency: SpyNNaker software

@author: UMan, rbodo, piewchee

class snntoolbox.simulation.target_simulators.spiNNaker_target_sim.SNN(config, queue=None)[source]

Bases: snntoolbox.simulation.target_simulators.pyNN_target_sim.SNN

scale_weights(weights)[source]
setup_layers(batch_shape)[source]

Iterates over all layers to instantiate them in the simulator.

add_layer(layer)[source]

Do anything that concerns adding any layer independently of its type.

Parameters:layer (keras.layers.Layer | keras.layers.Conv) – Layer
build_dense(layer)[source]
Parameters:layer (keras.layers.Dense) –
build_convolution(layer)[source]

Build spiking convolutional layer.

Parameters:layer (keras.layers.Layer) – Layer
build_pooling(layer)[source]

Build spiking pooling layer.

Parameters:layer (keras.layers.pooling._Pooling2D) – Layer
save(path, filename)[source]

Write model architecture and parameters to disk.

Parameters:
  • path (string) – Path to directory where to save model.
  • filename (string) – Name of file to write model to.
save_connections(path)[source]

Write parameters of a neural network to disk.

The parameters between two layers are saved in a text file. They can then be used to connect pyNN populations e.g. with sim.Projection(layer1, layer2, sim.FromListConnector(filename)), where sim is a simulator supported by pyNN, e.g. Brian, NEURON, or NEST.

Parameters:path (str) – Path to directory where connections are saved.
Returns:
  • Text files containing the layer connections. Each file is named
  • after the layer it connects to, e.g. layer2.txt if connecting
  • layer1 to layer2.
simulate(**kwargs)[source]

Simulate a spiking network for a certain duration, and record any variables of interest (spike trains, membrane potentials, …)

Returns:output_b_l_t – Array of shape (batch_size, num_classes, num_timesteps), containing the number of output spikes of the neurons in the final layer, for each sample and for each time step during the simulation.
Return type:ndarray
get_spiketrains_input()[source]

Get spike trains of input layer.

Returns:spiketrains_b_l_t – Spike trains of input.
Return type:ndarray
get_spiketrains_output()[source]

Get spike trains of output layer.

Returns:spiketrains_b_l_t – Spike trains of output.
Return type:ndarray
get_spiketrains(**kwargs)[source]

Get spike trains of a layer.

Returns:spiketrains_b_l_t – Spike trains.
Return type:ndarray
get_vmem(**kwargs)[source]

Get membrane potentials of a layer.

Returns:mem_b_l_t – Membrane potentials of layer.
Return type:ndarray