学习率衰减的 epoch 数
学习率衰减(Learning Rate Decay)是一种在训练过程中动态调整学习率的策略,旨在提高模型的训练效率和收敛性能。通过在训练过程中逐渐减小学习率,可以使模型在训练初期快速收敛,并在训练后期更精细地调整参数,从而提高模型的最终性能。
1. 学习率衰减的常见策略
1.1 逐步衰减(Step Decay)
在每个固定的时间间隔(如每个 epoch)减少学习率。例如,每 30 个 epoch 将学习率减半。
Python
def step_decay(epoch):
initial_lr = 0.1
drop = 0.5
epochs_drop = 30.0
return initial_lr * (drop ** (epoch // epochs_drop))
1.2 指数衰减(Exponential Decay)
学习率按指数衰减。例如,学习率每 epoch 减少 10%。
Python
def exponential_decay(epoch):
initial_lr = 0.1
k = 0.1
return initial_lr * np.exp(-k * epoch)
1.3 自适应衰减(Adaptive Decay)
根据验证集的性能动态调整学习率。例如,当验证集的损失不再下降时,减少学习率。
Python
from tensorflow.keras.callbacks import ReduceLROnPlateau
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=1)
2. 如何选择学习率衰减的 epoch 数
选择学习率衰减的 epoch 数是一个重要的超参数调整问题。以下是一些常见的方法和建议:
2.1 网格搜索(Grid Search)
尝试一系列预定义的 epoch 数,选择在验证集上表现最好的值。例如,每 10、20、30 个 epoch 衰减一次。
Python
epochs_list = [10, 20, 30, 50]
best_epochs = None
best_score = float('inf')
for epochs in epochs_list:
model = MyModel()
model.train(X_train, y_train, epochs=epochs)
score = model.evaluate(X_val, y_val)
if score < best_score:
best_score = score
best_epochs = epochs
print(f"Best epochs for decay: {best_epochs}")
2.2 随机搜索(Random Search)
在一定范围内随机选择 epoch 数,选择在验证集上表现最好的值。
Python
import numpy as np
epochs_list = np.random.randint(10, 50, 10)
best_epochs = None
best_score = float('inf')
for epochs in epochs_list:
model = MyModel()
model.train(X_train, y_train, epochs=epochs)
score = model.evaluate(X_val, y_val)
if score < best_score:
best_score = score
best_epochs = epochs
print(f"Best epochs for decay: {best_epochs}")
2.3 基于验证集的动态调整
使用自适应学习率调度器,如
ReduceLROnPlateau,根据验证集的性能动态调整学习率。这种方法不需要预先指定 epoch 数。Python
from tensorflow.keras.callbacks import ReduceLROnPlateau
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=1)
model.fit(X_train, y_train, epochs=100, validation_data=(X_val, y_val), callbacks=[reduce_lr])
3. 实现示例
3.1 PyTorch
在 PyTorch 中,可以使用
StepLR 或 ExponentialLR 调度器来实现学习率衰减。Python
import torch
import torch.optim as optim
from torch.optim.lr_scheduler import StepLR
# 定义模型和优化器
model = MyModel()
optimizer = optim.SGD(model.parameters(), lr=0.1)
# 定义学习率调度器
scheduler = StepLR(optimizer, step_size=30, gamma=0.1)
# 训练模型
for epoch in range(100):
model.train()
# 训练代码
scheduler.step()
3.2 TensorFlow/Keras
在 TensorFlow/Keras 中,可以使用
LearningRateScheduler 或 ReduceLROnPlateau 调度器。Python
import tensorflow as tf
from tensorflow.keras.callbacks import LearningRateScheduler, ReduceLROnPlateau
# 定义学习率调度器
def step_decay(epoch):
initial_lr = 0.1
drop = 0.5
epochs_drop = 30.0
return initial_lr * (drop ** (epoch // epochs_drop))
lr_scheduler = LearningRateScheduler(step_decay)
# 或者使用自适应调度器
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=1)
# 编译模型
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# 训练模型
model.fit(X_train, y_train, epochs=100, validation_data=(X_val, y_val), callbacks=[lr_scheduler, reduce_lr])
4. 总结
-
学习率衰减:通过在训练过程中动态调整学习率,可以使模型在训练初期快速收敛,并在训练后期更精细地调整参数。
-
选择 epoch 数:可以通过网格搜索、随机搜索或基于验证集的动态调整来选择合适的学习率衰减 epoch 数。
-
实现:在 PyTorch 和 TensorFlow/Keras 中,可以使用内置的学习率调度器来实现学习率衰减。
选择合适的学习率衰减策略和 epoch 数是提高模型性能的关键步骤之一。
浙公网安备 33010602011771号