Detail the variables_to_restore function in TensorFlow

  • 2020-11-30 08:24:44
  • OfStack

The variables_to_restore function, which is provided by TensorFlow for the sliding average. We've also seen how using sliding averages can make neural network models more robust. We also know that in TensorFlow, the sliding average of variables is maintained by the shadow variable. If you want to get the sliding average of variables, you need to get the shadow variable, not the variable itself.

1. Save the sliding mean model file


import tensorflow as tf
 
if __name__ == "__main__":
 v = tf.Variable(0.,name="v")
 # Set the coefficients of the sliding average model 
 ema = tf.train.ExponentialMovingAverage(0.99)
 # Set a variable v Using the sliding average model, tf.all_variables() Set all variables 
 op = ema.apply([v])
 # Get variables v The name of the 
 print(v.name)
 #v:0
 # create 1 Three objects that hold the model 
 save = tf.train.Saver()
 sess = tf.Session()
 # Initializes all variables 
 init = tf.initialize_all_variables()
 sess.run(init)
 # To the variable v To assign a value 
 sess.run(tf.assign(v,10))
 # Apply the average slide setting 
 sess.run(op)
 # Save the model file 
 save.save(sess,"./model.ckpt")
 # The output variable v Values before and after using the sliding average model 
 print(sess.run([v,ema.average(v)]))
 #[10.0, 0.099999905]

The above code is how to save a model file of sliding average. We have introduced the saving of sliding average and model file before, so we will not repeat it here.

2. Read the sliding mean model file


 v = tf.Variable(1.,name="v")
 # Define the model object 
 saver = tf.train.Saver({"v/ExponentialMovingAverage":v})
 sess = tf.Session()
 saver.restore(sess,"./model.ckpt")
 print(sess.run(v))
 #0.0999999

For model file to read in a blog post has introduced, particularly need to be aware of a place is here, in the use of tf. train. Saver function, passing by the model parameters is {" v/ExponentialMovingAverage ": v} rather than {" v" : v}, if you are using the back of the parameters, then you will be the result of the 10 instead of 0.09, it is because the latter get variables itself rather than a shadow. Does it feel like you need to enter a string of variable names to read the model file in this way?

3, the use of variables_to_restore function


 v = tf.Variable(1.,name="v")
 # The size of the sliding model's parameters does not matter v The value of the 
 ema = tf.train.ExponentialMovingAverage(0.99)
 print(ema.variables_to_restore())
 #{'v/ExponentialMovingAverage': <tf.Variable 'v:0' shape=() dtype=float32_ref>}
 sess = tf.Session()
 saver = tf.train.Saver(ema.variables_to_restore())
 saver.restore(sess,"./model.ckpt")
 print(sess.run(v))
 #0.0999999

By using the function variables_to_restore, the shadow variable can be directly mapped to the variable itself when the model is loaded, so we only need to get the value of the variable when we get the sliding average value of the variable, but do not need to get the shadow variable.


Related articles: