Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras keyboard interrupt to stop training?

There seems to be a way to do it in tensorflow: Keyboard interrupt tensorflow run and save at that point

Is there something like this in Keras?

like image 372
Johan Avatar asked Oct 31 '18 15:10

Johan


People also ask

How can keras training be stopped?

You can use model. stop_training parameter to stop the training.

What happens if I interrupt model training?

The trained model will still be in memory, in the state it was in when the KeyboardInterrupt happened. As long as the Python kernel isn't stopped or the model isn't reinstantiated, you can continue to use the trained model.


1 Answers

You could catch the KeyboardInterrupt exception and save the model within the except block:

save_path = './keras-saves/_latest.ckpt'
try:
    model.fit(x_train, y_train,
              batch_size=batch_size,
              epochs=epochs)
except KeyboardInterrupt:
    model.save(save_path)
    print('Output saved to: "{}./*"'.format(save_path))
like image 132
rvinas Avatar answered Sep 20 '22 13:09

rvinas