Skip to content

Commit ee643a9

Browse files
committed
More examples
1 parent 0b5ab78 commit ee643a9

File tree

216 files changed

+1113
-256
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

216 files changed

+1113
-256
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import tensorflow.keras
2+
import pygad.kerasga
3+
import numpy
4+
import pygad
5+
6+
def fitness_func(ga_instanse, solution, sol_idx):
7+
global data_inputs, data_outputs, keras_ga, model
8+
9+
predictions = pygad.kerasga.predict(model=model,
10+
solution=solution,
11+
data=data_inputs)
12+
13+
bce = tensorflow.keras.losses.BinaryCrossentropy()
14+
solution_fitness = 1.0 / (bce(data_outputs, predictions).numpy() + 0.00000001)
15+
16+
return solution_fitness
17+
18+
def callback_generation(ga_instance):
19+
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
20+
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution()[1]))
21+
22+
# Build the keras model using the functional API.
23+
input_layer = tensorflow.keras.layers.Input(2)
24+
dense_layer = tensorflow.keras.layers.Dense(4, activation="relu")(input_layer)
25+
output_layer = tensorflow.keras.layers.Dense(2, activation="softmax")(dense_layer)
26+
27+
model = tensorflow.keras.Model(inputs=input_layer, outputs=output_layer)
28+
29+
# Create an instance of the pygad.kerasga.KerasGA class to build the initial population.
30+
keras_ga = pygad.kerasga.KerasGA(model=model,
31+
num_solutions=10)
32+
33+
# XOR problem inputs
34+
data_inputs = numpy.array([[0.0, 0.0],
35+
[0.0, 1.0],
36+
[1.0, 0.0],
37+
[1.0, 1.0]])
38+
39+
# XOR problem outputs
40+
data_outputs = numpy.array([[1.0, 0.0],
41+
[0.0, 1.0],
42+
[0.0, 1.0],
43+
[1.0, 0.0]])
44+
45+
# Prepare the PyGAD parameters. Check the documentation for more information: https://pygad.readthedocs.io/en/latest/README_pygad_ReadTheDocs.html#pygad-ga-class
46+
num_generations = 250 # Number of generations.
47+
num_parents_mating = 5 # Number of solutions to be selected as parents in the mating pool.
48+
initial_population = keras_ga.population_weights # Initial population of network weights.
49+
50+
# Create an instance of the pygad.GA class
51+
ga_instance = pygad.GA(num_generations=num_generations,
52+
num_parents_mating=num_parents_mating,
53+
initial_population=initial_population,
54+
fitness_func=fitness_func,
55+
on_generation=callback_generation)
56+
57+
# Start the genetic algorithm evolution.
58+
ga_instance.run()
59+
60+
# After the generations complete, some plots are showed that summarize how the outputs/fitness values evolve over generations.
61+
ga_instance.plot_fitness(title="PyGAD & Keras - Iteration vs. Fitness", linewidth=4)
62+
63+
# Returning the details of the best solution.
64+
solution, solution_fitness, solution_idx = ga_instance.best_solution()
65+
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
66+
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
67+
68+
predictions = pygad.kerasga.predict(model=model,
69+
solution=solution,
70+
data=data_inputs)
71+
print("Predictions : \n", predictions)
72+
73+
# Calculate the binary crossentropy for the trained model.
74+
bce = tensorflow.keras.losses.BinaryCrossentropy()
75+
print("Binary Crossentropy : ", bce(data_outputs, predictions).numpy())
76+
77+
# Calculate the classification accuracy for the trained model.
78+
ba = tensorflow.keras.metrics.BinaryAccuracy()
79+
ba.update_state(data_outputs, predictions)
80+
accuracy = ba.result().numpy()
81+
print("Accuracy : ", accuracy)
82+
83+
# model.compile(optimizer="Adam", loss="mse", metrics=["mae"])
84+
85+
# _ = model.fit(x, y, verbose=0)
86+
# r = model.predict(data_inputs)

examples/KerasGA/cancer_dataset.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import tensorflow as tf
2+
import tensorflow.keras
3+
import pygad.kerasga
4+
import pygad
5+
import numpy
6+
7+
def fitness_func(ga_instanse, solution, sol_idx):
8+
global train_data, data_outputs, keras_ga, model
9+
10+
predictions = pygad.kerasga.predict(model=model,
11+
solution=solution,
12+
data=train_data)
13+
14+
cce = tensorflow.keras.losses.CategoricalCrossentropy()
15+
solution_fitness = 1.0 / (cce(data_outputs, predictions).numpy() + 0.00000001)
16+
17+
return solution_fitness
18+
19+
def on_generation(ga_instance):
20+
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
21+
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution(ga_instance.last_generation_fitness)[1]))
22+
23+
# The dataset path.
24+
dataset_path = r'../data/Skin_Cancer_Dataset'
25+
26+
num_classes = 2
27+
img_size = 224
28+
29+
# Create a simple CNN. This does not gurantee high classification accuracy.
30+
model = tf.keras.models.Sequential()
31+
model.add(tf.keras.layers.Input(shape=(img_size, img_size, 3)))
32+
model.add(tf.keras.layers.Conv2D(32, (3,3), activation="relu", padding="same"))
33+
model.add(tf.keras.layers.MaxPooling2D((2, 2)))
34+
model.add(tf.keras.layers.Flatten())
35+
model.add(tf.keras.layers.Dropout(rate=0.2))
36+
model.add(tf.keras.layers.Dense(num_classes, activation="softmax"))
37+
38+
# Create an instance of the pygad.kerasga.KerasGA class to build the initial population.
39+
keras_ga = pygad.kerasga.KerasGA(model=model,
40+
num_solutions=10)
41+
42+
train_data = tf.keras.utils.image_dataset_from_directory(
43+
directory=dataset_path,
44+
image_size=(img_size, img_size),
45+
label_mode="categorical",
46+
batch_size=32
47+
)
48+
49+
# Get the dataset labels.
50+
# train_data.class_indices
51+
data_outputs = numpy.array([])
52+
for x, y in train_data:
53+
data_outputs = numpy.concatenate([data_outputs, numpy.argmax(y.numpy(), axis=-1)])
54+
data_outputs = tf.keras.utils.to_categorical(data_outputs)
55+
56+
# Check the documentation for more information about the parameters: https://pygad.readthedocs.io/en/latest/README_pygad_ReadTheDocs.html#pygad-ga-class
57+
initial_population = keras_ga.population_weights # Initial population of network weights.
58+
59+
# Create an instance of the pygad.GA class
60+
ga_instance = pygad.GA(num_generations=10,
61+
num_parents_mating=5,
62+
initial_population=initial_population,
63+
fitness_func=fitness_func,
64+
on_generation=on_generation)
65+
66+
# Start the genetic algorithm evolution.
67+
ga_instance.run()
68+
69+
# After the generations complete, some plots are showed that summarize how the outputs/fitness values evolve over generations.
70+
ga_instance.plot_fitness(title="PyGAD & Keras - Iteration vs. Fitness", linewidth=4)
71+
72+
# Returning the details of the best solution.
73+
solution, solution_fitness, solution_idx = ga_instance.best_solution(ga_instance.last_generation_fitness)
74+
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
75+
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
76+
77+
predictions = pygad.kerasga.predict(model=model,
78+
solution=solution,
79+
data=train_data)
80+
# print("Predictions : \n", predictions)
81+
82+
# Calculate the categorical crossentropy for the trained model.
83+
cce = tensorflow.keras.losses.CategoricalCrossentropy()
84+
print("Categorical Crossentropy : ", cce(data_outputs, predictions).numpy())
85+
86+
# Calculate the classification accuracy for the trained model.
87+
ca = tensorflow.keras.metrics.CategoricalAccuracy()
88+
ca.update_state(data_outputs, predictions)
89+
accuracy = ca.result().numpy()
90+
print("Accuracy : ", accuracy)
91+
92+
# model.compile(optimizer="Adam", loss="mse", metrics=["mae"])
93+
94+
# _ = model.fit(x, y, verbose=0)
95+
# r = model.predict(data_inputs)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import tensorflow as tf
2+
import tensorflow.keras
3+
import pygad.kerasga
4+
import pygad
5+
6+
def fitness_func(ga_instanse, solution, sol_idx):
7+
global train_generator, data_outputs, keras_ga, model
8+
9+
predictions = pygad.kerasga.predict(model=model,
10+
solution=solution,
11+
data=train_generator)
12+
13+
cce = tensorflow.keras.losses.CategoricalCrossentropy()
14+
solution_fitness = 1.0 / (cce(data_outputs, predictions).numpy() + 0.00000001)
15+
16+
return solution_fitness
17+
18+
def on_generation(ga_instance):
19+
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
20+
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution(ga_instance.last_generation_fitness)[1]))
21+
22+
# The dataset path.
23+
dataset_path = r'../data/Skin_Cancer_Dataset'
24+
25+
num_classes = 2
26+
img_size = 224
27+
28+
# Create a simple CNN. This does not gurantee high classification accuracy.
29+
model = tf.keras.models.Sequential()
30+
model.add(tf.keras.layers.Input(shape=(img_size, img_size, 3)))
31+
model.add(tf.keras.layers.Conv2D(32, (3,3), activation="relu", padding="same"))
32+
model.add(tf.keras.layers.MaxPooling2D((2, 2)))
33+
model.add(tf.keras.layers.Flatten())
34+
model.add(tf.keras.layers.Dropout(rate=0.2))
35+
model.add(tf.keras.layers.Dense(num_classes, activation="softmax"))
36+
37+
# Create an instance of the pygad.kerasga.KerasGA class to build the initial population.
38+
keras_ga = pygad.kerasga.KerasGA(model=model,
39+
num_solutions=10)
40+
41+
data_generator = tf.keras.preprocessing.image.ImageDataGenerator()
42+
train_generator = data_generator.flow_from_directory(dataset_path,
43+
class_mode='categorical',
44+
target_size=(224, 224),
45+
batch_size=32,
46+
shuffle=False)
47+
# train_generator.class_indices
48+
data_outputs = tf.keras.utils.to_categorical(train_generator.labels)
49+
50+
# Check the documentation for more information about the parameters: https://pygad.readthedocs.io/en/latest/README_pygad_ReadTheDocs.html#pygad-ga-class
51+
initial_population = keras_ga.population_weights # Initial population of network weights.
52+
53+
# Create an instance of the pygad.GA class
54+
ga_instance = pygad.GA(num_generations=10,
55+
num_parents_mating=5,
56+
initial_population=initial_population,
57+
fitness_func=fitness_func,
58+
on_generation=on_generation)
59+
60+
# Start the genetic algorithm evolution.
61+
ga_instance.run()
62+
63+
# After the generations complete, some plots are showed that summarize how the outputs/fitness values evolve over generations.
64+
ga_instance.plot_fitness(title="PyGAD & Keras - Iteration vs. Fitness", linewidth=4)
65+
66+
# Returning the details of the best solution.
67+
solution, solution_fitness, solution_idx = ga_instance.best_solution(ga_instance.last_generation_fitness)
68+
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
69+
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
70+
71+
predictions = pygad.kerasga.predict(model=model,
72+
solution=solution,
73+
data=train_generator)
74+
# print("Predictions : \n", predictions)
75+
76+
# Calculate the categorical crossentropy for the trained model.
77+
cce = tensorflow.keras.losses.CategoricalCrossentropy()
78+
print("Categorical Crossentropy : ", cce(data_outputs, predictions).numpy())
79+
80+
# Calculate the classification accuracy for the trained model.
81+
ca = tensorflow.keras.metrics.CategoricalAccuracy()
82+
ca.update_state(data_outputs, predictions)
83+
accuracy = ca.result().numpy()
84+
print("Accuracy : ", accuracy)
85+
86+
# model.compile(optimizer="Adam", loss="mse", metrics=["mae"])
87+
88+
# _ = model.fit(x, y, verbose=0)
89+
# r = model.predict(data_inputs)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import tensorflow.keras
2+
import pygad.kerasga
3+
import numpy
4+
import pygad
5+
6+
def fitness_func(ga_instanse, solution, sol_idx):
7+
global data_inputs, data_outputs, keras_ga, model
8+
9+
predictions = pygad.kerasga.predict(model=model,
10+
solution=solution,
11+
data=data_inputs)
12+
13+
cce = tensorflow.keras.losses.CategoricalCrossentropy()
14+
solution_fitness = 1.0 / (cce(data_outputs, predictions).numpy() + 0.00000001)
15+
16+
return solution_fitness
17+
18+
def callback_generation(ga_instance):
19+
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
20+
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution()[1]))
21+
22+
# Build the keras model using the functional API.
23+
input_layer = tensorflow.keras.layers.Input(shape=(100, 100, 3))
24+
conv_layer1 = tensorflow.keras.layers.Conv2D(filters=5,
25+
kernel_size=7,
26+
activation="relu")(input_layer)
27+
max_pool1 = tensorflow.keras.layers.MaxPooling2D(pool_size=(5,5),
28+
strides=5)(conv_layer1)
29+
conv_layer2 = tensorflow.keras.layers.Conv2D(filters=3,
30+
kernel_size=3,
31+
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)
35+
36+
model = tensorflow.keras.Model(inputs=input_layer, outputs=output_layer)
37+
38+
# Create an instance of the pygad.kerasga.KerasGA class to build the initial population.
39+
keras_ga = pygad.kerasga.KerasGA(model=model,
40+
num_solutions=10)
41+
42+
# Data inputs
43+
data_inputs = numpy.load("../data/dataset_inputs.npy")
44+
45+
# Data outputs
46+
data_outputs = numpy.load("../data/dataset_outputs.npy")
47+
data_outputs = tensorflow.keras.utils.to_categorical(data_outputs)
48+
49+
# 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.
53+
54+
# Create an instance of the pygad.GA class
55+
ga_instance = pygad.GA(num_generations=num_generations,
56+
num_parents_mating=num_parents_mating,
57+
initial_population=initial_population,
58+
fitness_func=fitness_func,
59+
on_generation=callback_generation)
60+
61+
# Start the genetic algorithm evolution.
62+
ga_instance.run()
63+
64+
# 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)
66+
67+
# Returning the details of the best solution.
68+
solution, solution_fitness, solution_idx = ga_instance.best_solution()
69+
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
70+
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
71+
72+
predictions = pygad.kerasga.predict(model=model,
73+
solution=solution,
74+
data=data_inputs)
75+
# print("Predictions : \n", predictions)
76+
77+
# Calculate the categorical crossentropy for the trained model.
78+
cce = tensorflow.keras.losses.CategoricalCrossentropy()
79+
print("Categorical Crossentropy : ", cce(data_outputs, predictions).numpy())
80+
81+
# Calculate the classification accuracy for the trained model.
82+
ca = tensorflow.keras.metrics.CategoricalAccuracy()
83+
ca.update_state(data_outputs, predictions)
84+
accuracy = ca.result().numpy()
85+
print("Accuracy : ", accuracy)
86+
87+
# model.compile(optimizer="Adam", loss="mse", metrics=["mae"])
88+
89+
# _ = model.fit(x, y, verbose=0)
90+
# r = model.predict(data_inputs)

0 commit comments

Comments
 (0)