如何创建Keras学习费率时间表,该时间表基于批次而不是时期更新

我正在与Keras一起工作,并试图创建一个学习率计划程序,该程序根据处理的批次数而不是时期数进行计划。为此,我已将调度代码插入“优化器”的get_updates方法中。在大多数情况下,我尝试将常规Python变量用于在给定训练运行期间保持恒定的值,而将计算图节点仅用于实际变化的参数。

我的2个问题是:

  1. 如果放在get_updates Keras的{​​{1}}方法中,下面的代码是否应该像学习率调度程序一样正常工作。

  2. 如何将这段代码嵌入到类似于Optimizer的类中,但是该类是根据批次数而不是历元数计划的?


LearningRateScheduler
zhy1234567 回答:如何创建Keras学习费率时间表,该时间表基于批次而不是时期更新

比弄乱Keras源代码更容易(有可能,但是又复杂又明智),您可以使用回调。

from keras.callbacks import LambdaCallback

total_batches = 0
def what_to_do_when_batch_ends(batch,logs):
   total_batches += 1 #or use the "batch" variable,#which is the batch index of the last finished batch

   #change learning rate at will
   if your_condition == True:
       keras.backend.set_value(model.optimizer.lr,newLrValueAsPythonFloat)

训练时,使用回调:

lrUpdater = LambdaCallback(on_batch_end = what_to_do_when_batch_ends)
model.fit(........,callbacks = [lrUpdater,...other callbacks...])
本文链接:https://www.f2er.com/2497209.html

大家都在问