pythontensorflowneural-networkkeras

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?


Solution

  • 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.