tf.train.exponential_decay
生活随笔
收集整理的這篇文章主要介紹了
tf.train.exponential_decay
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
函數
- tf.train.exponential_decay
tf.train.exponential_decay(learning_rate, global_step, decay_steps, decay_rate, staircase=False, name=None)功能:實現指數衰減學習率內部代碼實現
decayed_learning_rate = learning_rate*decay_rate^(global_step/decay_steps)其中decayed_learning_rate為每一輪優化時使用的學習率;
learning_rate為事先設定的初始學習率
decay_rate為衰減系數;
decay_steps為衰減速度。
staircase=True時,global_step/decay_step會被轉換為整數
具體代碼用法
# 代碼段1,直觀感受staircase=False和staircase=True的情況 import tensorflow as tf import matplotlib.pyplot as pltlearning_rate = 0.1 decay_rate = 0.96 global_steps = 1000 decay_steps = 100 global_ = tf.Variable(tf.constant(0)) 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) plt.plot(range(global_steps), F_D, 'r-') plt.plot(range(global_steps), T_C, 'b-') plt.show() #代碼段2,如何使用該函數在最小算法上 global_step = tf.Variable(0,trainable = False) starter_learning_rate = 0.02 learning_rate = tf.train.exponential_decay(starter_learning_rate, global_step, 200, 0.96,staircase=True) train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cross_entropy, global_step = global_step)第一段代碼的結果
?
解釋:
第二段代碼的含義是設置初始學習率為0.02,衰減速度為200輪,衰減系數為0.96, 即每訓練200輪后學習率乘以0.96;衰減速度 = 總實例數 / batch; 初始學習率、衰減學習率、衰減系數這些都是根據經驗設置的?
使用tf.train.exponential_decay的好處
我們都知道,學習率的設置既不能過大,也不能過小。為了解決學習率的問題,TensorFlow提供了一種更加靈活的學習率設置方法——指數衰減法。
通過這個函數,我們可以在設置初始學習率的基礎上,隨著迭代的繼續逐步減小學習率,使得模型在訓練后期更加穩定。
參考
https://blog.csdn.net/wuguangbin1230/article/details/77658229
https://blog.csdn.net/uestc_c2_403/article/details/72213286
記錄時間
2018/9/11 20:55總結
以上是生活随笔為你收集整理的tf.train.exponential_decay的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: tensorflow常见函数——clip
- 下一篇: tf.nn.sparse_softmax