深度学习模型花费时间大多很长, 如果一次训练过程意外中断, 那么后续时间再跑就浪费很多时间. 这一次练习中, 我们利用 Keras checkpoint 深度学习模型在训练过程模型, 我的理解是检查训练过程, 将好的模型保存下来. 如果训练过程意外中断, 那么我们可以加载最近一次的文件, 继续进行训练, 这样以前运行过的就可以忽略.
那么如何 checkpoint 呢, 通过练习来了解.
- 数据: Pima diabete 数据
- 神经网络拓扑结构: 8-12-8-1
1.效果提升检查
如果神经网络在训练过程中, 其训练效果有所提升, 则将该次模型训练参数保存下来.
代码
:
# -*- coding: utf-8 -*-
# Checkpoint NN model imporvements
from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import ModelCheckpoint
import numpy as np
import urllib
url = "http://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data"
raw_data = urllib.urlopen(url)
dataset = np.loadtxt(raw_data, delimiter=",")
X = dataset[:, 0:8]
y = dataset[:, 8]
seed = 42
np.random.seed(seed)
# create model
model = Sequential()
model.add(Dense(12, input_dim=8, init='uniform', activation='relu'))
model.add(Dense(8, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))
# compile
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# checkpoint
filepath = "weights-improvement-{epoch:02d}-{val_acc:.2f}.hdf5"
# 中途训练效果提升, 则将文件保存, 每提升一次, 保存一次
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True,
mode='max')
callbacks_list = [checkpoint]
# Fit
model.fit(X, y, validation_split=0.33, nb_epoch=150, batch_size=10,
callbacks=callbacks_list, verbose=0)
部分结果
:
Epoch 00139: val_acc did not improve
Epoch 00140: val_acc improved from 0.70472 to 0.71654, saving model to weights-improvement-140-0.72.hdf5
Epoch 00141: val_acc did not improve
Epoch 00142: val_acc did not improve
Epoch 00143: val_acc did not improve
Epoch 00144: val_acc did not improve
Epoch 00145: val_acc did not improve
Epoch 00146: val_acc did not improve
Epoch 00147: val_acc did not improve
Epoch 00148: val_acc did not improve
Epoch 00149: val_acc did not improve
在运行程序的本地文件夹下, 我们会发现许多性能提升时, 程序自动保存的 hdf5 文件.
转自:https://anifacc.github.io/deeplearning/machinelearning/python/2017/08/30/dlwp-ch14-keep-best-model-checkpoint/