如果您想为各个时间间隔设置特定的学习率,例如 0 < a < b < c < ...
。然后,您可以将学习率定义为一个条件张量,以全局步长为条件,并将其正常地馈送到优化器。
您可以使用一堆嵌套tf.cond
语句来实现此目的,但是以递归方式构建张量更容易:
def make_learning_rate_tensor(reduction_steps, learning_rates, global_step):
assert len(reduction_steps) + 1 == len(learning_rates)
if len(reduction_steps) == 1:
return tf.cond(
global_step < reduction_steps[0],
lambda: learning_rates[0],
lambda: learning_rates[1]
)
else:
return tf.cond(
global_step < reduction_steps[0],
lambda: learning_rates[0],
lambda: make_learning_rate_tensor(
reduction_steps[1:],
learning_rates[1:],
global_step,)
)
然后,要使用它,您需要知道一个时期中有多少个训练步骤,以便我们可以使用全局步骤在正确的时间切换,并最终定义您想要的时期和学习率。因此,如果我想分别[0.1, 0.01, 0.001, 0.0001]
在每个纪元间隔内学习率[0, 19], [20, 59], [60, 99], [100, \infty]
,我会这样做:
global_step = tf.train.get_or_create_global_step()
learning_rates = [0.1, 0.01, 0.001, 0.0001]
steps_per_epoch = 225
epochs_to_switch_at = [20, 60, 100]
epochs_to_switch_at = [x*steps_per_epoch for x in epochs_to_switch_at ]
learning_rate = make_learning_rate_tensor(epochs_to_switch_at , learning_rates, global_step)
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value beta2_power