BasePlasticity

class BasePlasticity

Abstract type representing an encoder model, i.e. a neural network ables to memorize all the input data giving in output an encoding array of features for each input data.

This class is the base class for specialized models. The derived classes have to implement an appropriated version of the private member function “weights_update”, i.e. the function responsibles for the update of the weights matrix. A second member which could be specialized is the “normalize_weights” private member which is responsible of the normalization of the weights matrix before the fit function.

Subclassed by BCM, Hopfield

Public Functions

BasePlasticity()

Default constructor.

BasePlasticity(const int &outputs, const int &batch_size, int activation = transfer_t::linear, update_args optimizer = update_args(optimizer_t::sgd), weights_initialization weights_init = weights_initialization(weights_init_t::normal), int epochs_for_convergency = 1, float convergency_atol = 0.01)

Construct the object using the list of training parameters.

The constructor follows the same nomenclature of the Python counterpart. This is the abstract type for the plasticity model.

Note

Overriding this class you can specify the weights-update rule to use in the training.

Parameters
  • outputs – Number of hidden units.

  • batch_size – Size of the minibatch.

  • activation – Index of the activation function.

  • optimizer – update_args Optimizer object (default=SGD algorithm).

  • weights_init – weights_initialization object (default=uniform initialization in [-1, 1]).

  • epochs_for_convergency – Number of stable epochs requested for the convergency.

  • convergency_atol – Absolute tolerance requested for the convergency.

BasePlasticity(const BasePlasticity &b)

Copy constructor.

The copy constructor provides a deep copy of the object, i.e. all the arrays are copied and not moved.

Parameters

bBasePlasticity object

BasePlasticity &operator=(const BasePlasticity &b)

Copy operator.

The operator performs a deep copy of the object and if there are buffers already allocated, the operatore deletes them and then re-allocates an appropriated portion of memory.

Parameters

bBasePlasticity object

~BasePlasticity() = default

Destructor.

Completely delete the object and release the memory of the arrays.

template<class Callback = std::function<void(BasePlasticity*)>>
void fit(float *X, const int &n_samples, const int &n_features, const int &num_epochs, int seed = 42, Callback callback = [](BasePlasticity *) -> void {})

Train the model/encoder.

The model computes the weights and thus the encoded features using the given plasticity rule. The signature of the function is totally equivalent to the the Python counterpart except by the pointer arrays which require the dimension size as extra parameters.

Note

This function must be called before the predict member-function. A check is performed internally to ensure it.

Parameters
  • X – array in ravel format of the input variables/features

  • n_samples – dimension of the X matrix, i.e. the number of rows

  • n_features – dimension of the X matrix, i.e. the number of cols

  • num_epochs – Number of epochs for model convergency.

  • seed – Random seed number for the batch subdivisions.

  • callback – Callback function to call at each batch evaluation.

Template Parameters

Callback – void lambda function which can use member variables.

template<class Callback = std::function<void(BasePlasticity*)>>
void fit(const Eigen::MatrixXf &X, const int &num_epochs, int seed = 42, Callback callback = [](BasePlasticity *) -> void {})

Train the model/encoder.

Proxy function for a user interface compatible with Eigen matrix.

Parameters
  • X – Eigen matrix of the input variables/features.

  • num_epochs – Number of epochs for model convergency.

  • seed – Random seed number for the batch subdivisions.

  • callback – Callback function to call at each batch evaluation.

Template Parameters

Callback – void lambda function which can use member variables.

float *predict(float *X, const int &n_samples, const int &n_features)

Predict the model/encoder.

The model computes the weights and thus the encoded features using the given plasticity rule. The signature of the function is totally equivalent to the the Python counterpart except by the pointer arrays which require the dimension size as extra parameters.

Note

This function must be called before the predict member-function. A check is performed internally to ensure it.

Parameters
  • X – array in ravel format of the input variables/features.

  • n_samples – dimension of the X matrix, i.e. the number of rows.

  • n_features – dimension of the X matrix, i.e. the number of cols.

Returns

The array of encoded features.

float *predict(const Eigen::MatrixXf &X)

Predict the model/encoder.

Proxy function for a user interface compatible with Eigen matrix.

Parameters

X – Eigen matrix of the input variables/features.

Returns

The array of encoded features.

void save_weights(const std::string &filename)

Save the current weight matrix.

The weights matrix is saved in binary format. The first value of the file is an integer corresponding to the number of weights (rows x cols) of the weight matrix, followed by the (float) weight matrix in ravel format.

Parameters

filename – Filename or path where the file is saved.

void load_weights(const std::string &filename)

Load the current weight matrix.

The weights matrix is loaded according to the format specified in the save_weights function, i.e. the first first value of the file is an integer corresponding to the number of weights (rows x cols) of the weight matrix, followed by the (float) weight matrix in ravel format.

Parameters

filename – Filename or path of the weight.

float *get_weights()

Get the weight matrix as pointer array.

This function is just an utility for the Cython wrap of the object.

Returns

The weights matrix in ravel format.

Public Members

Eigen::MatrixXf weights

array-matrix of weights