pygad.kerasga Module¶
This section of the documentation discusses the pygad.kerasga module.
The pygad.kerasga module has a helper class and 2 functions to train Keras models using the genetic algorithm (PyGAD). The Keras model can be built using either the Sequential Model or the Functional API.
The contents of this module are:
KerasGA: A class for creating an initial population of all parameters in the Keras model.model_weights_as_vector(): A function to reshape the Keras model weights to a single vector.model_weights_as_matrix(): A function to restore the Keras model weights from a vector.predict(): A function to make predictions based on the Keras model and a solution.
More details are given in the next sections.
Steps Summary¶
The steps used to train a Keras model using PyGAD are summarized as follows:
Create a Keras model.
Create an instance of the
pygad.kerasga.KerasGAclass.Prepare the training data.
Build the fitness function.
Create an instance of the
pygad.GAclass.Run the genetic algorithm.
Create Keras Model¶
Before discussing training a Keras model using PyGAD, the first thing to do is to create the Keras model.
According to the Keras library documentation, there are 3 ways to build a Keras model:
PyGAD supports training the models created either using the Sequential Model or the Functional API.
Here is an example of a model created using the Sequential Model.
import tensorflow.keras
input_layer = tensorflow.keras.layers.Input(3)
dense_layer1 = tensorflow.keras.layers.Dense(5, activation="relu")
output_layer = tensorflow.keras.layers.Dense(1, activation="linear")
model = tensorflow.keras.Sequential()
model.add(input_layer)
model.add(dense_layer1)
model.add(output_layer)
This is the same model created using the Functional API.
input_layer = tensorflow.keras.layers.Input(3)
dense_layer1 = tensorflow.keras.layers.Dense(5, activation="relu")(input_layer)
output_layer = tensorflow.keras.layers.Dense(1, activation="linear")(dense_layer1)
model = tensorflow.keras.Model(inputs=input_layer, outputs=output_layer)
Feel free to add the layers of your choice.
pygad.kerasga.KerasGA Class¶
The pygad.kerasga module has a class named KerasGA for creating an initial population for the genetic algorithm based on a Keras model. The constructor, methods, and attributes within the class are discussed in this section.
__init__()¶
The pygad.kerasga.KerasGA class constructor accepts the following parameters:
model: An instance of the Keras model.num_solutions: Number of solutions in the population. Each solution has different parameters of the model.
Instance Attributes¶
All parameters in the pygad.kerasga.KerasGA class constructor are used as instance attributes in addition to adding a new attribute called population_weights.
Here is a list of all instance attributes:
modelnum_solutionspopulation_weights: A nested list holding the weights of all solutions in the population.
Methods in the KerasGA Class¶
This section discusses the methods available for instances of the pygad.kerasga.KerasGA class.
create_population()¶
The create_population() method creates the initial population of the genetic algorithm as a list of solutions where each solution represents different model parameters. The list of networks is assigned to the population_weights attribute of the instance.
Functions in the pygad.kerasga Module¶
This section discusses the functions in the pygad.kerasga module.
pygad.kerasga.model_weights_as_vector()¶
The model_weights_as_vector() function accepts a single parameter named model representing the Keras model. It returns a vector holding all model weights. The reason for representing the model weights as a vector is that the genetic algorithm expects all parameters of any solution to be in a 1D vector form.
This function filters the layers based on the trainable attribute to see whether the layer weights are trained or not. For each layer, if its trainable=False, then its weights will not be evolved using the genetic algorithm. Otherwise, it will be represented in the chromosome and evolved.
The function accepts the following parameters:
model: The Keras model.
It returns a 1D vector holding the model weights.
pygad.kerasga.model_weights_as_matrix()¶
The model_weights_as_matrix() function accepts the following parameters:
model: The Keras model.weights_vector: The model parameters as a vector.
It returns the restored model weights after reshaping the vector.
pygad.kerasga.predict()¶
The predict() function makes a prediction based on a solution. It accepts the following parameters:
model: The Keras model.solution: The solution evolved.data: The test data inputs.batch_size=None: The batch size (i.e. number of samples per step or batch).verbose=None: Verbosity mode.steps=None: The total number of steps (batches of samples).
Check documentation of the Keras Model.predict() method for more information about the batch_size, verbose, and steps parameters.
It returns the predictions of the data samples.
Examples¶
This section gives the complete code of some examples that build and train a Keras model using PyGAD. Each subsection builds a different network.