tf.train.exponential_decay
生活随笔
收集整理的這篇文章主要介紹了
tf.train.exponential_decay
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
函數(shù)
- tf.train.exponential_decay
tf.train.exponential_decay(learning_rate, global_step, decay_steps, decay_rate, staircase=False, name=None)功能:實現(xiàn)指數(shù)衰減學(xué)習(xí)率內(nèi)部代碼實現(xiàn)
decayed_learning_rate = learning_rate*decay_rate^(global_step/decay_steps)其中decayed_learning_rate為每一輪優(yōu)化時使用的學(xué)習(xí)率;
learning_rate為事先設(shè)定的初始學(xué)習(xí)率
decay_rate為衰減系數(shù);
decay_steps為衰減速度。
staircase=True時,global_step/decay_step會被轉(zhuǎn)換為整數(shù)
具體代碼用法
# 代碼段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,如何使用該函數(shù)在最小算法上 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)第一段代碼的結(jié)果
?
解釋:
第二段代碼的含義是設(shè)置初始學(xué)習(xí)率為0.02,衰減速度為200輪,衰減系數(shù)為0.96, 即每訓(xùn)練200輪后學(xué)習(xí)率乘以0.96;衰減速度 = 總實例數(shù) / batch; 初始學(xué)習(xí)率、衰減學(xué)習(xí)率、衰減系數(shù)這些都是根據(jù)經(jīng)驗設(shè)置的?
使用tf.train.exponential_decay的好處
我們都知道,學(xué)習(xí)率的設(shè)置既不能過大,也不能過小。為了解決學(xué)習(xí)率的問題,TensorFlow提供了一種更加靈活的學(xué)習(xí)率設(shè)置方法——指數(shù)衰減法。
通過這個函數(shù),我們可以在設(shè)置初始學(xué)習(xí)率的基礎(chǔ)上,隨著迭代的繼續(xù)逐步減小學(xué)習(xí)率,使得模型在訓(xùn)練后期更加穩(wěn)定。
參考
https://blog.csdn.net/wuguangbin1230/article/details/77658229
https://blog.csdn.net/uestc_c2_403/article/details/72213286
記錄時間
2018/9/11 20:55總結(jié)
以上是生活随笔為你收集整理的tf.train.exponential_decay的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: tensorflow常见函数——clip
- 下一篇: tf.nn.sparse_softmax