gpt4 book ai didi

python - 如何使用 tensorflow 在推理阶段加载移动平均权重变量

转载 作者:行者123 更新时间:2023-12-01 09:14:13 25 4
gpt4 key购买 nike

我想在推理阶段使用移动平均模型权重,我知道如何使用tf.train.ExponentialMovingAverage,但我不知道如何保存移动平均权重变量(也偏差)并在推理时间加载它们,而不是使用最终的训练值。

最佳答案

最简单的方法是使用内置 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)

关于python - 如何使用 tensorflow 在推理阶段加载移动平均权重变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51394132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com