Vinicius Luiz Pacheco
Vinicius Luiz Pacheco

Reputation: 21

How do I proceed to load a ga_instance as ".pkl" format in PyGad?

I have been trying to load the PyGad trained instance in another file, in order to make some prediction. But I have been having some problems in the loading process. After the training phase, I saved the instance like this:

The saving function:

filename = 'GNN_CPTNet' #GNN_CPTNet.pkl

ga_instance.save(filename=filename)

The loading function:

loaded_ga_instance = pygad.load(filename=filename)

loaded_ga_instance.plot_result()

But, when I tried to load the instance in a new notebook or script, I could not load the instance, especially the "GNN_CPT Net.pkl" file.

Upvotes: 2

Views: 515

Answers (1)

Ahmed Gad
Ahmed Gad

Reputation: 866

Edit on February 28, 2023

Starting from PyGAD 2.19, the library cloudpickle is used instead of pickle. Now, you do not have to re-define the functions. So, it is enough to call the load() function.

In the new script, you should define the the fitness function and all the callback functions you used in the original script.

For example, if you used only the on_generation (callback_generation) parameter, then the following functions should be defined:

def fitness_func(solution, solution_idx):
    ...


def callback_generation(ga_instance):
    ...

This way, the saved instance will be loaded correctly.

Anyway, it is better to post the sample codes you used to give more accurate answer.

Thanks for using PyGAD :)

Upvotes: 1

Related Questions