我想在推断阶段使用移动平均模型权重,我知道如何使用tf.train.ExponentialMovingAverage
,但是我不知道如何保存移动平均权重变量(也有偏差)并在推断期间加载它们时间而不是使用最终的训练值。
答案 0 :(得分:0)
最简单的方法是使用内置的tf.contrib.opt.MovingAverageOptimizer
,它包装了优化程序,并生成了一个变量保存程序,该变量保存程序将变量与移动平均值交换。
文档示例:
// Encapsulate your favorite optimizer (here the momentum one)
// inside the MovingAverageOptimizer.
opt = tf.train.MomentumOptimizer(learning_rate, FLAGS.momentum)
opt = tf.contrib.opt.MovingAverageOptimizer(opt)
// Then create your model and all its variables.
model = build_model()
// Add the training op that optimizes using opt.
// This needs to be called before swapping_saver().
opt.minimize(cost, var_list)
// Then create your saver like this:
saver = opt.swapping_saver()
// Pass it to your training loop.
slim.learning.train(
model,
...
saver=saver)