tf.train.exponential_decay(指數學習率衰減)
#!/usr/bin/env python3
# -*- coding: utf-8 -*- '''
學習率較大容易搜尋震盪(在最優值附近徘徊),學習率較小則收斂速度較慢,
那麼可以通過初始定義一個較大的學習率,通過設定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 # 衰減速率,即每一次學習都衰減為原來的0.96
global_steps = 1000 # 總學習次數
# 如果staircase為True,那麼每decay_steps改變一次learning_rate,
# 改變為learning_rate*(decay_rate**decay_steps)
# 如果為False則,每一步都改變,為learning_rate*decay_rate
decay_steps = 100
global_ = tf.placeholder(dtype=tf.int32)
# 如果staircase=True,那麼每decay_steps更新一次decay_rate,如果是False那麼每一步都更新一次decay_rate。
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-') # staircase=False
l2, = plt.plot(range(global_steps), T_C, 'b-') # staircase=True
plt.legend(handles=[l1, l2, ], labels=['staircase=False', 'staircase=True'], loc='best', )
plt.show()
結果如圖:
---------------------
原文:https://blog.csdn.net/u013061183/article/details/79334697