v = tf.Variable(0,dtype=tf.float32,name='v')
#在没有申明滑动平均模型时只有一个变量v,所以下面的语句只会输出'v:0'
for variables in tf.all_variables():
print(variables.name)
ema = tf.train.ExponentialMovingAverage(0.99)
maintain_averages_op = ema.apply(tf.all_variables())
#在申明滑动平均模型之后,Tensorflow会自动生成一个应自变量#v/ExponentialMoving Average。于是下面的语句会输出#'v:0'和'v/ExponentialMovingAverage:0'
for variables in tf.all_variables():
print(variables.name)
6.保存滑动平均影子变量
saver = tf.train.Saver()
with tf.Session() as sess:
init_op = tf.initialize_all_variables()
sess.run(init_op)
sess.run(maintain_averages_op)
#保存时,TensorFlow会将v:0和v/ExponentialMovingAverage:0两个变量都存下来
saver.save(sess,'./model/model.ckpt')
print(sess.run([v,ema.average(v)]))