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
-
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]]
-
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
-
num_neurons_with_bias
¶ Number of neurons with bias in the network (one entry per layer).
-
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]]
-
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 toimport 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
-
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.
-
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.
-
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. Therun
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:
-
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
), setbatch_size
to 1.
-
restore_snn
()[source]¶ Restore both the spiking and the parsed network from disk.
This method works for spiking Keras models.
-
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
andmem_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
-
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. IfFalse
,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
-
-
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: 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
.
- 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
-
snntoolbox.simulation.utils.
build_depthwise_convolution
(layer, delay, transpose_kernel=False)[source]¶ Build convolution layer.
Parameters: 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
.
- 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
-
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: Returns: spikerates_n_b_l
Return type:
-
snntoolbox.simulation.utils.
get_sample_activity_from_batch
(activity_batch, idx=0)[source]¶ Return layer activity for sample
idx
of anactivity_batch
.
-
snntoolbox.simulation.utils.
get_spiking_outbound_layers
(layer, config)[source]¶ Iterate until spiking outbound layers are found.
Parameters: - layer (keras.layers.Layer) – Layer
- config (configparser.ConfigParser) – Settings.
Returns: List of outbound layers.
Return type:
-
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:
-
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: Returns: Number of operations.
Return type:
-
snntoolbox.simulation.utils.
is_spiking
(layer, config)[source]¶ Test if layer is going to be converted to a layer that spikes.
Parameters: - layer (Keras.layers.Layer) – Layer of parsed model.
- config (configparser.ConfigParser) – Settings.
Returns: True
if converted layer will have spiking neurons.Return type:
-
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 excludingFlatten
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’.
- plot_vars (dict) –
-
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 excludingFlatten
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 excludingFlatten
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’.
- plot_vars (dict) –
-
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’.
- layer (tuple[np.array, str]) –
-
snntoolbox.simulation.plotting.
plot_activations
(model, x_test, path, data_format=None)[source]¶ Plot activations of a network.
Parameters:
-
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
andactivations
. IfTrue
, the maximum is chosen. Default:True
.
-
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'
.
- spikerates (list of tuples
-
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 excludingFlatten
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.
- spikerates_batch (list[tuple[np.array, str]]) –
-
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’.
- layer (tuple[np.array, str]) –
-
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 likev_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:
-
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:
-
snntoolbox.simulation.plotting.
plot_spikecount_vs_time
(spiketrains_n_b_l_t, duration, dt, path=None)[source]¶ Plot total spikenumber over time.
Parameters:
-
snntoolbox.simulation.plotting.
plot_input_image
(x, label, path=None, data_format=None, filename=None)[source]¶ Show an input image.
Parameters:
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.
-
class_name
¶ Get class name.
-
update_payload
(residuals, spikes)[source]¶ Update payloads.
Uses the residual of the membrane potential after spike.
-
quantized_activation
(mem, m, f)[source]¶ Activation with precision reduced to fixed point format Qm.f.
-
set_reset_mem
(mem, spikes)[source]¶ Reset membrane potential
mem
array wherespikes
array is nonzero.
-
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: 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.
-
get_clamp_idx
()[source]¶ Get time step when to stop clamping membrane potential.
Returns: Time step when to stop clamping. Return type: int
-
-
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.
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
-
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 intf.init_scope()
). It is recommended to create state, includingtf.Variable
instances and nestedLayer
instances,in__init__()
, or in thebuild()
method that iscalled 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 zeroarguments, andinputs
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 frominputs
only. input_spec
compatibility is only checked againstinputs
.- 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.
- NumPy array or Python scalar values in
- *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 indicatingwhether thecall
is meant for training or inference.
Returns: A tensor or list/tuple of tensors.
- inputs –
-
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 intf.init_scope()
). It is recommended to create state, includingtf.Variable
instances and nestedLayer
instances,in__init__()
, or in thebuild()
method that iscalled 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 zeroarguments, andinputs
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 frominputs
only. input_spec
compatibility is only checked againstinputs
.- 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.
- NumPy array or Python scalar values in
- *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 indicatingwhether thecall
is meant for training or inference.
Returns: A tensor or list/tuple of tensors.
- inputs –
-
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 intf.init_scope()
). It is recommended to create state, includingtf.Variable
instances and nestedLayer
instances,in__init__()
, or in thebuild()
method that iscalled 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 zeroarguments, andinputs
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 frominputs
only. input_spec
compatibility is only checked againstinputs
.- 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.
- NumPy array or Python scalar values in
- *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 indicatingwhether thecall
is meant for training or inference.
Returns: A tensor or list/tuple of tensors.
- inputs –
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
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.
-
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 lengthnum_bits
. The binary representation of each value inx
is distributed across the first dimension ofx_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
-
class_name
¶ Get class name.
-
-
class
snntoolbox.simulation.backends.inisim.temporal_pattern.
SpikeFlatten
(**kwargs)[source]¶ Bases:
keras.layers.reshaping.flatten.Flatten
Spike flatten layer.
-
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.
-
class
snntoolbox.simulation.backends.inisim.temporal_pattern.
SpikeZeroPadding2D
(*args, **kwargs)[source]¶ Bases:
keras.layers.reshaping.zero_padding2d.ZeroPadding2D
Spike padding layer.
-
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 intf.init_scope()
). It is recommended to create state, includingtf.Variable
instances and nestedLayer
instances,in__init__()
, or in thebuild()
method that iscalled 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 zeroarguments, andinputs
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 frominputs
only. input_spec
compatibility is only checked againstinputs
.- 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.
- NumPy array or Python scalar values in
- *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 indicatingwhether thecall
is meant for training or inference.
Returns: A tensor or list/tuple of tensors.
- inputs –
-
-
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 intf.init_scope()
). It is recommended to create state, includingtf.Variable
instances and nestedLayer
instances,in__init__()
, or in thebuild()
method that iscalled 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 zeroarguments, andinputs
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 frominputs
only. input_spec
compatibility is only checked againstinputs
.- 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.
- NumPy array or Python scalar values in
- *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 indicatingwhether thecall
is meant for training or inference.
Returns: A tensor or list/tuple of tensors.
- inputs –
-
-
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 intf.init_scope()
). It is recommended to create state, includingtf.Variable
instances and nestedLayer
instances,in__init__()
, or in thebuild()
method that iscalled 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 zeroarguments, andinputs
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 frominputs
only. input_spec
compatibility is only checked againstinputs
.- 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.
- NumPy array or Python scalar values in
- *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 indicatingwhether thecall
is meant for training or inference.
Returns: A tensor or list/tuple of tensors.
- inputs –
-
-
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 intf.init_scope()
). It is recommended to create state, includingtf.Variable
instances and nestedLayer
instances,in__init__()
, or in thebuild()
method that iscalled 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 zeroarguments, andinputs
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 frominputs
only. input_spec
compatibility is only checked againstinputs
.- 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.
- NumPy array or Python scalar values in
- *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 indicatingwhether thecall
is meant for training or inference.
Returns: A tensor or list/tuple of tensors.
- inputs –
-
-
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 intf.init_scope()
). It is recommended to create state, includingtf.Variable
instances and nestedLayer
instances,in__init__()
, or in thebuild()
method that iscalled 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 zeroarguments, andinputs
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 frominputs
only. input_spec
compatibility is only checked againstinputs
.- 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.
- NumPy array or Python scalar values in
- *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 indicatingwhether thecall
is meant for training or inference.
Returns: A tensor or list/tuple of tensors.
- inputs –
-
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.
-
class_name
¶ Get class name.
-
set_reset_mem
(mem, spikes)[source]¶ Reset membrane potential
mem
array wherespikes
array is nonzero.
-
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: Returns: init_mem – A tensor of
self.output_shape
(same as layer).Return type: ndarray
-
-
class
snntoolbox.simulation.backends.inisim.ttfs.
SpikeConcatenate
(axis, **kwargs)[source]¶ Bases:
keras.layers.merging.concatenate.Concatenate
Spike merge layer
-
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
-
class_name
¶ Get class name.
-
-
class
snntoolbox.simulation.backends.inisim.ttfs.
SpikeReshape
(*args, **kwargs)[source]¶ Bases:
keras.layers.reshaping.reshape.Reshape
Spike reshape layer
-
class_name
¶ Get class name.
-
-
class
snntoolbox.simulation.backends.inisim.ttfs.
SpikeFlatten
(**kwargs)[source]¶ Bases:
keras.layers.reshaping.flatten.Flatten
Spike flatten layer.
-
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.
-
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.
-
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.
-
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.
-
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.
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.
-
class_name
¶ Get class name.
-
set_reset_mem
(mem, spikes)[source]¶ Reset membrane potential
mem
array wherespikes
array is nonzero.
-
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: Returns: init_mem – A tensor of
self.output_shape
(same as layer).Return type: ndarray
-
-
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 intf.init_scope()
). It is recommended to create state, includingtf.Variable
instances and nestedLayer
instances,in__init__()
, or in thebuild()
method that iscalled 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 zeroarguments, andinputs
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 frominputs
only. input_spec
compatibility is only checked againstinputs
.- 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.
- NumPy array or Python scalar values in
- *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 indicatingwhether thecall
is meant for training or inference.
Returns: A tensor or list/tuple of tensors.
- inputs –
-
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.
-
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.
-
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.
-
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.
-
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.
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.
-
class_name
¶ Get class name.
-
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: Returns: init_mem – A tensor of
self.output_shape
(same as layer).Return type: ndarray
-
-
class
snntoolbox.simulation.backends.inisim.ttfs_corrective.
SpikeConcatenate
(axis, **kwargs)[source]¶ Bases:
keras.layers.merging.concatenate.Concatenate
Spike merge layer
-
class_name
¶ Get class name.
-
-
class
snntoolbox.simulation.backends.inisim.ttfs_corrective.
SpikeFlatten
(**kwargs)[source]¶ Bases:
keras.layers.reshaping.flatten.Flatten
Spike flatten layer.
-
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.
-
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.
-
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.
-
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.
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.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
-
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.
-
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.
-
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.
-
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
-
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
-
-
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 sizenum_bits
. The binary representation of each value inx
is distributed across the last dimension ofy
.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
-
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
-
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_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
-
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.
-
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.
-
init_cells
()[source]¶ Set cellparameters of neurons in each layer and initialize membrane potential.
-
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:
-
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))
, wheresim
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: Returns: layers – List of pyNN
Population
objects.Return type: list[pyNN.Population]
-
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]
-
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
-
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.
-
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.
-
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
-
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
-
#Parameters for the convolutional module and avgerage pooling module
-
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
-
# Parameters for the output
-
# The fully connected module has population_size instead of Nx_array
-
# Needed by the state file
-
# Scaling factor
-
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
-
-
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
-
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
-
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: -
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
-
-
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’]
-
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
-
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’]
-
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
-
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’]
-
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.
-
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
-
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
-
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.
-
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.
-
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: 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 ——-
-
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
-
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_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))
, wheresim
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
-