Usage of TensorFlow series--saver

Source: Internet
Author: User
the use of Saver 1. Background introduction of SaverWe often want to save the results of the training after training a model, which refers to the parameters of the model so that the next iteration is trained or used for testing. TensorFlow provides a saver class for this requirement. The Saver class provides methods for saving and recovering variables from the checkpoints file to the checkpoints file. The checkpoints file is a binary file that maps the variable name to the corresponding tensor value. Whenever a counter is provided, the Saver class can generate the checkpoint file automatically when the counter is triggered. This allows us to save multiple intermediate results during the training process. For example, we can save the results of each step of training.
To avoid filling up the entire disk, saver can automatically manage checkpoints files. For example, we can specify to save the nearest n checkpoints file. 2. Examples of saverHere's an example of how to use the Saver class
Import TensorFlow as TF import numpy as NP x = Tf.placeholder (Tf.float32, Shape=[none, 1]) y = 4 * x + 4 W = tf. Variable (Tf.random_normal ([1],-1, 1)) B = tf. Variable (Tf.zeros ([1])) y_predict = w * x + b loss = Tf.reduce_mean (Tf.square (y-y_predict)) optimizer = Tf.train.Gradi Entdescentoptimizer (0.5) train = optimizer.minimize (loss) Istrain = False Train_steps = + Checkpoint_steps = Checkpo Int_dir = "Saver = tf.train.Saver () # defaults to saving all variables-in this case W and b x_data = Np.reshape (NP.R Andom.rand (Astype (Np.float32), (1)) with TF.
            Session () as Sess:sess.run (Tf.initialize_all_variables ()) If istrain:for I in Xrange (train_steps): Sess.run (Train, Feed_dict={x:x_data}) if (i + 1)% Checkpoint_steps = = 0:saver.save (s ESS, Checkpoint_dir + ' model.ckpt ', global_step=i+1) else:ckpt = Tf.train.get_checkpoint_state (checkpoint_dir
     ) If Ckpt and Ckpt.model_checkpoint_path:       Saver.restore (Sess, Ckpt.model_checkpoint_path) else:pass print (Sess.run (w)) p
 Rint (Sess.run (b))

Istrain: Used to differentiate between training and testing phases, true for training, false for test
train_steps: Indicates the number of sessions, using 100 in the example
checkpoint_steps: Indicates how many times the training is saved checkpoints, the example uses 50
Checkpoint_dir: Represents the save path for the checkpoints file, using the current path in the example

2.1 Training StagesSave the model using the Saver.save () method: Sess: Represents the current session, the current session records the current variable value Checkpoint_dir + ' model.ckpt ': Represents the stored file name
Global_step: Indicates that the current is the first step
After the training is completed, there are 5 more files under the current directory.
Open a file named "Checkpoint" to see the saved record, and the latest model storage location.
2.1 Test PhaseThe test phase uses the Saver.restore () method to restore the variable: sess: Represents the current session, and the previously saved results will be loaded into the session Ckpt.model_checkpoint_path: represents the location of the model store and does not need to provide the name of the model.     It will go to the checkpoint file to see who the newest is and what it is called. The result of running the results of the previously trained parameters W and B is shown in the following figure.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.