tf.train.exponential

xiaoxiao2021-02-28  47

#!/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://www.6miu.com/read-2623818.html

最新回复(0)