tf.train.ExponentialMovingAverage用法

本文通过示例展示了如何使用TensorFlow实现学习率的指数衰减,并比较了不同衰减方式的效果。同时介绍了如何利用ExponentialMovingAverage进行变量的滑动平均。
import tensorflow as tf 
 
import matplotlib.pyplot as plt 

learning_rate = tf.Variable(initial_value=0.9,dtype=tf.float32)
learning_rate1 = tf.Variable(0.9,dtype=tf.float32)
decay_rate = 0.99 
global_steps = 1000  
decay_steps = 100  

global_ = tf.placeholder(tf.int32)
c = tf.train.exponential_decay(learning_rate, global_, decay_steps, decay_rate, staircase=True)  
d = tf.train.exponential_decay(learning_rate, global_, decay_steps, decay_rate, staircase=False)  
ema = tf.train.ExponentialMovingAverage(0.99)
ema_apply=ema.apply([learning_rate1])
T_C = []  
T_D = []  
T_E = []
with tf.Session() as sess:  
    sess.run(tf.global_variables_initializer())
    sess.run(tf.assign(learning_rate1,0.85))
    for i in range(global_steps): 
        
        sess.run(ema_apply)
        T_c,T_d=sess.run([c,d],feed_dict={global_: i})  
        _,T_e=sess.run([learning_rate1,ema.average(learning_rate1)])
        T_C.append(T_c)  
        T_D.append(T_d) 
        T_E.append(T_e)


plt.figure(1) 
  
plt.plot(range(global_steps), T_C, 'b-')
plt.plot(range(global_steps), T_D, 'r-')   
plt.plot(range(global_steps), T_E, 'g-') 

plt.axis([0,1000,0.6,1])
plt.show()
import tensorflow as tf

v1 = tf.placeholder(tf.float64)

ema = tf.train.ExponentialMovingAverage(0.99) #这里的第二个参数如果指定了可以加快迭代速度。但是会更复杂
maintain_average = ema.apply([v1])
 
with tf.Session() as sess:
  init = tf.global_variables_initializer()
  sess.run(init, feed_dict={v1:5})
  for i in range(10):
    print(sess.run([maintain_average, v1, ema.average(v1)], feed_dict={v1:5}))
import time import tensorflow.compat.v1 as tf tf.disable_v2_behavior() from tensorflow.examples.tutorials.mnist import input_data import mnist_inference import mnist_train tf.compat.v1.reset_default_graph() EVAL_INTERVAL_SECS = 10 def evaluate(mnist): with tf.Graph().as_default() as g: #定义输入与输出的格式 x = tf.compat.v1.placeholder(tf.float32, [None, mnist_inference.INPUT_NODE], name='x-input') y_ = tf.compat.v1.placeholder(tf.float32, [None, mnist_inference.OUTPUT_NODE], name='y-input') validate_feed = {x: mnist.validation.images, y_: mnist.validation.labels} #直接调用封装好的函数来计算前向传播的结果 y = mnist_inference.inference(x, None) #计算正确率 correcgt_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correcgt_prediction, tf.float32)) #通过变量重命名的方式加载模型 variable_averages = tf.train.ExponentialMovingAverage(0.99) variable_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variable_to_restore) #每隔10秒调用一次计算正确率的过程以检测训练过程中正确率的变化 while True: with tf.compat.v1.Session() as sess: ckpt = tf.train.get_checkpoint_state(minist_train.MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: #load the model saver.restore(sess, ckpt.model_checkpoint_path) global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] accuracy_score = sess.run(accuracy, feed_dict=validate_feed) print("After %s training steps, validation accuracy = %g" % (global_step, accuracy_score)) else: print('No checkpoint file found') return time.sleep(EVAL_INTERVAL_SECS) def main(argv=None): mnist = input_data.read_data_sets(r"D:\Anaconda123\Lib\site-packages\tensorboard\mnist", one_hot=True) evaluate(mnist) if __name__ == '__main__': tf.compat.v1.app.run()对代码进行改进
05-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值