import tensorflow as tf
v1 = tf.Variable(0, dtype=tf.float32)
step = tf.Variable(0, trainable=False)
ema = tf.train.ExponentialMovingAverage(0.99, step)
maintain_averages_op = ema.apply([v1])
#The apply() method adds shadow copies of trained variables and add ops that
#maintain a moving average of the trained variables in their shadow copies.
#It is used when building the training model. The ops that maintain moving
#averages are typically run after each training step.
#ema.apply()方法用来定义一个执行滑动平均的操作,并保存shadow值
#ema.average()方法用来取出ema.apply()方法保存的变量的shadow值
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
print(sess.run([v1, ema.average(v1)]))
sess.run(tf.assign(v1, 5))
sess.run(maintain_averages_op)
print(sess.run([v1, ema.average(v1)]))