Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to retrain a previously saved keras model?

i'm working in a time series prediction using keras and tensorflow. I need to retrain the model with future data. My question is, is this possible in keras and how we can do that?

like image 689
Nuwan Avatar asked Aug 15 '18 07:08

Nuwan


People also ask

How do you continue training in TensorFlow?

To continue training a loaded model with checkpoints, we simply rerun the model. fit function with the callback still parsed. This however overwrites the currently saved best model, so make sure to change the checkpoint file path if this is undesired.

How do you predict a saved model in keras?

The first step is to import your model using load_model method. Then you have to compile the model in order to make predictions. Now you can predict results for a new entry image. You do not need to compile anymore.

How do I save a keras model for later use?

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.


2 Answers

yes.

Save your model as .h5

When you want to train your model, load it again and do a model.fit as normal.

Make sure you do not compile your model after loading it as this will reset your weights.

See this link for more info

like image 80
VegardKT Avatar answered Oct 20 '22 07:10

VegardKT


I am updating the answer for any new user as it was long time back. if you are using recent Tensorflow (such as TF2.1 or later), then you can retrain the model as mentioned above.

There are two important options (saving in *.tf format and saving in *.h5 format). Saving is similar for both the options but there is a difference in loading the saved model.

1. Saving in *.tf format

When you load the saved model, compile = True is by default and it will retain the weights without any issue. After loading the saved model, you can retrain as usual using loaded_model.fit().

model.save('./MyModel_tf',save_format='tf')
# loading the saved model
loaded_model = tf.keras.models.load_model('./MyModel_tf')

# retraining the model
loaded_model.fit(x_train, y_train, epochs = 10, validation_data = (x_test,y_test),verbose=1)

2. Saving in *.h5 format

When you load the saved model, compile = True is by default and it will show a warning as follows.

WARNING:tensorflow:Error in loading the saved optimizer state. As a result, your model is starting with a freshly initialized optimizer

The above error means it will use freshly initialized optimizer. After loading the saved model, you can retrain as usual using loaded_model.fit().

model.save('./MyModel_h5.h5', save_format='h5')
# loading the saved model
loaded_model_h5 = tf.keras.models.load_model('./MyModel_h5.h5')

Please check detailed example here.

Another most important point is that when you have a custom_objects, then you need to select compile=False when you load the model and then compile the model with the custom_objects. This is true for the above two approaches.

Hope this helps. Thanks!

like image 34
Vishnuvardhan Janapati Avatar answered Oct 20 '22 09:10

Vishnuvardhan Janapati