TensorFlow Saving and loading training models

Source: Internet
Author: User

For machine learning, especially for deep-learning DL algorithms, model training can be time-consuming, hours or days, so if the test module is out of the question, it can be wasteful to re-run every time, so if there is no problem in the training section, then it is possible to save the training model directly. Then the next run loads the model directly and then tests it very conveniently.

The class that holds the (save) and load (restore) models in TensorFlow is Tf.train.Saver (), where the variables are saved key-value, and the default is all variables.

The save model uses the Save function, which first creates a saver object,

Save the model as follows:

Import TensorFlow as TF
"" "
Declaration variable and OP  
initialize OP declaration" "
#创建saver对象, which adds some op for save and restore model parameters
saver = Tf.train.Saver () with

TF. Session () as Sess:
    sess.run (init_op)
    #训练模型过程
    #使用saver提供的简便方法去调用 Save op
    saver.save (Sess, "save_ Path/file_name.ckpt ")  

The load model uses the RESTORE function, first creating a Saver object,

The recovery model is as follows:

Import TensorFlow as TF
"" "
Declaration variable and OP
initialize OP declaration" "
#创建saver object
saver = Tf.train.Saver () with

TF. Session () as Sess:
    sess.run (INIT_OP) #可以执行或不执行, the value of restore will override the initial value
    Saver.restore (sess, "save_path/file _name.ckpt ") 

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.