save and restore

the tf.train.Saver class provides methods to save and restore medels. Estimators automatically save and restore variables in the model_dir


the tf.train.Saver constructor adds save and restore ops to the graph for all, or a specified list, of the variables in the graph. the Saver object provides methods to run these ops, specifying paths for the checkpoint files to write to or read from


Saver restores all variables already defined in your model. if you're loading a model without knowing how to build its graph(for example, if you're writing a generic program to load models), then read the Overview of saving and restoring models section later in this document


thensorflow saves variables in binary checkpoint files that map variable names to tensor values


create a Saver with tf.train.Saver() to manage all variables in the model. for example, the following snippet demonstrates how to call the tf.train.Saver.save method to save variables to checkpoint files


the tf.train.Saver object not only saves variables to checkpoint files, it also restores variables


there is not a physical file called /tmp/model.ckpt. it is the prefix of a filenames created for the checkpoint. users only interact with the prefix instead of physical checkpoint files


if you do not pass any arguments to tf.train.Saver(), the saver handles all variables in the graph. each variable is saved under the name that was passed when the variable was created


it is sometimes useful to explicitly specify names for variables in the checkpoint files. for example, you may have trained a model with a variable named 'weights' whose value you want to restore into a variable named 'params'


it is also sometimes useful to only save or restore a subset of the variables used by a model. for example, you may have trained a neural net with five layers, and you now want to train a new model with six layers that reuses the existing weights of the five trained layers. you can use the saver to restore the weights of just the first five layers


you can easily specify the names and variables to save or load by passing to the tf.train.Saver() constructor either of the following:
(1)a list of variables(which will be stored under their own names)
(2)a python dictionary in which keys are the names to use and the values are the variables to manage


you can create as many Saver objects as you want if you need to save and restore different subsets of the model variables. the same variable can be listed in multiple saver objects; its value is only changed when the Saver.restore() method is run


if you only restore a subset of the model variables at the start of a session, you have to run an initialize op for the other variables. see tf.variables_initializer for more information


to inspect the variables in a checkpoint, you can use the inspect_checkpoint library, particularly the print_tensor_in_checkpoint_file function


by defualt, Saver uses the value of the tf.Variable.name property for each variable. however, when you create a Saver object, you may optionally choose names for the variables in the checkpoint files

你可能感兴趣的:(Tensorflow)