'''
学习率较大容易搜索震荡(在最优值附近徘徊),学习率较小则收敛速度较慢,
那么可以通过初始定义一个较大的学习率,通过设置decay_rate来缩小学习率,减少迭代次数。
tf.train.exponential_decay就是用来实现这个功能。
'''
__author__ =
'Zhang Shuai'
import tensorflow
as tf
import matplotlib.pyplot
as plt
learning_rate =
0.1
decay_rate =
0.96
global_steps =
1000
decay_steps =
100
global_ = tf.placeholder(dtype=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)
T_C = []
F_D = []
with tf.Session()
as sess:
for i
in range(global_steps):
T_c = sess.run(c, feed_dict={global_: i})
T_C.append(T_c)
F_d = sess.run(d, feed_dict={global_: i})
F_D.append(F_d)
plt.figure(
1)
l1, = plt.plot(range(global_steps), F_D,
'r-')
l2, = plt.plot(range(global_steps), T_C,
'b-')
plt.legend(handles=[l1, l2, ], labels=[
'staircase=False',
'staircase=True'],
loc=
'best', )
plt.show()
结果如图: