evolutionary_keras package

Module contents

Submodules

Module: evolutionary_keras.optimizers.NGA

class evolutionary_keras.optimizers.NGA(*args: Any, **kwargs: Any)

Bases: evolutionary_keras.optimizers.EvolutionaryStrategies

The Nodal Genetic Algorithm (NGA) is similar to the regular GA, but this time a number of nodes (defined by the mutation_rate variable) are selected at random and only the weights and biases corresponding to the selected nodes are mutated by adding normally distributed values with normal distrubtion given by sigma.

Parameters
  • sigma_init (int) – Allows adjusting the original sigma

  • population_size (int) – Number of mutants to be generated per iteration

  • mutation_rate (float) – Mutation rate

get_config()
get_shape()

Study the model to get the shapes of all trainable weight as well as the number of nodes. It also saves a reference to the non-trainable weights in the system.

Returns

`weight_shapes`

Return type

a list of the shapes of all trainable weights

create_mutants(change_both_wb=True)

Takes the current state of the network as the starting mutant and creates a new generation by performing random nodal mutations. By default, from a layer dense layer, only weights or biases will be mutated. In order to mutate both set change_both_wb to True

evaluate_mutants(mutants, x=None, y=None, verbose=0)

Evaluates all mutants of a generation and select the best one.

Parameters

mutants (list of all mutants for this generation) –

Returns

  • `loss` (loss of the best performing mutant)

  • `best_mutant` (best performing mutant)

run_step(x, y)

Wrapper to run one single step of the optimizer

Module: evolutionary_keras.optimizers.CMA

class evolutionary_keras.optimizers.CMA(*args: Any, **kwargs: Any)

Bases: evolutionary_keras.optimizers.EvolutionaryStrategies

From http://cma.gforge.inria.fr/: “The CMA-ES (Covariance Matrix Adaptation Evolution Strategy) is an evolutionary algorithm for difficult non-linear non-convex black-box optimisation problems in continuous domain.” The work-horse of this class is the cma package developed and maintained by Nikolaus Hansen (see https://pypi.org/project/cma/), this class allows for convenient implementation within the keras environment.

Parameters
  • sigma_init (int) – Allows adjusting the initial sigma

  • population_size (int) – Number of mutants to be generated per iteration

  • target_value (float) – Stops the minimizer if the target loss is achieved

  • max_evaluations (int) – Maximimum total number of mutants tested during optimization

get_config()
on_compile(model)

Function to be called by the model during compile time. Register the model model with the optimizer.

get_shape()

Gets the shape of the weights to train

weights_per_layer()

‘weights_per_layer’ creates ‘self.lengt_flat_layer’ which is a list conatining the numer of weights in each layer of the network.

flatten()

‘flatten’ returns a 1 dimensional list of all weights in the keras model.

undo_flatten(flattened_weights)

‘undo_flatten’ does the inverse of ‘flatten’: it takes a 1 dimensional input and returns a weight structure that can be loaded into the model.

run_step(x, y)

Wrapper to the optimizer