Saver Instance Code:
# # Save to File
# Remember to define the same dtype and shape when restore
W = tf. Variable ([[[1,2,3],[3,4,5]], dtype=tf.float32, name= ' weights ')
B = tf. Variable ([[[Dtype=tf.float32]],, name= ' biases ')
init= tf.global_variables_initialize ()
saver = Tf.train.Saver () with
TF. Session () as Sess:
sess.run (init)
Save_path = Saver.save (Sess, "my_net/save_net.ckpt")
print ("Save to Path: ", Save_path)
Restore Instance code:
# Restore Variables
# redefine the same shape and same type for your variables
W = tf. Variable (Np.arange (6). Reshape ((2, 3)), Dtype=tf.float32, name= "weights")
B = tf. Variable (Np.arange (3). Reshape ((1, 3)), Dtype=tf.float32, name= "biases")
# not need init step
saver = Tf.train.Saver () with
TF. Session () as Sess:
saver.restore (Sess, "my_net/save_net.ckpt")
print ("Weights:", Sess.run (W))
Print ("Biases:", Sess.run (b))
After a saver.save (), you can see the new four files in the folder:
The checkpoint file holds a list of the many model files that are recorded Model.ckpt.meta saved the structure of the TensorFlow calculation diagram, Model.ckpt Save the value of each variable, where the file name is written differently depending on the settings of the different parameters, but the file path name when the restore was loaded is in the checkpoint file The "Model_checkpoint_path" value is determined. The simple understanding is that the weights and other parameters are saved to the. chkp.data file, in the form of a dictionary; diagrams and metadata are saved to the. chkp.meta file, which can be tf.train.import_meta_graph loaded into the current default diagram.