The Model class

A model consists of neurons without any type of structuring (i.e. no layers) and allows to make predictions based on inputs.

neuralfit.Model(inputs, outputs, size=None, recurrent=False)
Parameters:
  • inputs: int The number of inputs. If e.g. inputs=4, the model accepts inputs with shape (n,4) where n is the number of input samples.
  • outputs: int The number of outputs. If e.g. inputs=7, the model produces outputs with shape (n,7).
  • size: int, optional Starting size of the model. Should be at least as large sum of induts and outputs.
  • recurrent: bool If the model should be recurrent or not. If set to True, backwards connections can be mutated that give the model memory. As a result, giving the same input can result in different outputs.

Model.clear method

Clears the inner state of a recurrent model.
Model.clear()

Model.compile method

Compiles a model to prepare it for the evolution process. Compilation is required before calling the evolve methods.

Model.compile(optimizer='alpha', loss=None, metrics=None, monitors=None)
Parameters:
  • optimizer: str or neuralfit.optimizer
    The neuro-evolution algorithm used to evolve the model withModel.evolve(...) or Model.func_evolve(...). View the list of possible optimizers here.
  • loss: str, optional
    Identifier of the loss function used to asses the performance of the model during evolution. View the list of possible loss functions here. Only needs to be set when using Model.evolve(...).
  • metrics: list of str, optional
    Additional identifiers of loss functions which will be assessed during evolution, but only for tracking purposes. View the list of possible loss functions here. Only needs to be set when using Model.evolve(...).
  • monitors: list of str, optional
    Identifiers of monitor functions which will be tracked during evolution. View the list of possible monitor functions here.

Model.evaluate method

Evaluates the performance of a model on a given dataset using the losses and metrics set during compilation.

Model.evaluate(x, y)

Parameters:

  • x: np.ndarray

    Input samples. Should have shape (n_samples, inputs).

  • y: np.ndarray

    Target outputs. Should have shape (n_samples, outputs).

Returns:

  • list of double

    A list containing the values of the loss and metrics defined during compilation, in order.

Model.evolve method

Evolves the model on a given dataset (i.e. supervised).

Model.evolve(x, y, pop_size=100, batch_size=-1, epochs=100, validation_data=None, verbose=1)
Parameters:
  • x: np.ndarray
    Input samples. Should have shape (n_samples, n_features) when feedforward, and (n_series, n_samples, n_features) when recurrent.
  • y: np.ndarray
    Target outputs. Should have shape (n_samples, n_features) when feedforward, and either (n_series, n_samples, n_features) or (n_series, 1, n_features) when recurrent. When the latter input shape is used, the last timeseries output of the model will be compared to the target value of each series.
  • pop_size: int, optional
    Population size for the evolutionary algorithm.
  • batch_size: int, optional
    Batch size. A lower batch size will apply evolutionary operators based on the performance of genomes on a smaller part of the dataset. Generally, this leads to faster convergence. However, ensure that your batch size is sufficiently large to represent the dataset. Otherwise, evolution might not converge.
  • epochs: int, optional
    The number of times the entire dataset is passed through the population.
  • validation_data: tuple, optional
    A tuple containing the validation data in the form (x_val, y_val). Note that unlike the training data, the validation data is only evaluated on the best performing genome at the end of each epoch.
  • verbose: int, optional
    The verbosity level. For 0 nothing will be shown. For 1 a progress bar will be shown for each epoch. For 2 only one line will be printed per epoch.

Model.func_evolve method

Evolves the model on a given dataset (i.e. supervised). If applied to a recurrent model, make sure to use genome.clear() in the function to reset the inner state before each series of inputs.
Model.func_evolve(train_func, pop_size=100, epochs=100, verbose=1, validation_func=None)
Parameters:
  • train_func: function
    A function that has only one mandatory input: a list of genomes (similar to models). These genomes can be used for predictions on arbitrary problems. The function must return an numpy.ndarray of the same size as the list of genomes, containing their loss values. Visit the examples for more information.
  • pop_size: int, optional
    Population size for the evolutionary algorithm.
  • epochs: int, optional
    The number of times the entire dataset is passed through the population.
  • verbose: int, optional
    The verbosity level. For 0 nothing will be shown. For 1 a progress bar will be shown for each epoch. For 2 only one line will be printed per epoch.
  • validation_func: function, optional
    A validation function, similar to train_func. The most important difference is that this function should only accept one genome as input and return a double instead. Visit the examples for more information.

Model.get_connections method

Returns a list containing all the connections in the model.

Model.get_connections()
Returns:
  • list of tuple
    The first element of each tuple is the index of the sending neuron, while the second element is the index of the receiving neuron. The third element of the tuple is a dict containing the weight of the connection.

Model.get_nodes method

Returns a list containing all the neurons in the model.

Model.get_nodes()
Returns:
  • list of tuple
    The first element of each tuple is the index of neuron. The second element of the tuple is a dict containing the bias and activation function of the neuron.

Model.predict method

Use the model to predict the output on given (possibly unseen) input data. Use this method in defining your custom fitness function for Model.func_evolve.
Model.predict(x)
Parameters:
  • x: np.ndarray
    Input samples. Should have shape (n_samples, inputs).
Returns:
  • np.ndarray
    An array of outputs with shape (n_samples, outputs).

Model.save method

Saves the model in the sparse NeuralFit (.nf) format. Requires less memory than converting to Keras and then saving in for instance the .hdf5 format.
Model.save(path)
Parameters:
  • path: str
    Path to save the model to.

Model.to_keras method

Convert the model to an equivalent Keras model. Note that there can be very small differences in the outputs of the NeuralFit and equivalent Keras model. Note that this method will automatically import Keras if not yet done.

Model.to_keras()

Returns:

  • tf.keras.Model

    The equivalent Keras model.