Skip to content

Commit

Permalink
Merge pull request #287 from ahmedfgad/master
Browse files Browse the repository at this point in the history
Keep up to date
  • Loading branch information
ahmedfgad authored Apr 14, 2024
2 parents 8bbc4c8 + 0e8be9d commit c16cdcc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
33 changes: 22 additions & 11 deletions examples/KerasGA/image_classification_CNN.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import pygad.kerasga
import numpy
import pygad
import gc


def fitness_func(ga_instanse, solution, sol_idx):
global data_inputs, data_outputs, keras_ga, model
Expand All @@ -11,27 +13,33 @@ def fitness_func(ga_instanse, solution, sol_idx):
data=data_inputs)

cce = tensorflow.keras.losses.CategoricalCrossentropy()
solution_fitness = 1.0 / (cce(data_outputs, predictions).numpy() + 0.00000001)
solution_fitness = 1.0 / \
(cce(data_outputs, predictions).numpy() + 0.00000001)

return solution_fitness


def on_generation(ga_instance):
print(f"Generation = {ga_instance.generations_completed}")
print(f"Fitness = {ga_instance.best_solution()[1]}")
gc.collect() # can useful for not exploding the memory usage on notebooks (ipynb) freeing memory


# Build the keras model using the functional API.
input_layer = tensorflow.keras.layers.Input(shape=(100, 100, 3))
conv_layer1 = tensorflow.keras.layers.Conv2D(filters=5,
kernel_size=7,
activation="relu")(input_layer)
max_pool1 = tensorflow.keras.layers.MaxPooling2D(pool_size=(5,5),
max_pool1 = tensorflow.keras.layers.MaxPooling2D(pool_size=(5, 5),
strides=5)(conv_layer1)
conv_layer2 = tensorflow.keras.layers.Conv2D(filters=3,
kernel_size=3,
activation="relu")(max_pool1)
flatten_layer = tensorflow.keras.layers.Flatten()(conv_layer2)
dense_layer = tensorflow.keras.layers.Dense(15, activation="relu")(flatten_layer)
output_layer = tensorflow.keras.layers.Dense(4, activation="softmax")(dense_layer)
flatten_layer = tensorflow.keras.layers.Flatten()(conv_layer2)
dense_layer = tensorflow.keras.layers.Dense(
15, activation="relu")(flatten_layer)
output_layer = tensorflow.keras.layers.Dense(
4, activation="softmax")(dense_layer)

model = tensorflow.keras.Model(inputs=input_layer, outputs=output_layer)

Expand All @@ -47,13 +55,15 @@ def on_generation(ga_instance):
data_outputs = tensorflow.keras.utils.to_categorical(data_outputs)

# Prepare the PyGAD parameters. Check the documentation for more information: https://pygad.readthedocs.io/en/latest/README_pygad_ReadTheDocs.html#pygad-ga-class
num_generations = 200 # Number of generations.
num_parents_mating = 5 # Number of solutions to be selected as parents in the mating pool.
initial_population = keras_ga.population_weights # Initial population of network weights.
num_generations = 200 # Number of generations.
# Number of solutions to be selected as parents in the mating pool.
num_parents_mating = 5
# Initial population of network weights.
initial_population = keras_ga.population_weights

# Create an instance of the pygad.GA class
ga_instance = pygad.GA(num_generations=num_generations,
num_parents_mating=num_parents_mating,
ga_instance = pygad.GA(num_generations=num_generations,
num_parents_mating=num_parents_mating,
initial_population=initial_population,
fitness_func=fitness_func,
on_generation=on_generation)
Expand All @@ -62,7 +72,8 @@ def on_generation(ga_instance):
ga_instance.run()

# After the generations complete, some plots are showed that summarize how the outputs/fitness values evolve over generations.
ga_instance.plot_fitness(title="PyGAD & Keras - Iteration vs. Fitness", linewidth=4)
ga_instance.plot_fitness(
title="PyGAD & Keras - Iteration vs. Fitness", linewidth=4)

# Returning the details of the best solution.
solution, solution_fitness, solution_idx = ga_instance.best_solution()
Expand Down
2 changes: 1 addition & 1 deletion examples/example_custom_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def crossover_func(parents, offspring_size, ga_instance):
parent1 = parents[idx % parents.shape[0], :].copy()
parent2 = parents[(idx + 1) % parents.shape[0], :].copy()

random_split_point = numpy.random.choice(range(offspring_size[0]))
random_split_point = numpy.random.choice(range(offspring_size[1]))

parent1[random_split_point:] = parent2[random_split_point:]

Expand Down

0 comments on commit c16cdcc

Please sign in to comment.