Dweller
Dweller

Reputation: 19

How to load the latest checkpoint and save it as a model in Tensorflow?

So, I was wondering how to load the latest checkpoint in Tensorflow having its path/directory and continue the training where I left off. And also how to load the latest checkpoint and save it as a complete model. Please help me

My code:

        cp_callback = tf.keras.callbacks.ModelCheckpoint(
            filepath=checkpoint_path, 
            verbose=1, 
            save_weights_only=True,
            save_freq=1*batch_size)

        # Create a basic model instance
        model = create_model(training, output)
        model.save_weights(checkpoint_path.format(epoch=0))

        # Create a TensorBoard callback (for metrics monitoring)
        tb_callback = tf.keras.callbacks.TensorBoard(log_dir="chatbot/training/logs", histogram_freq=1, update_freq= 1, profile_batch= 1)

        # Train the model with the new callback
        model.fit(training, output, epochs=500, batch_size = batch_size, validation_data=(training, output), callbacks=[cp_callback, tb_callback], verbose = 1)

Upvotes: 1

Views: 4351

Answers (1)

nFinite
nFinite

Reputation: 41

The simplest solution to your problem would be to save the entire model with the ModelCheckpoint callback. You only have to remove the save_weights_only argument for it to work.

cp_callback = tf.keras.callbacks.ModelCheckpoint(
            filepath=checkpoint_path, 
            verbose=1,
            save_freq=1*batch_size)

To load the checkpoint and continue training at a later point in time, just call

model = tf.keras.models.load_model(checkpoint_path)

If you want to load a checkpoint given you only saved the model weights, you have to first build your model and transfer your saved weights into it.

model.load_weights(checkpoint_path)

If you need further information about loading and saving models, I would recommend reading the documentation: https://www.tensorflow.org/guide/keras/save_and_serialize

This answer is referencing the answer of : Save and load weights in keras

Upvotes: 3

Related Questions