TensorFLow uses Saver to save and restore variables.
This article shares with you the code for saving and restoring variables using Saver in TensorFLow for your reference. The specific content is as follows:
Create the tensor_save.py file and save the tensor of the v1 and v2 variables to the checkpoint files. Set the names to v3 and v4 respectively.
import tensorflow as tf# Create some variables.v1 = tf.Variable(3, name="v1")v2 = tf.Variable(4, name="v2")# Create modely=tf.add(v1,v2)# Add an op to initialize the variables.init_op = tf.initialize_all_variables()# Add ops to save and restore all the variables.saver = tf.train.Saver({'v3':v1,'v4':v2})# Later, launch the model, initialize the variables, do some work, save the# variables to disk.with tf.Session() as sess: sess.run(init_op) print("v1 = ", v1.eval()) print("v2 = ", v2.eval()) # Save the variables to disk. save_path = saver.save(sess, "f:/tmp/model.ckpt") print ("Model saved in file: ", save_path)
Create the file tensor_restror.py and restore the tensor named v3 and v4 in checkpoint files to the variables v3 and v4 respectively.
import tensorflow as tf# Create some variables.v3 = tf.Variable(0, name="v3")v4 = tf.Variable(0, name="v4")# Create modely=tf.mul(v3,v4)# Add ops to save and restore all the variables.saver = tf.train.Saver()# Later, launch the model, use the saver to restore variables from disk, and# do some work with the model.with tf.Session() as sess: # Restore variables from disk. saver.restore(sess, "f:/tmp/model.ckpt") print ("Model restored.") print ("v3 = ", v3.eval()) print ("v4 = ", v4.eval()) print ("y = ",sess.run(y))
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.