Evolution¶
Here there can be found the classes responsible of the evolution of the netwroks. This is a key part of the library, in charge of initializing and evolving the desired networks, using other class to achieve it. Is also important because is the one that the user will make use of.
Evolving class is the main class and the one in charge of evolving model descriptors.
Those descriptors and the atributes specified in the initialization will generate
network to evolve; so the stepts to follow are:
Initialize this class with all the desired qualities.
Call
evolve()function.
DescriptorContainer is just an auxiliar class to help Evolving. It is
used with the DEAP library and it represent the individul to be evolved.
-
class
deatf.evolution.DescriptorContainer(desc_list)¶ Bases:
objectAuxiliar class for DEAP algorithm. This object will represent the individual that will be evolved in the Genetic Algorithm with DEAP. It contains a dictionary with the networks descriptors and the hyperparameters that will be evolved. In this dictionary ‘nX’ will be the key for each network descriptor (where X is the number of the descripor, starting from 0) and ‘hypers’ will be the key for the hyperparameters. This class does not require a fitness attribute because it will be added later by the DEAP’s creator.
- Parameters
desc_list – A dictionary containing the descriptors of the networks and hyperparameters that will be evolved with DEAP algorithm.
-
class
deatf.evolution.Evolving(desc_list, x_trains, y_trains, x_tests, y_tests, evaluation, n_inputs, n_outputs, batch_size, population, generations, iters, lrate=0.01, sel='best', max_num_layers=10, max_num_neurons=100, max_filter=4, max_stride=3, seed=None, cxp=0, mtp=1, compl=False, dropout=False, batch_norm=False, evol_kwargs={}, sel_kwargs={}, evol_alg='mu_plus_lambda', hyperparameters={}, custom_mutations={}, add_obj=0)¶ Bases:
objectThis is the main class and contains all the needed functions and atributes in order to realize the evolution. As is can be seen this class has many atributes to initialice, this is due to its high cutomization. Many of the parameters (like evol_kwargs or sel_kwargs) are not neccesary unless custom evoluationary or selection functions are used.
In order to facilitate the use of this class, here is a table with the atributes used in the initialization of it. Is divided in three columns:
Required atributes: atributes that always have to be declared, because otherwise the initialization will be inclompleted and it will give an error.
Predefined atributes: atributes that are necesary and have to be declared but they already predefined with a value and there is no need to define them (it will run, but with the predefined values), but they can be defined by the user and custom the execution. For example, evol_alg is intialize with ‘mu_plus_lambda’ algorithm and if Evolving class is created without asigning a value to ‘evol_alg it will run with it; but it can be declared and defined with ‘mu_comm_lambda’ or ‘simple’ algorithms.
Optional atributes: atributes that are not needed, it can be initialiced without defining these atributes. Even so, they can be defined and the initialization will be more custom and with more options.
Required attributes
Predefined attributes
Optional attributes
n_inputs
evol_alg
evol_kwargs
n_outputs
sel
sel_kwargs
desc_list
max_num_neurons
seed
x_trains
max_num_layers
hyperpatameters
y_trains
max_filter
custom_mutations
x_tests
max_stride
y_tests
dropout
population
batch_norm
generations
lrate
iters
cxp
batch_size
mtp
evaluation
add_obj compl
By using the described atributes in the initialization and some functions defined in this class, an Evolving object is created. This is the first step in the evolving process and the most relevant one, because here is where decisions are taken. Then by calling
evolve()function everything is done automaticaly.- Parameters
n_inputs – List of lists with the dimensions of the input sizes of each network.
n_outputs – List of lists with the dimensions of the output sizes of each network.
max_num_layers – Maximum number of layers.
max_num_neurons – Maximum number of neurons in all layers (only relevant with MLP descriptors).
max_filter – Maximum size of filter (only relevant with CNN and TCNN descriptors).
max_stride – Maximum size of stride (only relevant with CNN and TCNN descriptors).
descriptors – List with all the network descriptors that are wanted to be evolved.
evaluation – Function for evaluating the model. A string in simple cases (‘MSE’ or ‘XEntropy’) and one defined by the user in complex cases.
batch_size – Number of samples per batch are used during training process.
lrate – Learning rate used during training process.
iters – Number of iterations that each model is trained.
train_inputs – Dictionary with the trainig input features for each network. The key is ‘iX’ being X the network for which the data is and the value is the actual data.
train_outputs – Dictionary with the trainig output labels for each network. The key is ‘oX’ being X the network for which the data is and the value is the actual data.
test_inputs – Dictionary with the testing input features for each network. The key is ‘iX’ being X the network for which the data is and the value is the actual data.
test_outputs – Dictionary with the testing output labels for each network. The key is ‘oX’ being X the network for which the data is and the value is the actual data.
complex – A boolean valeu that indicates if the network that is going to be evaluated is complex or not. The conditions for being complex are: if is indicated in initialization, if user defined evalutaion method is used, if more than one network descriptor are evolved or if the network descriptor to be evolved is different from a MLP. It is complex if occurs any of these conditions; otherwise, if none of the condition are true, is is considered simple (not complex).
toolbox – Object Toolbox() from deap.base in the DEAP algorithm.
selection – String that indicates the selection method used in the evolution. Possibilities are: ‘best’, ‘tournament’, ‘roulette’, ‘random’ or ‘nsga2’.
evol_alg – String that indicates the evolution algorithm that will be used. Possibilities are: ‘simple’, ‘mu_plus_lambda’ or ‘mu_comm_lambda’.
evol_kwargs – Dictionary with parameters for the main DEAP function. The keys for that parameters are:’ mu’, ‘lambda’, ‘cxpb’ or ‘mutpb’. Being mu and lambda to float values for the mu_plus_lambda algorithm from DEAP. And cxpb and mutpb the crossover and mutation probabliites respectively.
cXp – Float value indicating the crossover probability. It will be 0 if there is only one descriptor to be evolved.
mtp – Float value indicating the crossover probability. It will be 1 if there is only one descriptor to be evolved.
generations – Number of generations that the evolution algorithm will be running.
population_size – Number of individuals that will be evaluated in each generation of the evolution algorithm.
hypers – Hyperparameters to be evolved in the algorithm (e.g., optimizer, batch size).
-
data_save(x_trains, y_trains, x_tests, y_tests)¶ Load data given by parameters in the atributes of data from the class Evolving, it initialices those atributes (train_inputs, train_outputs, test_inputs and test_outputs). That data can be given in dictionary or list format, if is given as dictionary inputs’ key must be ‘iX’ and outputs’ ‘oX’ (being X the number of data it is, first data will hava ‘i0’ and ‘o0’).
- Parameters
x_trains – Features data for training.
y_trains – Labels data for training.
x_tests – Features data for testing.
y_tests – Labels data for testing.
-
define_evaluation(evaluation)¶ Define the evaluation function. It accepts an string (‘MSE’ or ‘XEntropy) and it will use the predifined functions from TensorFlow; this will be done in simple cases. Otherwise, the evaluation function will be defined and passed by parameter by the user. If is defined by the user it has to folloe the next structure:
- It must have the next parameters in the following order:
nets (dictionary with the networks descriptors, where key ‘n0’ has the first network descriptor).
train_inputs (data for the training).
train_outputs (expected outputs for the training).
batch_size (size of the batch that is going to be taken from the train data).
iters (number of iterations that each network will be trained).
test_inputs (data for testing).
test_outputs (expected outputs for the testing).
hyperparameters (dictionary with the hyperparameters like ‘optimizer’ or ‘lrate’ that also being evolved).
The output must be: value, . It must be like that because it has to receive more than one output. The value is the fitness or evaluation value calculated in the function.
- Parameters
evaluation – Evaluation function. Either string (predefined) or customized by the user.
-
define_selection(selection)¶ Define the selection method for the evolution algorithm. It can be used a predefined method from DEAP library (‘best’, ‘tournament’, ‘roulette’, ‘random’ and ‘nsga2’) or a selection method defined by the user. If is defined by the user it has to follow the next structure: as parameters recieve at least individuals (list with all the individuals) and k (the number of individuals that will be selected) and as output it will return the list with the selected individuals. Apart from the individuals and the number of selected ones, more parameters can be used, thses should be defined in ‘sel_kwargs’ in the initialization.
- Parameters
selection – Selection function, Either string that indicates the selection method (‘best’, ‘tournament’, ‘roulette’, ‘random’ or ‘nsga2’) or customized by the user.
-
define_evolving(evol_alg)¶ Define the evolutionary algorithm for the evolution. It uses predefined algorithms from DEAP library (‘simple’, ‘mu_plus_lambda’ or ‘mu_comm_lambda’) or a custom function defined by the user. This custom evolutionary algorithm function must follow the next structure:
At least it has to receive the following parameters: population, toolbox, cxpb, mutpb, ngen, stats=None, halloffame=None.
More parameters can be used, they will have to be defined in evol_kwargs parameter in the initialization.
It has to return population (the final population) and logbook (object from deap.tools.Logbook with the statistics of the evolution).
During the procces of the defined function, other functions defined in the toolbox can be used (selection, muatation or evalutaion).
- Parameters
evol_alg – Evolutionary algorithm. It can be either a string that indicates the evolutionary algorithm (‘simple’, ‘mu_plus_lambda’ or ‘mu_comm_lambda’) or a defined evolutionary function by the user.
-
is_complex(compl, evaluation, hyperparameters)¶ Determines if the case that will be evolved is a simple or a complex case. This will affect in the evaluation method used
simple_eval()orcomplex_eval().- Parameters
compl – A boolean value that directly indicates if is a simple or complex case.
evaluation – Evaluation parameter that is used for initialization (string or function).
hyperparameters – Dictionary with the hyperparameters to ve evolved.
- Returns
True boolean value if is a complex case and False boolean value if is a simple case.
-
initialize_deap(sel, sel_kwargs, batch_norm, dropout, custom_mutations, add_obj)¶ Initialize DEAP function and atributes in order to be ready for evolutionary algorithm. In this function all the other functions that have been defined in
define_evaluation(),define_evolving()anddefine_selection()will be added to the toolbox of DEAP. Also here the individualsDescriptorContainerwill be assigned as individuals to be evolved.- Parameters
sel – Selection method.
sel_kwargs – Hyperparameters for the selection methods (e.g., size of the tournament if that method is selected).
batch_norm – Whether the evolutive process includes batch normalization in the networks or not.
dropout – Whether the evolutive process includes dropout in the networks or not.
custom_mutations – List with the desired mutations to be applied.
add_obj – Number of additional objectives.
-
evolve()¶ Function that actualy applies the evolutionary algorithm. Using all the information provided in the initialization of the class, this function does the evolution. It will print the mean, standard, minimum and maximum values obtained form the individuals in each generation. Finally, it return the individuals from the last generation, the stats and the best individuals found during the algorithm.
- Returns
The last generation, a log book (stats) and the hall of fame (the best individuals found).
-
init_individual(init_ind, batch_norm, dropout)¶ Initializes the individual that is going to be used and evolved during the evolutionary algorithm. That individual will be used as dictionary with the string network id as key and the network descriptor as a value, i.e., {“net_id”: net_desc}. In simple case there will only be one network that is a MLP, in complex cases more than one network can be evaluated.
- Parameters
init_ind – DEAP function for transforming a network descriptor, or a list of descriptors + evolvable hyperparameters into a DEAP individual.
batch_norm – A boolean value that indicates whether batch normalization is included into the evolution or not.
dropout – A boolean value that incidates whether dropout is included into the evolution or not.
- Returns
A DEAP individual totaly initialized.
-
eval_individual(individual)¶ Function used for evaluating a DEAP individual during the evolutionary algorithm. This is the registered function for evalution and is an auxiliar function because it only does another calling depending on the type of evaluation (
simple_eval()orcomplex_eval()).- Parameters
individual – DEAP individual.
- Returns
Value obtained from the evaluation.
-
simple_eval(individual)¶ Evaluation in the simple case. Function for evolving a single individual. No need of the user providing a evaluation function.
- Parameters
individual – DEAP individual
- Returns
Value obtained from the evaluation.
-
complex_eval(individual)¶ Evaluation in the complex case. Function for evolving individuals in a complex case. The user must have implemented the training and evaluation functions.
- Parameters
individual – DEAP individual
- Returns
Value obtained from the evaluation.
-
deatf.evolution.mutations(hypers, batch_norm, dropout, custom_mutations, individual)¶ Mutation operators for individuals. It can be affected any network or hyperparameter. Depending on the type of network that will suffer the mutation, this function will create a different object from
deatf.mutation.Mutation.- Parameters
hypers – Hyperparameters not included in the networks to be evolved.
batch_normalization – Whether batch normalization is part of the evolution or not.
dropout – Whether dropout is part of the evolution or not.
individual – DEAP individual. Contains a dict where the keys are the components of the model.
- Returns
Mutated version of the DEAP individual.
-
deatf.evolution.cross(init_ind, ind1, ind2)¶ Crossover operator for individuals. Cannot be applied in the simple case, as it randomly interchanges model components.
- Parameters
init_ind – DEAP function for initializing dicts (in this case) as DEAP individuals.
ind1 – 1st individual to be crossed (first parent).
ind2 – 2st individual to be crossed (second parent).
- Returns
Two new DEAP individuals, the crossed versions of the incoming parameters (the offspring).