Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keras: how to save the training history attribute of the history object

In Keras, we can return the output of model.fit to a history as follows:

 history = model.fit(X_train, y_train,                       batch_size=batch_size,                       nb_epoch=nb_epoch,                      validation_data=(X_test, y_test)) 

Now, how to save the history attribute of the history object to a file for further uses (e.g. draw plots of acc or loss against epochs)?

like image 821
jingweimo Avatar asked Dec 09 '16 13:12

jingweimo


People also ask

How do you save a trained neural network model?

Save Your Neural Network Model to JSON This can be saved to a file and later loaded via the model_from_json() function that will create a new model from the JSON specification. The weights are saved directly from the model using the save_weights() function and later loaded using the symmetrical load_weights() function.

How do you save a model after every epoch Keras?

Let's say for example, after epoch = 150 is over, it will be saved as model. save(model_1. h5) and after epoch = 152 , it will be saved as model. save(model_2.

How do you save the best epoch model?

If you want to save the best model during training, you have to use the ModelCheckpoint callback class. It has options to save the model weights at given times during the training and will allow you to keep the weights of the model at the end of the epoch specifically where the validation loss was at its minimum.

How do I save model weights for each epoch?

To save weights every epoch, you can use something known as callbacks in Keras. checkpoint = ModelCheckpoint(.....) , assign the argument 'period' as 1 which assigns the periodicity of epochs. This should do it.


1 Answers

What I use is the following:

    with open('/trainHistoryDict', 'wb') as file_pi:         pickle.dump(history.history, file_pi) 

In this way I save the history as a dictionary in case I want to plot the loss or accuracy later on.

like image 65
AEndrs Avatar answered Sep 23 '22 12:09

AEndrs