Skip to content

Commit

Permalink
PyGAD 3.0.0 parallel processing
Browse files Browse the repository at this point in the history
There was an issue when using the `parallel_processing` parameter with Keras and PyTorch. As Keras/PyTorch are not thread-safe, the `predict()` method gives incorrect and weird results when more than 1 thread is used. ahmedfgad/GeneticAlgorithmPython#145 ahmedfgad/TorchGA#5 #6. Thanks to this StackOverflow answer https://stackoverflow.com/a/75606666/5426539.
  • Loading branch information
ahmedfgad committed Apr 8, 2023
1 parent 8c3e113 commit 0f135f8
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 13 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ pip3 install pygad

# Donation

You can donate via [Open Collective](https://opencollective.com/pygad): [opencollective.com/pygad](https://opencollective.com/pygad).

To donate using PayPal, use either this link: [paypal.me/ahmedfgad](https://paypal.me/ahmedfgad) or the e-mail address ahmed.f.gad@gmail.com.
- [Credit/Debit Card](https://donate.stripe.com/eVa5kO866elKgM0144): https://donate.stripe.com/eVa5kO866elKgM0144
- [Open Collective](https://opencollective.com/pygad): [opencollective.com/pygad](https://opencollective.com/pygad)
- PayPal: Use either this link: [paypal.me/ahmedfgad](https://paypal.me/ahmedfgad) or the e-mail address ahmed.f.gad@gmail.com
- Interac e-Transfer: Use e-mail address ahmed.f.gad@gmail.com

# Installation

Expand Down Expand Up @@ -80,7 +81,7 @@ import numpy
function_inputs = [4,-2,3.5,5,-11,-4.7]
desired_output = 44

def fitness_func(solution, solution_idx):
def fitness_func(ga_instance, solution, solution_idx):
output = numpy.sum(solution*function_inputs)
fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001)
return fitness
Expand Down Expand Up @@ -162,7 +163,7 @@ import pygad.kerasga
import numpy
import pygad

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

model_weights_matrix = pygad.kerasga.model_weights_as_matrix(model=model,
Expand Down Expand Up @@ -243,7 +244,7 @@ import pygad.kerasga
import numpy
import pygad

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

model_weights_matrix = pygad.kerasga.model_weights_as_matrix(model=model,
Expand Down
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .kerasga import *

__version__ = "1.1.0"
__version__ = "1.2.0"
2 changes: 1 addition & 1 deletion keras_pygad_XOR_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy
import pygad

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

predictions = pygad.kerasga.predict(model=model,
Expand Down
2 changes: 1 addition & 1 deletion keras_pygad_image_classification_CNN.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy
import pygad

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

predictions = pygad.kerasga.predict(model=model,
Expand Down
2 changes: 1 addition & 1 deletion keras_pygad_image_classification_Dense.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy
import pygad

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

predictions = pygad.kerasga.predict(model=model,
Expand Down
2 changes: 1 addition & 1 deletion keras_pygad_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy
import pygad

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

predictions = pygad.kerasga.predict(model=model,
Expand Down
6 changes: 4 additions & 2 deletions kerasga.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
import numpy
import tensorflow.keras

def model_weights_as_vector(model):
weights_vector = []
Expand Down Expand Up @@ -40,8 +41,9 @@ def predict(model, solution, data):
# Fetch the parameters of the best solution.
solution_weights = model_weights_as_matrix(model=model,
weights_vector=solution)
model.set_weights(solution_weights)
predictions = model.predict(data)
_model = tensorflow.keras.models.clone_model(model)
_model.set_weights(solution_weights)
predictions = _model.predict(data)

return predictions

Expand Down

0 comments on commit 0f135f8

Please sign in to comment.