snntoolbox.utils

snntoolbox.utils.utils

General utility functions on project-level.

@author: rbodo

snntoolbox.utils.utils.get_range(start=0.0, stop=1.0, num=5, method='linear')[source]

Return a range of parameter values.

Convenience function. For more flexibility, use numpy.linspace, numpy.logspace, numpy.random.random_sample directly.

Parameters:
  • start (float) – The starting value of the sequence
  • stop (float) – End value of the sequence.
  • num (int) – Number of samples to generate. Must be non-negative.
  • method (str) – The sequence will be computed on either a linear, logarithmic or random grid.
Returns:

samples – There are num samples in the closed interval [start, stop].

Return type:

np.array

snntoolbox.utils.utils.confirm_overwrite(filepath)[source]

If config.get(‘output’, ‘overwrite’)==False and the file exists, ask user if it should be overwritten.

snntoolbox.utils.utils.to_json(data, path)[source]

Write data dictionary to path.

A TypeError is raised if objects in data are not JSON serializable.

snntoolbox.utils.utils.import_helpers(filepath, config)[source]

Import a module with helper functions from filepath.

Parameters:
  • filepath (str) – Filename or relative or absolute path of module to import. If only the filename is given, module is assumed to be in current working directory (config.get('paths', 'path_wd')). Non-absolute paths are taken relative to working dir.
  • config (configparser.ConfigParser) – Settings.
Returns:

Module with helper functions.

snntoolbox.utils.utils.get_abs_path(filepath, config)[source]

Get an absolute path, possibly using current toolbox working dir.

Parameters:
  • filepath (str) – Filename or relative or absolute path. If only the filename is given, file is assumed to be in current working directory (config.get('paths', 'path_wd')). Non-absolute paths are interpreted relative to working dir.
  • config (configparser.ConfigParser) – Settings.
Returns:

path – Absolute path to file.

Return type:

str

snntoolbox.utils.utils.import_script(path, filename)[source]

Import python script independently from python version.

Parameters:
  • path (string) – Path to directory where to load script from.
  • filename (string) – Name of script file.
snntoolbox.utils.utils.binary_tanh(x)[source]

Round a float to -1 or 1.

Parameters:x (float) –
Returns:Integer in {-1, 1}
Return type:int
snntoolbox.utils.utils.binary_sigmoid(x)[source]

Round a float to 0 or 1.

Parameters:x (float) –
Returns:Integer in {0, 1}
Return type:int
snntoolbox.utils.utils.hard_sigmoid(x)[source]
Parameters:x
snntoolbox.utils.utils.binarize_var(w, h=1.0, deterministic=True)[source]

Binarize shared variable.

Parameters:
  • w (keras.backend.Variable) – Weights.
  • h (float) – Values are round to +/-h.
  • deterministic (bool) – Whether to apply deterministic rounding.
Returns:

w – The binarized weights.

Return type:

keras.backend.variable

snntoolbox.utils.utils.binarize(w, h=1.0, deterministic=True)[source]

Binarize weights.

Parameters:
  • w (ndarray) – Weights.
  • h (float) – Values are round to +/-h.
  • deterministic (bool) – Whether to apply deterministic rounding.
Returns:

The binarized weights.

Return type:

ndarray

snntoolbox.utils.utils.reduce_precision(x, m, f)[source]

Reduces precision of x to format Qm.f.

Parameters:
  • x (ndarray) – Input data.
  • m (int) – Number of integer bits.
  • f (int) – Number of fractional bits.
Returns:

x_lp – The input data with reduced precision.

Return type:

ndarray

snntoolbox.utils.utils.reduce_precision_var(x, m, f)[source]

Reduces precision of x to format Qm.f.

Parameters:
  • x (keras.backend.variable) – Input data.
  • m (int) – Number of integer bits.
  • f (int) – Number of fractional bits.
Returns:

x_lp – The input data with reduced precision.

Return type:

keras.backend.variable

snntoolbox.utils.utils.quantized_relu(x, m, f)[source]

Rectified linear unit activation function with precision of x reduced to fixed point format Qm.f.

Parameters:
  • x (keras.backend.variable) – Input data.
  • m (int) – Number of integer bits.
  • f (int) – Number of fractional bits.
Returns:

x_lp – The input data with reduced precision.

Return type:

keras.backend.variable

class snntoolbox.utils.utils.LimitedReLU(cfg)[source]

Bases: tensorflow.python.keras.layers.advanced_activations.ReLU

get_cfg()[source]
set_cfg(cfg)[source]
class snntoolbox.utils.utils.ClampedReLU(threshold=0.1, max_value=None)[source]

Bases: object

Rectified linear unit activation function where values in x below threshold are clamped to 0, and values above max_value are clipped to max_value.

threshold
Type:Optional[float]
max_value
Type:Optional[float]
class snntoolbox.utils.utils.NoisySoftplus(k=0.17, sigma=1)[source]

Bases: object

snntoolbox.utils.utils.wilson_score(p, n)[source]

Confidence interval of a binomial distribution.

See https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval.

Parameters:
  • p (float) – The proportion of successes in n experiments.
  • n (int) – The number of Bernoulli-trials (sample size).
Returns:

Return type:

The confidence interval.

snntoolbox.utils.utils.extract_label(label)[source]

Get the layer number, name and shape from a string.

Parameters:label (str) – Specifies both the layer type, index and shape, e.g. '03Conv2D_3x32x32'.
Returns:
  • layer_num: The index of the layer in the network.
  • name: The type of the layer.
  • shape: The shape of the layer
Return type:tuple[int, str, tuple]
snntoolbox.utils.utils.in_top_k(predictions, targets, k)[source]

Returns whether the targets are in the top k predictions.

# Arguments
predictions: A tensor of shape batch_size x classes and type float32. targets: A tensor of shape batch_size and type int32 or int64. k: An int, number of top elements to consider.
# Returns
A tensor of shape batch_size and type int. output_i is 1 if targets_i is within top-k values of predictions_i
snntoolbox.utils.utils.top_k_categorical_accuracy(y_true, y_pred, k=5)[source]
Parameters:
  • y_true
  • y_pred
  • k
snntoolbox.utils.utils.echo(text)[source]

python 2 version of print(end=’’, flush=True).

snntoolbox.utils.utils.to_list(x)[source]

Normalize a list/tensor to a list.

If a tensor is passed, returns a list of size 1 containing the tensor.

snntoolbox.utils.utils.apply_modifications(model, custom_objects=None)[source]

Applies modifications to the model layers to create a new Graph. For example, simply changing model.layers[idx].activation = new activation does not change the graph. The entire graph needs to be updated with modified inbound and outbound tensors because of change in layer building function.

Parameters:
  • model (keras.models.Model) –
  • custom_objects (dict) –
Returns:

  • The modified model with changes applied. Does not mutate the original
  • model.

snntoolbox.utils.utils.import_configparser()[source]
snntoolbox.utils.utils.is_module_installed(mod)[source]
snntoolbox.utils.utils.get_pearson_coefficients(spikerates_batch, activations_batch, max_rate)[source]

Compute Pearson coefficients.

Parameters:
  • spikerates_batch
  • activations_batch
  • max_rate (float) – Highest spike rate.
Returns:

co

Return type:

list