Home > Net >  Is there any method/function within Keras to recover the weights of a model during different trainin
Is there any method/function within Keras to recover the weights of a model during different trainin

Time:02-19

I have a model that I want to train for 10 epochs for a certain hyperparameters setting. After training, I will use the history.history object and find the epoch where the validation loss was at a minimum. Once I have this best scoring epoch, I would like to retrieve this model and use it to predict the test data. Now, imagine that my best scoring epoch was not the last one. Is there any option within this Keras history object (such as history.model) to retrieve past values of weights? I imagine that, if there is not, I would have to create a dictionary and temporarily store each model per epoch until finishing training and finding the best one. But, when using model.fit, there is no option to store each model per epoch right. How would you do this?

CodePudding user response:

Keras offers the option of evaluate your model on validation data after each epoch

After divide your data into trainning, test and validation data you can train you model like this:

model=modelmlp(np.shape(x_trai)[0],hidden,4)
model.compile(loss='categorical_crossentropy', optimizer = 'adam', metrics='accuracy'])
hist=model.fit(x_train,y_train,epochs=epochs,batch_size=batch,
               validation_data(x_valid,y_valid),verbose=verbose[1],
               callbacks=[ModelCheckpoint(filepath='bestweigths.hdf5',
               monitor='val_loss',verbose=verbose[2],save_best_only=True,mode='min')])
model=load_model('bestweigths.hdf5')

This code will train your model and, after each epoch, your model will be evaluated at the validation data. Every time the result on the validation data is improved the model will be saved on a file

After the trainning process end, you just need to load the model from the file

CodePudding user response:

you can use the callback class of keras for this matter. You can save the model weights based on the metric you need. Let's say for example you need to save the model with minimum loss. You'll have to define a modelcheckpoint.

first before training the model define the checkpoint in the below given format

callbacks = [ModelCheckpoint(filepath='resNet_centering2.h5', monitor='val_loss', mode='min', save_best_only=True)]

now since you have defined callback, you'll have to use use this callbacks in the model.fit call

history = model.fit(
    x=X_train,
    y=Y_train,
    callbacks=callbacks,
    batch_size=4,
    epochs=100,
    verbose=1,
    validation_data=(X_test, Y_test))

this will save the best weights of your model at defined filepath and you can fetch those weights using the below given call.

model=load_model('bestweigths.hdf5')

I hope it solves your problem.

  • Related