0

我写了一个余弦退火LRScheduler:

class CosineAnnealingLRScheduler(optimizers.schedules.LearningRateSchedule):
def __init__(self, epochs, train_step, lr_max, lr_min, warmth_rate=0.2):
    super(CosineAnnealingLRScheduler, self).__init__()

    self.total_step = epochs * train_step
    self.warm_step = int(self.total_step * warmth_rate)
    self.lr_max = lr_max
    self.lr_min = lr_min

@tf.function
def __call__(self, step):
    if step < self.warm_step:
        lr = self.lr_max / self.warm_step * step
    else:
        lr = self.lr_min + 0.5 * (self.lr_max - self.lr_min) * (1.0 + tf.cos((step - self.warm_step) / self.total_step * np.pi))

    return lr

我想知道学习率是否正确衰减,所以我写了一个指标来显示我的 lr:

def print_lr(optimizer):
def lr(y_true, y_pred):
    return optimizer._decayed_lr("float32")
return lr

输出是:

纪元 1/10 488/488 [===============================] - 51 秒 104 毫秒/步 - 损失:2.0970 - lr :2.5051e-05 - val_loss:1.3853 - val_lr:5.0000e-05

纪元 2/10 488/488 [===============================] - 50 秒 102 毫秒/步 - 损失:1.1636 - lr :7.5051e-05 - val_loss:1.2225 - val_lr:1.0000e-04

所以我想知道 keras model.fit() 是否验证调用优化器?我认为优化器应该只在训练阶段而不是在验证阶段被调用。

4

0 回答 0