天天看點

Keras學習4:Callback

首先給出Keras官方文檔對Callback的解釋:Keras-Callback

回調函數是一組在訓練的特定階段被調用的函數集,你可以使用回調函數來觀察訓練過程中網絡内部的狀态和統計資訊。通過傳遞回調函數清單到模型的.fit()中,即可在給定的訓練階段調用該函數集中的函數。

常用的Callback有兩種:ModelCheckPoint和EarlyStopping

ModelCheckPoint

該回調函數将在每個epoch後根據訓練情況儲存模型到filepath

def __init__(self, filepath, monitor='val_loss', verbose=0,
                 save_best_only=False, save_weights_only=False,
                 mode='auto', period=1):
           

filepath:可以是格式化的字元串,裡面的占位符将會被epoch值和傳入on_epoch_end的logs關鍵字所填入

For example: if

filepath

is

weights.{epoch:02d}-{val_loss:.2f}.hdf5

, then the model checkpoints will be saved with the epoch number and the validation loss in the filename.

比如:filepath=’./model-{epoch:02d}-{val_acc:.2f}.h5’

若目前為epoch 2, val_acc為24%,則filename=model-02-0.24.h5

monitor:螢幕,即需要監視的值,通常為’val_acc’、'val_loss’等

verbose:資訊展示模式,0或1

save_best_only:當設定為True時,将隻儲存在驗證集上性能最好的模型

save_best_only: if

save_best_only=True

, the latest best model according to the quantity monitored will not be overwritten.

mode:‘auto’,‘min’,‘max’之一,在save_best_only=True時決定性能最佳模型的評判準則,例如,當監測值為val_acc時,模式應為max,當檢測值為val_loss時,模式應為min。在auto模式下,評價準則由被監測值的名字自動推斷

save_weights_only:若設定為True,則隻儲存模型權重,否則将儲存整個模型(包括模型結構,配置資訊等)

period:CheckPoint之間的間隔的epoch數,即每隔period個epoch監視一次monitor,決定是否儲存目前模型

EarlyStopping

當監測值不再改善時,該回調函數将中止訓練,即ML中的早停政策(對抗過拟合的技巧之一,面試經常問到~~)

def __init__(self, monitor='val_loss',
                 min_delta=0, patience=0, verbose=0, mode='auto')
           

monitor:需要監視的量,如‘val_acc’、‘val_loss’

min_delta:被視作模型性能上升的最小變化量,小于該變化量認為模型性能沒有提升

minimum change in the monitored quantity to qualify as an improvement, i.e. an absolute change of less than min_delta, will count as no improvement.

patience:當early stop被激活(如發現loss相比上一個epoch訓練沒有下降),則經過patience個epoch後停止訓練

number of epochs with no improvement after which training will be stopped.

verbose:資訊展示模式

mode:‘auto’,‘min’,‘max’之一,在min模式下,如果檢測值停止下降則中止訓練。在max模式下,當檢測值不再上升則停止訓練

一個小例子

# bulid network
inputs = Input(shape=(input_dim, 1))
x = inputs
x = Conv1D(1, int(input_dim/num_classes), dilation_rate=num_classes, padding='valid', use_bias=False, name='conv1', kernel_initializer='random_uniform')(x)
x = Flatten()(x)
x = Activation('softmax')(x)
model = Model(inputs, x)

# compile model
adam = Adam(lr=1e-1, decay=1e-5)
model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy'])

# define Callback
save_best = ModelCheckpoint(filepath='./model/ep{epoch:d}-acc{val_acc:f}.h5', monitor='val_acc', mode='max', verbose=1, save_best_only=True, period=1)
early_stop = EarlyStopping(monitor='val_acc', patience=50, verbose=2, mode='max')

# train
model.fit(x_train, y_train, epochs=max_epoch, batch_size=batch_size, shuffle=True, verbose=2, validation_data=[x_val, y_val], callbacks=[save_best])

# evaluate
val_result = model.evaluate(x_val, y_val, batch_size=batch_size, verbose=0)
print("val_result = {}".format(val_result))

weight = model.get_layer('conv1').get_weights()
print(weight)
# best_model = load_model(best_model_path)
# best_result = best_model.evaluate(x_val, y_val, batch_size=batch_size, verbose=0)
# print("best_result = {}".format(best_result))
           

繼續閱讀