today share with you some of the things that have been trained with TensorFlow's Saver access.

1. uses saver to access variables;
2. uses saver to access a specified variable.

uses saver to access variables.

said, first on the

 coding=utf-8 import # code OS import tensorflow as TF import numpy os.environ['TF_CPP_MIN_LOG_LEVEL'] ='2'# some instruction set is not installed, and this does not show that warning w = tf.Variable ([[1,2,3], [2,3,4], [6,7,8]], dtype=tf.float32) B = tf.Variable ([[4,5,6]], dtype=tf.float32, s = tf.Variable ([[2) 5], [5, 6]], dtype=tf.float32, init) = tf.global_variables_initializer (Saver) =tf.train.Saver (with) tf.Session (as) sess: sess.run (init) save_path = saver.save (sess, save_net.ckpt) # path can set their own print ("save to path:", save_path 

) here I can define a few variables and then operation, after the operation, the variable W, B, s will be saved. Save the generated following file:

  • cheakpoint
  • save_net.ckpt.data-*
  • save_net.ckpt.index
  • save_net.ckpt.meta

 is the next read code import tensorflow as TF import OS import numpy as NP os.environ['TF_CPP_MIN_LOG_LEVEL'] ='2'w = tf.Variable (np.arange (9).Reshape ((3,3)), dtype=tf.float32 (tf.Variable) B = np.arange (.Reshape (3) (1,3)), dtype=tf.float32 (np.arange) a = tf.Variable (4).Reshape ((2,2)), dtype=tf.float32 (Saver) =tf.train.Saver) with (tf.Session) as sess: saver.restore (sess,'save_net.ckpt') print ("weights", sess.run (W) print ("B"), sess.run (b) (print) "s", sess.run (a)) 

in writing to read the code to the attention of the type, size and variable definition The quantity and order of the quantity should be in accordance with the time of storage, otherwise the error will be reported. When you save, the order is w, B, s, and the same order when you take it. At the time of storage, w defined that dtype did not define name, and when it was fetching, it also needed to be done, because TensorFlow access was accessed by key value pairs, so it must be consistent. The variable name here, that is, W, s and so on, can be different.

is the result I read successfully.

uses saver to access the specified variables.

when we do training, some of the variables are not necessary to save, but if you use tf.train.Saver () directly. The program will save all the variables, at which time we can specify the save, only the variables we need, and the others are lost.
is actually very simple, only need to modify slightly in the above code based on only the tf.train.Saver (

) to replace the following code [] = program program + = [w, b] tf.train.Saver (program) 

, the program will only save W and B. In the same way, the tf.train.Saver () in the read program should be modified as above. Dtype, name and so on must still be consistent.

:

 and finally attached to the final code # coding=utf-8 # Saver import OS import tensorflow save variable test as TF import numpy os.environ['TF_CPP_MIN_LOG_LEVEL'] ='2'# some instruction set is not installed, and this does not show that w = tf.Variable ("warning 1,2,3], [2,3,4], [6,7,8]], dtype=tf.float32) B = tf.Variable ([[4,5,6]], dtype=tf.float32) = s tf.Variable ([[2, 5], [5, 6]], dtype=tf.float32) init = tf.global_variables_initializer (program) = program [w = b] [], Saver = tf.train.Saver (program) with (tf.Session) as sess: sess.run (init) save_path = saver.save (sess, save_net.ckpt) # path can set their own print (" save to path: ". Save_path 
 #saver) import tensorflow as TF extraction test of variables of import OS import numpy as NP os. Environ['TF_CPP_MIN_LOG_LEVEL'] ='2'w = tf.Variable (np.arange (9).Reshape ((3,3)), dtype=tf.float32 (tf.Variable) B = np.arange (3).Reshape ((1,3)), dtype=tf.float32 (tf.Variable) a = np.arange (4).Reshape ((2,2)), dtype=tf.float32) program = program +=[w b] saver =tf.train.Saver [], (program tf.Session (with) as sess: saver.restore (SESS),'save_net.ckpt' print ("weights"), sess.run (W) print ("B"), sess.run (b) #print ("s"), sess.run (a) 

) all above is the article, I hope to help you study. I hope you will support a script.

you might be interested in this article:

management variables


This concludes the body part

This paper fixed link:http://www.script-home.com/access-to-tensorflow-saver-specified-variables.html | Script Home | +Copy Link

Article reprint please specify:Access to TensorFlow Saver specified variables | Script Home

You may also be interested in these articles!