Skip to content

Problems with multithreading and generation step #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
BenoitMiquey opened this issue Dec 13, 2022 · 1 comment
Open

Problems with multithreading and generation step #150

BenoitMiquey opened this issue Dec 13, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@BenoitMiquey
Copy link

BenoitMiquey commented Dec 13, 2022

Hi,

I realy appreciate your works on PyGAD!

I'm using it to make some chaotic learning with thousands of model, and a greedy fitness function. the parallelization is realy efficient in my case.

I have found some problems with multithreading using keras models.

To reproduce the problem, i use this regression sample : https://pygad.readthedocs.io/en/latest/README_pygad_kerasga_ReadTheDocs.html#example-1-regression-example

I only reduce the num_generations to 100.

Steps to reproduce :

I run a few times the sample,
image
image
image
image

  • then, i enable the parallel processing on 8 threads :

image

  • then, run again a few times :

image
image
image
image

  • sometimes, i see in logs a fitness lower than the n-1 generation, example :

image

  • I printed all solutions used in each epoch, and i saw thats solutions are most of time the same, so the parallel_processing seems to break the generation of the next population in the most of cases.

Thanks!

EDIT :
In addition i tried to reproduce the same problem with this classification problem sample ,
Adding the multiprocessing support cause the same problem.

@ahmedfgad ahmedfgad added the bug Something isn't working label Feb 25, 2023
@ahmedfgad
Copy link
Owner

Thanks to https://stackoverflow.com/a/75606666/5426539.

Keras is not thread-safe. The issue can be simply solved by cloning the model before calling the predict() method.

model = ...
_model = tensorflow.keras.models.clone_model(model)
predictions = _model.predict(...)

PyGAD 3.0.0 solved this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants