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 ")