TensorFlow 2.0-学习速率调度器



我使用Python 3.7和TensorFlow 2.0,我必须使用以下学习率调度器训练160个时期的神经网络:

在80和120个时期将学习率降低10倍,其中初始学习率=0.01。

我如何编写一个函数来合并这个学习率调度器:

def scheduler(epoch):
    if epoch < 80:
        return 0.01
    elif epoch >= 80 and epoch < 120:
        return 0.01 / 10
    elif epoch >= 120:
        return 0.01 / 100
callback = tf.keras.callbacks.LearningRateScheduler(scheduler)
model.fit(
    x = data, y = labels,
    epochs=100, callbacks=[callback],
    validation_data=(val_data, val_labels))

这是一个正确的实施吗?

谢谢!

tf.keras.callbacks.LearningRateScheduler期望一个函数以epoch索引作为输入(整数,从0开始索引(,并返回一个新的学习率作为输出(浮点(:

def scheduler(epoch, current_learning_rate):
    if epoch == 79 or epoch == 119:
        return current_learning_rate / 10
    else:
        return min(current_learning_rate, 0.001)

这将使Epochs 80和120的学习率降低10倍,并使其在所有其他时期保持原样。

最新更新