4

I train a neat-python model to play snake. I want to save the model after 50 generations, and after that i want to load it and retrain it from there, is it possible? I saw a method where you can replay it, but just replay it one time, not retraining it from that stage.

My code for saving:

winner = p.run(main, 170)
with open("winner.pkl", "wb") as f:
    pickle.dump(winner, f)
    f.close()

I want to be able to load it and call the run function again to retrain it

def run(config_file):
config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction,
                            neat.DefaultSpeciesSet, neat.DefaultStagnation,
                            config_file)
p = neat.Population(config)
p.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
p.add_reporter(stats)
winner = p.run(main, 50)
with open("winner.pkl", "wb") as f:
    pickle.dump(winner, f)
    f.close()

if __name__ == '__main__':
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'config-feedforward.txt')
    run(config_path)

1 Answers1

3

Try this:

https://neat-python.readthedocs.io/en/latest/_modules/checkpoint.html

It works, only problem with this is that it loads from the last saved generation and overwrites it again.

So if you run for 5 generations and save all 5: 0,1,2,3,4; If you load the last one than you will get the saves: 4,5,6,7,8

This doesn't allow to save only one generation as it will load en redo the same generation which will basically leave you at generation 0 the whole time.. super weird.

But this is the script if you don't mind losing your last generation every time.

Anyone who has a workaround for this one?

Igor Markovic
  • 145
  • 1
  • 11