tf.train.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}))
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值