我有一个变量v,并想对它应用移动平均。我应用了以下步骤来保存它:
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)])保存此会话后,我想恢复它,并使用variables_to_restore直接将'v/ExponentialMovingAverage'分配给v。以下是代码:
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)但是,也有NotFoundError:
NotFoundError (see above for traceback): Key v/ExponentialMovingAverage/ExponentialMovingAverage_1 not found in checkpoint我对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>}为什么有这么多变量,v_1, v_2等等?如何使用variables_to_restore校正NotFoundError并计算v的移动平均值
发布于 2018-08-16 05:21:02
如果在同一程序中调用tf.train.ExponentialMovingAverage(0.99)两次,则会创建ExponentialMovingAverage_1。
https://stackoverflow.com/questions/48861215
复制相似问题