import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
learning_rate = 0.001
decay_rate = 0.001#这个太小了
global_steps = 60
decay_steps = 5
#staircase=False, 默认为False,为True则不衰减
global_step = tf.Variable(0)
lr1 = tf.exponential_decay(
learning_rate = learning_rate,
global_step = global_step,
decay_steps = decay_steps,
decay_rate = decay_rate,
staircase = True
#tf.train.exponential_decay还有一个参数是staircase,默认为False,如果是False,学习率会平滑地衰减,而非每迭代1000轮才衰减一次,如果为True,则会每迭代1000次才衰减一次,这种称为阶梯函数。
)
lr2 = tf.exponential_decay(
learning_rate = learning_rate,
global_step = global_step,
decay_steps = decay_steps,
decay_rate = decay_rate,
staircase = False
)
LR1 = []
LR2 = []
with tf.Session() as sess:
for i in range(global_steps):
LR1.append(sess.run(lr1, feed_dict = {global_step: i}))
LR2.append(sess.run(lr2, feed_dict = {global_step: i}))
plt.figure(1)
plt.plot(range(global_steps), LR1, 'r-')
plt.plot(range(global_steps), LR2, 'b-')
plt.show()
decayed_learning_rate =
learning_rate * decay_rate ^(global_step / decay_ steps)
红色表示staircase=True,绿色表示staircase=False。
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
learning_rate = 0.001
decay_rate = 0.001
global_steps = 60
decay_steps = 60
global_step = tf.Variable(0)
lr1 = tf.exponential_decay(
learning_rate = learning_rate,
global_step = global_step,
decay_steps = decay_steps,
decay_rate = decay_rate,
staircase = True
)
lr2 = tf.exponential_decay(
learning_rate = learning_rate,
global_step = global_step,
decay_steps = decay_steps,
decay_rate = decay_rate,
staircase = False
)
LR1 = []
LR2 = []
with tf.Session() as sess:
for i in range(global_steps):
LR1.append(sess.run(lr1, feed_dict = {global_step: i}))
LR2.append(sess.run(lr2, feed_dict = {global_step: i}))
plt.figure(1)
plt.plot(range(global_steps), LR1, 'r-')
plt.plot(range(global_steps), LR2, 'b-')
plt.show()
问题:tf.train.exponential_decay
在tf2.0里面去掉这个API了,但是还不知道代替的是啥