Tensorflow 中滑动平均恢复到原变量的方法

通过saver将滑动平均 恢复到变量采用这样的方法:
saver = tf.train.Saver( ema.variables_to_restore())
或用用字典算法:
saver = tf.train.Saver( {"w1/ExponentialMovingAverage":w1} )
字典法其实还可以恢复到其他变量:saver = tf.train.Saver( {"w1/ExponentialMovingAverage":w2} )  #w1/movAvg=>w2 


#coding:utf-8
import tensorflow as tf

model_path = './VarAndMovAvg/'
model_name = 'ckpt'

def saveVarAndMovAvg() :
    #1. 定义变量及滑动平均类
    w1 = tf.Variable(0, dtype=tf.float32 , name = 'w1')
    global_step = tf.Variable(0, trainable=False)
    MOVING_AVERAGE_DECAY = 0.99
    ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
    ema_op = ema.apply(tf.trainable_variables())
    
    #2. 查看不同迭代中变量取值的变化。
    with tf.Session() as sess:
        # 初始化
        init_op = tf.global_variables_initializer()
        sess.run(init_op)    
        gA = tf.assign_add( global_step , 20  )
        task = [gA , ema_op]
        # 更新global_step和w1的值,模拟出轮数为100时,参数w1变为10, 以下代码global_step保持为100,每次执行滑动平均操作,影子值会更新 
        sess.run(tf.assign(global_step, 100))  
        sess.run(tf.assign(w1, 10))
        #print ('w1 start' , sess.run( w1)  )
        sess.run(task)        
        sess.run(task)    
        sess.run(task)
        sess.run(task)
        print ("current global_step:" , sess.run(global_step))
        print ("current w1:", sess.run([w1, ema.average(w1)]))
        
        saver = tf.train.Saver() 
        saver.save(sess , model_path+model_name , global_step = global_step  )

def restoreVar():
    w1 = tf.Variable(0, dtype=tf.float32 , name='w1')
    global_step = tf.Variable(180, trainable=False)
    MOVING_AVERAGE_DECAY = 0.99
    ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
    sess = tf.Session() 
    init_op = tf.global_variables_initializer()
    sess.run(init_op) 
    print( sess.run(global_step) ) 
    saver = tf.train.Saver()    
    ckpt = tf.train.get_checkpoint_state( model_path  )
    saver.restore( sess , ckpt.model_checkpoint_path ) 
    print( 'var:', sess.run(w1))

        
def restoreMovAvg2VarUsingDict():
    w2 = tf.Variable(0, dtype=tf.float32 ) #, name = 'w1')
    global_step = tf.Variable(180, trainable=False)
    MOVING_AVERAGE_DECAY = 0.99
    ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
    sess = tf.Session() 
    init_op = tf.global_variables_initializer()
    sess.run(init_op) 
    print( sess.run(global_step) ) 
    saver = tf.train.Saver( {"w1/ExponentialMovingAverage":w2} ) # ema.variables_to_restore())    
    ckpt = tf.train.get_checkpoint_state( model_path  )
    saver.restore( sess , ckpt.model_checkpoint_path ) 
    print('MovAvg2var:' ,  sess.run(w2))
    
def restoreMovAvg2Var():
    w1 = tf.Variable(0, dtype=tf.float32  , name = 'w1')
    global_step = tf.Variable(180, trainable=False)
    MOVING_AVERAGE_DECAY = 0.99
    ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
    sess = tf.Session() 
    init_op = tf.global_variables_initializer()
    sess.run(init_op) 
    print( sess.run(global_step) ) 
    saver = tf.train.Saver( ema.variables_to_restore())    
    ckpt = tf.train.get_checkpoint_state( model_path  )
    saver.restore( sess , ckpt.model_checkpoint_path ) 
    print('MovAvg2var:' ,  sess.run(w1))

    
    
#saveVarAndMovAvg()
#restoreVar()
#restoreMovAvg2Var()
restoreMovAvg2VarUsingDict()

 

你可能感兴趣的:(tensorflow)