tf.compat.v1.train.Saver

View source on GitHub

Saves and restores variables.

tf.compat.v1.train.Saver(
    var_list=None, reshape=False, sharded=False, max_to_keep=5,
    keep_checkpoint_every_n_hours=10000.0, name=None, restore_sequentially=False,
    saver_def=None, builder=None, defer_build=False, allow_empty=False,
    write_version=tf.train.SaverDef.V2, pad_step_number=False,
    save_relative_paths=False, filename=None
)

See Variables for an overview of variables, saving and restoring.

The Saver class adds ops to save and restore variables to and from checkpoints. It also provides convenience methods to run these ops.

Checkpoints are binary files in a proprietary format which map variable names to tensor values. The best way to examine the contents of a checkpoint is to load it using a Saver.

Savers can automatically number checkpoint filenames with a provided counter. This lets you keep multiple checkpoints at different steps while training a model. For example you can number the checkpoint filenames with the training step number. To avoid filling up disks, savers manage checkpoint files automatically. For example, they can keep only the N most recent files, or one checkpoint for every N hours of training.

You number checkpoint filenames by passing a value to the optional global_step argument to save():

saver.save(sess, 'my-model', global_step=0) ==> filename: 'my-model-0'
...
saver.save(sess, 'my-model', global_step=1000) ==> filename: 'my-model-1000'

Additionally, optional arguments to the Saver() constructor let you control the proliferation of checkpoint files on disk:

Note that you still have to call the save() method to save the model. Passing these arguments to the constructor will not save variables automatically for you.

A training program that saves regularly looks like:

...
# Create a saver.
saver = tf.compat.v1.train.Saver(...variables...)
# Launch the graph and train, saving the model every 1,000 steps.
sess = tf.compat.v1.Session()
for step in xrange(1000000):
    sess.run(..training_op..)
    if step % 1000 == 0:
        # Append the step number to the checkpoint name:
        saver.save(sess, 'my-model', global_step=step)

In addition to checkpoint files, savers keep a protocol buffer on disk with the list of recent checkpoints. This is used to manage numbered checkpoint files and by latest_checkpoint(), which makes it easy to discover the path to the most recent checkpoint. That protocol buffer is stored in a file named 'checkpoint' next to the checkpoint files.

If you create several savers, you can specify a different filename for the protocol buffer file in the call to save().

Args:

Attributes:

Raises:

Methods

as_saver_def

View source

as_saver_def()

Generates a SaverDef representation of this saver.

Returns:

A SaverDef proto.

build

View source

build()

export_meta_graph

View source

export_meta_graph(
    filename=None, collection_list=None, as_text=False, export_scope=None,
    clear_devices=False, clear_extraneous_savers=False, strip_default_attrs=False,
    save_debug_info=False
)

Writes MetaGraphDef to save_path/filename.

Args:

Returns:

A MetaGraphDef proto.

from_proto

View source

@staticmethod
from_proto(
    saver_def, import_scope=None
)

Returns a Saver object created from saver_def.

Args:

Returns:

A Saver built from saver_def.

recover_last_checkpoints

View source

recover_last_checkpoints(
    checkpoint_paths
)

Recovers the internal saver state after a crash.

This method is useful for recovering the "self._last_checkpoints" state.

Globs for the checkpoints pointed to by checkpoint_paths. If the files exist, use their mtime as the checkpoint timestamp.

Args:

restore

View source

restore(
    sess, save_path
)

Restores previously saved variables.

This method runs the ops added by the constructor for restoring variables. It requires a session in which the graph was launched. The variables to restore do not have to have been initialized, as restoring is itself a way to initialize variables.

The save_path argument is typically a value previously returned from a save() call, or a call to latest_checkpoint().

Args:

Raises:

save

View source

save(
    sess, save_path, global_step=None, latest_filename=None,
    meta_graph_suffix='meta', write_meta_graph=True, write_state=True,
    strip_default_attrs=False, save_debug_info=False
)

Saves variables.

This method runs the ops added by the constructor for saving variables. It requires a session in which the graph was launched. The variables to save must also have been initialized.

The method returns the path prefix of the newly created checkpoint files. This string can be passed directly to a call to restore().

Args:

Returns:

A string: path prefix used for the checkpoint files. If the saver is sharded, this string ends with: '-?????-of-nnnnn' where 'nnnnn' is the number of shards created. If the saver is empty, returns None.

Raises:

set_last_checkpoints

View source

set_last_checkpoints(
    last_checkpoints
)

DEPRECATED: Use set_last_checkpoints_with_time.

Sets the list of old checkpoint filenames.

Args:

Raises:

set_last_checkpoints_with_time

View source

set_last_checkpoints_with_time(
    last_checkpoints_with_time
)

Sets the list of old checkpoint filenames and timestamps.

Args:

Raises:

to_proto

View source

to_proto(
    export_scope=None
)

Converts this Saver to a SaverDef protocol buffer.

Args:

Returns:

A SaverDef protocol buffer.