Reputation: 505
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?
Upvotes: 10
Views: 18371
Reputation: 3288
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.
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)
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!
Upvotes: 9