如何使用 Keras 中的 Adam 优化器在每个时期打印学习率? [英] How can I print the Learning Rate at each epoch with Adam optimizer in Keras?

查看:86
本文介绍了如何使用 Keras 中的 Adam 优化器在每个时期打印学习率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为当你使用自适应优化器时,在线学习不能很好地与 Keras 配合使用(调用 .fit() 时学习率计划会重置),我想看看我是否可以手动设置它.然而,为了做到这一点,我需要找出最后一个时期的学习率.

Because online learning does not work well with Keras when you are using an adaptive optimizer (the learning rate schedule resets when calling .fit()), I want to see if I can just manually set it. However, in order to do that, I need to find out what the learning rate was at the last epoch.

也就是说,我如何打印每个时期的学习率?我想我可以通过回调来做到这一点,但似乎每次都必须重新计算,我不知道如何对 Adam 进行.

That said, how can I print the learning rate at each epoch? I think I can do it through a callback but it seems that you have to recalculate it each time and I'm not sure how to do that with Adam.

我在另一个帖子中找到了这个,但它只适用于 SGD:

I found this in another thread but it only works with SGD:

class SGDLearningRateTracker(Callback):
    def on_epoch_end(self, epoch, logs={}):
        optimizer = self.model.optimizer
        lr = K.eval(optimizer.lr * (1. / (1. + optimizer.decay * optimizer.iterations)))
        print('
LR: {:.6f}
'.format(lr))

推荐答案

我正在使用以下方法,这是基于@jorijnsmit 的回答:

I am using the following approach, which is based on @jorijnsmit answer:

def get_lr_metric(optimizer):
    def lr(y_true, y_pred):
        return optimizer._decayed_lr(tf.float32) # I use ._decayed_lr method instead of .lr
    return lr

optimizer = keras.optimizers.Adam()
lr_metric = get_lr_metric(optimizer)

model.compile(
    optimizer=optimizer,
    metrics=['accuracy', lr_metric],
    loss='mean_absolute_error', 
    )

它适用于亚当.

这篇关于如何使用 Keras 中的 Adam 优化器在每个时期打印学习率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆