Most deep learning model takes a long time, if a training process is unexpectedly interrupted, then the follow-up time to run again wasted a lot of time. This time exercise, we use Keras checkpoint depth learning model training process model, my understanding is to check the training process to save a good model down. If the training process is unexpectedly interrupted, then we can load a file recently, continued training, so before run-off can be ignored.
So how do checkpoint, through exercises to understand.
- Data: Pima diabete data
- Neural network topology: 8-12-8-1
1. Check the lifting effect
If the neural network in the training process, the training effect has improved, then save the parameters of the model training times down.
代码
:
# -*- 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