Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does calling the model.fit method again reinitialize the already trained weights?

I am using Keras to train a network. Let's say that after 20 epochs I want to stop the training to check if everything is fine, then continue form the 21st epoch. Does calling the model.fit method for a second time reinitialize the already trained weights ?

like image 545
Lilo Avatar asked Feb 03 '18 16:02

Lilo


People also ask

Does model fit reset weights?

Weights are not reset - your model would have exactly the same weights as before calling fit - of course until the optimization algorithm won't change them during the first batch.

What happens if you fit a model twice?

If you will execute model. fit(X_train, y_train) for a second time - it'll overwrite all previously fitted coefficients, weights, intercept (bias), etc.

What does model fit return?

By default Keras' model. fit() returns a History callback object. This object keeps track of the accuracy, loss and other training metrics, for each epoch, in the memory.

How does model fit works?

Model fitting is a measure of how well a machine learning model generalizes to similar data to that on which it was trained. A model that is well-fitted produces more accurate outcomes. A model that is overfitted matches the data too closely. A model that is underfitted doesn't match closely enough.


1 Answers

Does calling the model.fit method for a second time reinitialize the already trained weights ?

No, it will use the preexisting weights your model had and perform updates on them. This means you can do consecutive calls to fit if you want to and manage it properly.

This is true also because in Keras you are also able to save a model (with the save and load_model methods), load it back, and call fit on it. For more info on that check this question.

Another option you got is to use the train_on_batch method instead:

train_on_batch(self, x, y, sample_weight=None, class_weight=None)

Runs a single gradient update on a single batch of data.

This way I think you may have more control in between the updates of you model, where you can check if everything is fine with the training, and then continue to the next gradient update.

like image 82
DarkCygnus Avatar answered Oct 18 '22 20:10

DarkCygnus