0

I have a variable v and want to apply moving average to it. I applied the following steps to save it:

import tensorflow as tf
v=tf.Variable(0,dtype=tf.float32,name='v')
ema=tf.train.ExponentialMovingAverage(0.99)
maintain_averages_op=ema.apply(tf.global_variables())
init=tf.global_variables_initializer()
saver=tf.train.Saver()
with tf.Session() as sess:
    sess.run(init)
    sess.run(tf.assign(v,10))
    sess.run(maintain_averages_op)
    saver.save(sess, 'C:/Users/User/PycharmProjects/Neural_Network.model.ckpt')
    sess.run([v, ema.average(v)])

After saving this session, I want to restore it and assign 'v/ExponentialMovingAverage' to v directly using variables_to_restore This is the code:

v=tf.Variable(0,dtype=tf.float32,name='v')
ema=tf.train.ExponentialMovingAverage(0.99)
print(ema.variables_to_restore())
saver=tf.train.Saver(ema.variables_to_restore())
with tf.Session() as sess:
    saver.restore(sess,'C:/Users/User/PycharmProjects/Neural_Network.model.ckpt')
    sess.run(v)

However, there is NotFoundError:

NotFoundError (see above for traceback): Key v/ExponentialMovingAverage/ExponentialMovingAverage_1 not found in checkpoint

I'm a bit confused with the output of print(ema.variables_to_restore()):

{'v/ExponentialMovingAverage/ExponentialMovingAverage_1': <tf.Variable 'v/ExponentialMovingAverage:0' shape=() dtype=float32_ref>, 'v_1/ExponentialMovingAverage_1': <tf.Variable 'v_1:0' shape=() dtype=float32_ref>, 'v_3/ExponentialMovingAverage': <tf.Variable 'v_3:0' shape=() dtype=float32_ref>, 'v/ExponentialMovingAverage_2': <tf.Variable 'v:0' shape=() dtype=float32_ref>, 'v_2/ExponentialMovingAverage_1': <tf.Variable 'v_2:0' shape=() dtype=float32_ref>, 'v/ExponentialMovingAverage_1': <tf.Variable 'v/ExponentialMovingAverage_1:0' shape=() dtype=float32_ref>, 'v_1/ExponentialMovingAverage': <tf.Variable 'v_1/ExponentialMovingAverage:0' shape=() dtype=float32_ref>, 'v/ExponentialMovingAverage/ExponentialMovingAverage': <tf.Variable 'v/ExponentialMovingAverage/ExponentialMovingAverage:0' shape=() dtype=float32_ref>, 'v_2/ExponentialMovingAverage': <tf.Variable 'v_2/ExponentialMovingAverage:0' shape=() dtype=float32_ref>}

Why there are so many variables v_1, v_2 etc.? How can I correct the NotFoundError and compute the moving average of v using variables_to_restore?

1 Answer 1

0

If tf.train.ExponentialMovingAverage(0.99) is called twice in the same program, then ExponentialMovingAverage_1 will be created.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.