Taivanbat Badamdorj
Taivanbat Badamdorj

Reputation: 867

Saving weights after model has finished training - Tensorflow

In Tensorflow, how can I save the weights and all other variables of the program after it has finished training? I would like to be able to use the model I trained later on. Thanks in advance.

Upvotes: 1

Views: 827

Answers (1)

RobR
RobR

Reputation: 2190

You can define a saver object like this:

saver = tf.train.Saver(max_to_keep=5, keep_checkpoint_every_n_hours=1)

In this case, the saver is configured to keep the five most recent checkpoints and also to keep a checkpoint every hour during training.

The saver can then be called periodically in your main training loop with a call such as the following.

sess=tf.Session()

    ...

    # Save the model every 100 iterations
    if step % 100 == 0:
        saver.save(sess, "./model", global_step=step)

In this example the saver is saving a checkpoint into the ./model subdirectory every 100 training steps. The optional parameter global_step appends this value to the checkpoint filenames.

The model weights and other values may be restored at a later time for additional training or inference by the following:

        saver.restore(sess, path.model_checkpoint_path)

There are a variety of other useful variants and options. A good place to start learning about them is the TF how-to on variable creation, storage and retrieval here

Upvotes: 1

Related Questions