ahmedfgad / kerasga Goto Github PK
View Code? Open in Web Editor NEWTraining Keras Models by the Genetic Algorithm using PyGAD
Home Page: https://pygad.readthedocs.io
Training Keras Models by the Genetic Algorithm using PyGAD
Home Page: https://pygad.readthedocs.io
Keras gives wrong results when used with parallel processing. This is similar to this issue: ahmedfgad/GeneticAlgorithmPython#145
import tensorflow.keras
import numpy
import concurrent.futures
numpy.random.seed(1)
def create_rand_weights(model, num_models):
random_model_weights = []
for model_idx in range(num_models):
random_weights = []
for layer_idx in range(len(model.weights)):
layer_shape = model.weights[layer_idx].shape
if len(layer_shape) > 1:
layer_weights = numpy.random.rand(layer_shape[0], layer_shape[1])
else:
layer_weights = numpy.random.rand(layer_shape[0])
random_weights.append(layer_weights)
random_weights = numpy.array(random_weights, dtype=object)
random_model_weights.append(random_weights)
random_model_weights = numpy.array(random_model_weights)
return random_model_weights
def model_error(model_weights):
global data_inputs, data_outputs, model
model.set_weights(model_weights)
predictions = model.predict(data_inputs)
mae = tensorflow.keras.losses.MeanAbsoluteError()
abs_error = mae(data_outputs, predictions).numpy() + 0.00000001
return abs_error
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)
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 = numpy.array([[0.1],
[0.6],
[1.3],
[2.5]])
num_models = 10
random_model_weights = create_rand_weights(model, num_models)
ExecutorClass = concurrent.futures.ThreadPoolExecutor
thread_output = []
with ExecutorClass(max_workers=2) as executor:
output = executor.map(model_error, random_model_weights)
for out in output:
thread_output.append(out)
thread_output=numpy.array(thread_output)
print("Wrong Outputs using Threads")
print(thread_output)
print("\n\n")
correct_output = []
for idx in range(num_models):
error = model_error(random_model_weights[idx])
correct_output.append(error)
correct_output=numpy.array(correct_output)
print("Correct Outputs without Threads")
print(correct_output)
print(correct_output - thread_output)
Dear Mr. Ahmed F. Gad,
Hallo Dr., I try to learn your pyGAD package starting from your example. But I don't understand with the output of fitness_function in Example 1: Train Keras Regression Model. What is the mean of the output? Because I think the objective of Regression problem is to minimize the output. Why the graph is increased instead of decreased? How to change the graph to show MAE history instead of fitness function?
Thank you
Hi,
Trying to experiment your code but I have got this error "failed to allocate memory [Op:Sub]" when running on Colab. Any good idea how can I cope with this?
Thanks in advance
The current keras example uses a fixed architecture (model). Do you have an example of modifying the crossover or mutation in for a heterogeneous population?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.