Home > OS >  Make a prediction with Keras models trained using the Genetic Algorithm with PyGAD
Make a prediction with Keras models trained using the Genetic Algorithm with PyGAD

Time:06-14

I successfully run the code (original link where to find code) to train Keras Models using the Genetic Algorithm with PyGAD:

import tensorflow.keras
import pygad.kerasga
import numpy
import pygad


def fitness_func(solution, sol_idx):
    global data_inputs, data_outputs, keras_ga, model

    model_weights_matrix = pygad.kerasga.model_weights_as_matrix(model=model,
                                                                 weights_vector=solution)

    model.set_weights(weights=model_weights_matrix)

    predictions = model.predict(data_inputs)

    mae = tensorflow.keras.losses.MeanAbsoluteError()
    abs_error = mae(data_outputs, predictions).numpy()   0.00000001
    solution_fitness = 1.0 / abs_error

    return solution_fitness


def callback_generation(ga_instance):
    print("Generation = {generation}".format(generation=ga_instance.generations_completed))
    print("Fitness    = {fitness}".format(fitness=ga_instance.best_solution()[1]))

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)

weights_vector = pygad.kerasga.model_weights_as_vector(model=model)

keras_ga = pygad.kerasga.KerasGA(model=model,
                                 num_solutions=10)

# Data inputs
data_inputs = numpy.array([[0.02, 0.1, 0.15],
                           [0.7, 0.6, 0.8],
                           [1.5, 1.2, 1.7],
                           [3.2, 2.9, 3.1]])

# Data outputs
data_outputs = numpy.array([[0.1],
                            [0.6],
                            [1.3],
                            [2.5]])

num_generations = 10
num_parents_mating = 5
initial_population = keras_ga.population_weights

ga_instance = pygad.GA(num_generations=num_generations,
                       num_parents_mating=num_parents_mating,
                       initial_population=initial_population,
                       fitness_func=fitness_func,
                       on_generation=callback_generation)
ga_instance.run()

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

# Returning the details of the best solution.
solution, solution_fitness, solution_idx = ga_instance.best_solution()
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))

# Fetch the parameters of the best solution.
best_solution_weights = pygad.kerasga.model_weights_as_matrix(model=model,
                                                              weights_vector=solution)
model.set_weights(best_solution_weights)
predictions = model.predict(data_inputs)
print("Predictions : \n", predictions)

mae = tensorflow.keras.losses.MeanAbsoluteError()
abs_error = mae(data_outputs, predictions).numpy()
print("Absolute Error : ", abs_error)

Out:

Fitness value of the best solution = 5.007608966738384
Index of the best solution : 0
1/1 [==============================] - 0s 18ms/step
Predictions : 
 [[0.4351511 ]
 [0.78366435]
 [1.3436508 ]
 [2.736318  ]]
Absolute Error :  0.1996961

As I understand, the code above should train a model to help me forecast a new 3-dimension input such as [0.9, 0.7, 0.85].

I wonder how could I modify the code to adapt to the input and output data as below, or call model then make a predictions for new data_inputs = numpy.array([[0.9, 0.7, 0.85]]):

# Data inputs
data_inputs = numpy.array([[0.02, 0.1, 0.15],
                           [0.7, 0.6, 0.8],
                           [1.5, 1.2, 1.7],
                           [3.2, 2.9, 3.1],
                           [0.9, 0.7, 0.85] # new entry which need forecast
                           ])

# Data outputs
data_outputs = numpy.array([[0.1],
                            [0.6],
                            [1.3],
                            [2.5]]) # Output data for training

Thanks a lot for your help at advance.

CodePudding user response:

My trial code:

from tensorflow import keras
# Load model and weights
with open("./ga_model.json", "r") as json_file:
    model_json = json_file.read()

model = keras.models.model_from_json(model_json)
model.load_weights("./ga_model.h5")

# Data inputs
new_data_inputs = numpy.array([
                           [0.9, 0.7, 0.85] # new entry which need forecast
                           ])


predictions = model.predict(new_data_inputs)
print("Predictions : \n", predictions)

Out:

Predictions : 
 [[0.8672837]]
  • Related