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_sampledirectly.Parameters: Returns: samples – There are
numsamples 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
datadictionary topath.A
TypeErroris raised if objects indataare 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.
- 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 (
-
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: - filepath (str) – Filename or relative or absolute path. If only the filename is given,
file is assumed to be in current working directory
(
-
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.binarize_var(w, h=1.0, deterministic=True)[source]¶ Binarize shared variable.
Parameters: Returns: w – The binarized weights.
Return type: keras.backend.variable
-
snntoolbox.utils.utils.binarize(w, h=1.0, deterministic=True)[source]¶ Binarize weights.
Parameters: Returns: The binarized weights.
Return type: ndarray
-
snntoolbox.utils.utils.reduce_precision(x, m, f)[source]¶ Reduces precision of
xto formatQm.f.Parameters: 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
xto formatQm.f.Parameters: 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
xreduced to fixed point formatQm.f.Parameters: Returns: x_lp – The input data with reduced precision.
Return type: keras.backend.variable
-
class
snntoolbox.utils.utils.ClampedReLU(threshold=0.1, max_value=None)[source]¶ Bases:
objectRectified linear unit activation function where values in
xbelowthresholdare clamped to 0, and values abovemax_valueare clipped tomax_value.
-
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: 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
targetsare in the topkpredictions.- # 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.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 activationdoes 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.