滑动平均值的tensorflow实践

作者: shaneZhang 分类: 机器学习的实践 发布时间: 2018-12-27 10:20

滑动平均:记录了一段时间内模型中所有参数 w 和 b 各自的平均值。利用滑动平均值可以增强模 型的泛化能力。滑动平均值的计算公式为:

#coding=utf-8
import  tensorflow as tf

#定义变量及滑动平均值
#定义一个32位的浮点变量,初始值为0.0,这个代码就是不断的更新w1参数,优化w1参数,滑动平均值做了一个w1的影子
w1 = tf.Variable(0,dtype=tf.float32)
#定义num_updates (NN的迭代轮数),初始值为0.不可被优化,这个参数不训练
globa_step = tf.Variable(0,trainable=False)
#实例化滑动平均类,给删减率为0.99,当前轮数为global_step
MOVING_AVERAGE_DECAY=0.99
ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,globa_step)
#ema.apply后的括号里更新列表,每次运行sess.run(ema_op)时,对更新参数中的元素求滑动平均值
#在实际应用中会使用tf.trainable_variables()自动将所所有带训练的参数汇总为列表
#ema_op=ema.apply([w1])
ema_op = ema.apply(tf.trainable_variables())

#查看不同迭代中变量取值的编号
with tf.Session() as sess:
    #初始化
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    #用ema.average(w1)来获取w1的滑动平均值
    print sess.run([w1,ema.average(w1)])
    #将w1的值赋值为1
    sess.run(tf.assign(w1,1))
    sess.run(ema_op)
    print sess.run([w1, ema.average(w1)])

    #模拟出100轮迭代后参数w1变为10
    sess.run(tf.assign(globa_step,100))
    sess.run(tf.assign(w1,10))
    sess.run(ema_op)
    print sess.run([w1, ema.average(w1)])

    #每次运行,滑动平均值都是不一样的
    sess.run(ema_op)
    print sess.run([w1, ema.average(w1)])

    sess.run(ema_op)
    print sess.run([w1, ema.average(w1)])

    for i in range(400):
        sess.run(ema_op)
        print sess.run([w1, ema.average(w1)])
代码输出为:
[0.0, 0.0]
[1.0, 0.9]
[10.0, 1.6445453]
[10.0, 2.3281732]
[10.0, 2.955868]
[10.0, 3.532206]
[10.0, 4.061389]
[10.0, 4.547275]
[10.0, 4.9934072]
[10.0, 5.4030375]
[10.0, 5.7791524]
[10.0, 6.1244946]
[10.0, 6.4415812]
[10.0, 6.7327247]
[10.0, 7.000047]
[10.0, 7.2454977]
[10.0, 7.470866]
[10.0, 7.6777954]
[10.0, 7.867794]
[10.0, 8.042247]
[10.0, 8.202427]
[10.0, 8.349501]
[10.0, 8.484542]
[10.0, 8.608534]
[10.0, 8.722381]
[10.0, 8.826913]
[10.0, 8.922893]
[10.0, 9.01102]
[10.0, 9.091936]
[10.0, 9.166232]
[10.0, 9.234449]
[10.0, 9.297086]
[10.0, 9.354597]
[10.0, 9.407403]
[10.0, 9.455888]
[10.0, 9.500406]
[10.0, 9.541282]
[10.0, 9.578814]
[10.0, 9.613275]
[10.0, 9.644916]
[10.0, 9.673968]
[10.0, 9.700644]
[10.0, 9.725137]
[10.0, 9.747625]
[10.0, 9.768274]
[10.0, 9.787233]
[10.0, 9.804642]
[10.0, 9.820625]
[10.0, 9.835301]
[10.0, 9.848777]
[10.0, 9.86115]
[10.0, 9.87251]
[10.0, 9.882941]
[10.0, 9.892519]
[10.0, 9.901313]
[10.0, 9.909388]
[10.0, 9.916801]
[10.0, 9.923609]
[10.0, 9.929859]
[10.0, 9.935598]
[10.0, 9.940867]
[10.0, 9.945705]
[10.0, 9.950148]
[10.0, 9.9542265]
[10.0, 9.957972]
[10.0, 9.9614105]
[10.0, 9.964568]
[10.0, 9.967467]
[10.0, 9.970129]
[10.0, 9.972573]
[10.0, 9.974817]
[10.0, 9.976877]
[10.0, 9.978769]
[10.0, 9.980506]
[10.0, 9.9821005]
[10.0, 9.983565]
[10.0, 9.98491]
[10.0, 9.986145]
[10.0, 9.987279]
[10.0, 9.988319]
[10.0, 9.989275]
[10.0, 9.990152]
[10.0, 9.990958]
[10.0, 9.991698]
[10.0, 9.992377]
[10.0, 9.993001]
[10.0, 9.993573]
[10.0, 9.994099]
[10.0, 9.994581]
[10.0, 9.995025]
[10.0, 9.995432]
[10.0, 9.995806]
[10.0, 9.996149]
[10.0, 9.996464]
[10.0, 9.996753]
[10.0, 9.997019]
[10.0, 9.997263]
[10.0, 9.997487]
[10.0, 9.997693]
[10.0, 9.997882]
[10.0, 9.998055]
[10.0, 9.998215]
[10.0, 9.998361]
[10.0, 9.998495]
[10.0, 9.998618]
[10.0, 9.998732]
[10.0, 9.998836]
[10.0, 9.998931]
[10.0, 9.999019]
[10.0, 9.999099]
[10.0, 9.999172]
[10.0, 9.99924]
[10.0, 9.999302]
[10.0, 9.999359]
[10.0, 9.999412]
[10.0, 9.999459]
[10.0, 9.999503]
[10.0, 9.999544]
[10.0, 9.999581]
[10.0, 9.999616]
[10.0, 9.999647]
[10.0, 9.999676]
[10.0, 9.999702]
[10.0, 9.999727]
[10.0, 9.999749]
[10.0, 9.99977]
[10.0, 9.999789]
[10.0, 9.999806]
[10.0, 9.999823]
[10.0, 9.999837]
[10.0, 9.99985]
[10.0, 9.999863]
[10.0, 9.999874]
[10.0, 9.999885]
[10.0, 9.999894]
[10.0, 9.999903]
[10.0, 9.99991]
[10.0, 9.999918]
[10.0, 9.999925]
[10.0, 9.99993]
[10.0, 9.999936]
[10.0, 9.999941]
[10.0, 9.999946]
[10.0, 9.99995]
[10.0, 9.999954]
[10.0, 9.999958]
[10.0, 9.999962]
[10.0, 9.999965]
[10.0, 9.999968]
[10.0, 9.99997]
[10.0, 9.999973]
[10.0, 9.999975]
[10.0, 9.999977]
[10.0, 9.999979]
[10.0, 9.999981]
[10.0, 9.999983]
[10.0, 9.999984]
[10.0, 9.999985]
[10.0, 9.999986]
[10.0, 9.999987]
[10.0, 9.999988]
[10.0, 9.999989]
[10.0, 9.9999895]
[10.0, 9.99999]
[10.0, 9.999991]
[10.0, 9.999992]
[10.0, 9.999993]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]
[10.0, 9.999994]

通过以上输出可以得出,随着训练轮数的增加,滑动平均值越来越趋近于w1的初始值。

本页面支持繁体中文友好显示:滑动平均值的tensorflow实践

如果觉得我的文章对您有用,请随意打赏。如果有其他问题请联系博主QQ(909491009)或者下方留言!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注