22import pygad .kerasga
33import numpy
44import pygad
5+ import gc
6+
57
68def fitness_func (ga_instanse , solution , sol_idx ):
79 global data_inputs , data_outputs , keras_ga , model
@@ -11,27 +13,33 @@ def fitness_func(ga_instanse, solution, sol_idx):
1113 data = data_inputs )
1214
1315 cce = tensorflow .keras .losses .CategoricalCrossentropy ()
14- solution_fitness = 1.0 / (cce (data_outputs , predictions ).numpy () + 0.00000001 )
16+ solution_fitness = 1.0 / \
17+ (cce (data_outputs , predictions ).numpy () + 0.00000001 )
1518
1619 return solution_fitness
1720
21+
1822def on_generation (ga_instance ):
1923 print (f"Generation = { ga_instance .generations_completed } " )
2024 print (f"Fitness = { ga_instance .best_solution ()[1 ]} " )
25+ gc .collect () # can useful for not exploding the memory usage on notebooks (ipynb) freeing memory
26+
2127
2228# Build the keras model using the functional API.
2329input_layer = tensorflow .keras .layers .Input (shape = (100 , 100 , 3 ))
2430conv_layer1 = tensorflow .keras .layers .Conv2D (filters = 5 ,
2531 kernel_size = 7 ,
2632 activation = "relu" )(input_layer )
27- max_pool1 = tensorflow .keras .layers .MaxPooling2D (pool_size = (5 ,5 ),
33+ max_pool1 = tensorflow .keras .layers .MaxPooling2D (pool_size = (5 , 5 ),
2834 strides = 5 )(conv_layer1 )
2935conv_layer2 = tensorflow .keras .layers .Conv2D (filters = 3 ,
3036 kernel_size = 3 ,
3137 activation = "relu" )(max_pool1 )
32- flatten_layer = tensorflow .keras .layers .Flatten ()(conv_layer2 )
33- dense_layer = tensorflow .keras .layers .Dense (15 , activation = "relu" )(flatten_layer )
34- output_layer = tensorflow .keras .layers .Dense (4 , activation = "softmax" )(dense_layer )
38+ flatten_layer = tensorflow .keras .layers .Flatten ()(conv_layer2 )
39+ dense_layer = tensorflow .keras .layers .Dense (
40+ 15 , activation = "relu" )(flatten_layer )
41+ output_layer = tensorflow .keras .layers .Dense (
42+ 4 , activation = "softmax" )(dense_layer )
3543
3644model = tensorflow .keras .Model (inputs = input_layer , outputs = output_layer )
3745
@@ -47,13 +55,15 @@ def on_generation(ga_instance):
4755data_outputs = tensorflow .keras .utils .to_categorical (data_outputs )
4856
4957# Prepare the PyGAD parameters. Check the documentation for more information: https://pygad.readthedocs.io/en/latest/README_pygad_ReadTheDocs.html#pygad-ga-class
50- num_generations = 200 # Number of generations.
51- num_parents_mating = 5 # Number of solutions to be selected as parents in the mating pool.
52- initial_population = keras_ga .population_weights # Initial population of network weights.
58+ num_generations = 200 # Number of generations.
59+ # Number of solutions to be selected as parents in the mating pool.
60+ num_parents_mating = 5
61+ # Initial population of network weights.
62+ initial_population = keras_ga .population_weights
5363
5464# Create an instance of the pygad.GA class
55- ga_instance = pygad .GA (num_generations = num_generations ,
56- num_parents_mating = num_parents_mating ,
65+ ga_instance = pygad .GA (num_generations = num_generations ,
66+ num_parents_mating = num_parents_mating ,
5767 initial_population = initial_population ,
5868 fitness_func = fitness_func ,
5969 on_generation = on_generation )
@@ -62,7 +72,8 @@ def on_generation(ga_instance):
6272ga_instance .run ()
6373
6474# After the generations complete, some plots are showed that summarize how the outputs/fitness values evolve over generations.
65- ga_instance .plot_fitness (title = "PyGAD & Keras - Iteration vs. Fitness" , linewidth = 4 )
75+ ga_instance .plot_fitness (
76+ title = "PyGAD & Keras - Iteration vs. Fitness" , linewidth = 4 )
6677
6778# Returning the details of the best solution.
6879solution , solution_fitness , solution_idx = ga_instance .best_solution ()
0 commit comments