这篇教程Keras的回调函数ReduceLROnPlateau 训练过程优化学习率写得很实用,希望能帮到您。
目的:定义学习率之后,经过一定epoch迭代之后,模型效果不再提升,该学习率可能已经不再适应该模型。需要在训练过程中缩小学习率,进而提升模型。如何在训练过程中缩小学习率呢?我们可以使用keras中的回调函数ReduceLROnPlateau。与EarlyStopping配合使用,会非常方便。
为什么初始化一个非常小的学习率呢?因为初始的学习率过小,会需要非常多次的迭代才能使模型达到最优状态,训练缓慢。如果训练过程中不断缩小学习率,可以快速又精确的获得最优模型。
-
monitor:监测的值,可以是accuracy,val_loss,val_accuracy
-
factor:缩放学习率的值,学习率将以lr = lr*factor的形式被减少
-
patience:当patience个epoch过去而模型性能不提升时,学习率减少的动作会被触发
-
mode:‘auto’,‘min’,‘max’之一 默认‘auto’就行
-
epsilon:阈值,用来确定是否进入检测值的“平原区”
-
cooldown:学习率减少后,会经过cooldown个epoch才重新进行正常操作
-
-
-
Reduce=ReduceLROnPlateau(monitor=
-
-
-
-
-
-
-
使用手写数字mnist作演示,当只设置EarlyStopping的时候,代码及效果如下:
-
-
-
from keras.datasets import mnist
-
from keras.models import Sequential
-
from keras.layers import Dense
-
from keras.layers import Dropout
-
from keras.utils import np_utils
-
from keras.callbacks import EarlyStopping
-
from keras import optimizers
-
from keras.callbacks import ReduceLROnPlateau
-
-
-
-
-
(X_train, y_train), (X_test, y_test) = mnist.load_data(path='mnist.npz')
-
-
num_pixels = X_train.shape[1] * X_train.shape[2]
-
X_train = X_train.reshape(X_train.shape[0], num_pixels).astype('float32')
-
X_test = X_test.reshape(X_test.shape[0], num_pixels).astype('float32')
-
-
-
-
-
-
-
-
-
-
-
-
y_train = np_utils.to_categorical(y_train)
-
y_test = np_utils.to_categorical(y_test)
-
num_classes = y_test.shape[1]
-
-
-
-
-
-
-
model.add(Dense(num_pixels, input_dim=num_pixels, kernel_initializer='normal', activation='relu'))
-
model.add(Dropout(rate=0.5))
-
model.add(Dense(num_classes, kernel_initializer='normal', activation='softmax'))
-
-
adam=optimizers.Adam(learning_rate=0.01)
-
model.compile(loss='categorical_crossentropy',
-
optimizer=adam, metrics=['accuracy'])
-
-
-
-
-
EarlyStop=EarlyStopping(monitor='val_accuracy',
-
patience=2,verbose=1, mode='auto')
-
-
-
-
-
-
-
-
-
-
-
history=model.fit(X_train, y_train, validation_data=(X_test, y_test),
-
epochs=30, batch_size=200,
-
callbacks=[EarlyStop],verbose=2)
-
-
scores = model.evaluate(X_test, y_test, verbose=1)
-
print("Baseline Error: %.2f%%" % (100-scores[1]*100))
效果:训练到5轮就触发早停了。
当使用ReduceLROnPlateau在训练过程中优化减小learning_rate:
-
-
-
from keras.datasets import mnist
-
from keras.models import Sequential
-
from keras.layers import Dense
-
from keras.layers import Dropout
-
from keras.utils import np_utils
-
from keras.callbacks import EarlyStopping
-
from keras import optimizers
-
from keras.callbacks import ReduceLROnPlateau
-
-
-
-
-
(X_train, y_train), (X_test, y_test) = mnist.load_data(path='mnist.npz')
-
-
num_pixels = X_train.shape[1] * X_train.shape[2]
-
X_train = X_train.reshape(X_train.shape[0], num_pixels).astype('float32')
-
X_test = X_test.reshape(X_test.shape[0], num_pixels).astype('float32')
-
-
-
-
-
-
-
-
-
-
-
-
y_train = np_utils.to_categorical(y_train)
-
y_test = np_utils.to_categorical(y_test)
-
num_classes = y_test.shape[1]
-
-
-
-
-
-
-
model.add(Dense(num_pixels, input_dim=num_pixels, kernel_initializer='normal', activation='relu'))
-
model.add(Dropout(rate=0.5))
-
model.add(Dense(num_classes, kernel_initializer='normal', activation='softmax'))
-
-
adam=optimizers.Adam(learning_rate=0.01)
-
model.compile(loss='categorical_crossentropy',
-
optimizer=adam, metrics=['accuracy'])
-
-
-
-
-
EarlyStop=EarlyStopping(monitor='val_accuracy',
-
patience=2,verbose=1, mode='auto')
-
-
Reduce=ReduceLROnPlateau(monitor='val_accuracy',
-
-
-
-
-
-
-
-
-
history=model.fit(X_train, y_train, validation_data=(X_test, y_test),
-
epochs=30, batch_size=200,
-
callbacks=[EarlyStop,Reduce],verbose=2)
-
-
scores = model.evaluate(X_test, y_test, verbose=1)
-
print("Baseline Error: %.2f%%" % (100-scores[1]*100))
-
得到的val_accuracy有所提升,训练轮数会增加。不会过早触发EarlyStooping。当然EarlyStopping的patience要比ReduceLROnPlateau的patience大一些才会有效果。
ubuntu系统下anaconda安装dlib成功-ubuntu系统下安装dlib成功 图像分类经典项目:基于开源数据集Fashion-MNIST的应用实践 |