Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving best model in keras

I use the following code when training a model in keras

from keras.callbacks import EarlyStopping  model = Sequential() model.add(Dense(100, activation='relu', input_shape = input_shape)) model.add(Dense(1))  model_2.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy'])   model.fit(X, y, epochs=15, validation_split=0.4, callbacks=[early_stopping_monitor], verbose=False)  model.predict(X_test) 

but recently I wanted to get the best trained model saved as the data I am training on gives a lot of peaks in "high val_loss vs epochs" graph and I want to use the best one possible yet from the model.

Is there any method or function to help with that?

like image 767
dJOKER_dUMMY Avatar asked Jan 16 '18 15:01

dJOKER_dUMMY


People also ask

How do you save the best model in Tensorflow?

if save_best_only=True , it only saves when the model is considered the "best" and the latest best model according to the quantity monitored will not be overwritten. If filepath doesn't contain formatting options like {epoch} then filepath will be overwritten by each new better model.

Does early stopping save best model?

As far as I remember, Early stopping does not save any model automatically. The EarlyStopping class has a parameter restore_best_weights , but this is just about restoring the weights of your final neural network (if I remember correctly).

How do I save the keras model in Tensorflow?

Using save_weights() method Now you can simply save the weights of all the layers using the save_weights() method. It saves the weights of the layers contained in the model. It is advised to use the save() method to save h5 models instead of save_weights() method for saving a model using tensorflow.


2 Answers

EarlyStopping and ModelCheckpoint is what you need from Keras documentation.

You should set save_best_only=True in ModelCheckpoint. If any other adjustments needed, are trivial.

Just to help you more you can see a usage here on Kaggle.


Adding the code here in case the above Kaggle example link is not available:

model = getModel() model.summary()  batch_size = 32  earlyStopping = EarlyStopping(monitor='val_loss', patience=10, verbose=0, mode='min') mcp_save = ModelCheckpoint('.mdl_wts.hdf5', save_best_only=True, monitor='val_loss', mode='min') reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=7, verbose=1, epsilon=1e-4, mode='min')  model.fit(Xtr_more, Ytr_more, batch_size=batch_size, epochs=50, verbose=0, callbacks=[earlyStopping, mcp_save, reduce_lr_loss], validation_split=0.25) 
like image 136
Shridhar R Kulkarni Avatar answered Oct 03 '22 04:10

Shridhar R Kulkarni


EarlyStopping's restore_best_weights argument will do the trick:

restore_best_weights: whether to restore model weights from the epoch with the best value of the monitored quantity. If False, the model weights obtained at the last step of training are used.

So not sure how your early_stopping_monitor is defined, but going with all the default settings and seeing you already imported EarlyStopping you could do this:

early_stopping_monitor = EarlyStopping(     monitor='val_loss',     min_delta=0,     patience=0,     verbose=0,     mode='auto',     baseline=None,     restore_best_weights=True ) 

And then just call model.fit() with callbacks=[early_stopping_monitor] like you already do.

like image 26
jorijnsmit Avatar answered Oct 03 '22 04:10

jorijnsmit