<jupyter_start><jupyter_text>https://machinelearningmastery.com/check-point-deep-learning-models-keras/<jupyter_code>!wget http://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data
from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import ModelCheckpoint
import matplotlib.pyplot as plt
import numpy as np

seed = 7
np.random.seed(seed)

# load pima indians dataset
dataset = np.loadtxt('pima-indians-diabetes.data', delimiter=',')
X = dataset[:, 0:8]
Y = dataset[:, 8]

# create model
model = Sequential()
model.add(Dense(12, input_dim=8, kernel_initializer='uniform', activation='relu'))
model.add(Dense(8, kernel_initializer='uniform', activation='relu'))
model.add(Dense(1, kernel_initializer='uniform', activation='sigmoid'))

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

filepath = 'weights-improvement-{epoch:03d}-{val_acc:.2f}.hdf5'
checkpoint = ModelCheckpoint(filepath,
                             monitor='val_acc',
                             verbose=1,
                             save_best_only=True,
                             mode='max')
callbacks_list = [checkpoint]
model.fit(X, Y, validation_split=0.33, epochs=150, batch_size=10,
          callbacks=callbacks_list, verbose=0)
ls *.hdf5<jupyter_output>weights-improvement-000-0.67.hdf5  weights-improvement-052-0.75.hdf5
weights-improvement-003-0.69.hdf5  weights-improvement-062-0.75.hdf5
weights-improvement-004-0.71.hdf5  weights-improvement-071-0.76.hdf5
weights-improvement-009-0.71.hdf5  weights-improvement-077-0.77.hdf5
weights-improvement-011-0.72.hdf5  weights-improvement-083-0.77.hdf5
weights-improvement-013-0.72.hdf5  weights-improvement-095-0.78.hdf5
weights-improvement-017-0.73.hdf5  weights-improvement-100-0.78.hdf5
weights-improvement-029-0.74.hdf5  weights-improvement-101-0.78.hdf5
weights-improvement-036-0.74.hdf5  weights-improvement-105-0.78.hdf5
weights-improvement-037-0.74.hdf5  weights-improvement-113-0.79.hdf5
weights-improvement-038-0.74.hdf5  weights-improvement-141-0.79.hdf5
weights-improvement-047-0.74.hdf5  weights-improvement-142-0.80.hdf5<jupyter_text>Checkpoint Best Neural Network Model Only<jupyter_code>from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import ModelCheckpoint
import matplotlib.pyplot as plt
import numpy as np

seed = 7
np.random.seed(seed)

# load pima indians dataset
dataset = np.loadtxt('pima-indians-diabetes.data', delimiter=',')
X = dataset[:, 0:8]
Y = dataset[:, 8]

# create model
model = Sequential()
model.add(Dense(12, input_dim=8, kernel_initializer='uniform', activation='relu'))
model.add(Dense(8, kernel_initializer='uniform', activation='relu'))
model.add(Dense(1, kernel_initializer='uniform', activation='sigmoid'))

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

filepath = 'weights.best.hdf5'
checkpoint = ModelCheckpoint(filepath,
                             monitor='val_acc',
                             verbose=1,
                             save_best_only=True,
                             mode='max')
callbacks_list = [checkpoint]
model.fit(X, Y, validation_split=0.33, epochs=150, batch_size=10,
          callbacks=callbacks_list, verbose=0)<jupyter_output>Epoch 00000: val_acc improved from -inf to 0.67323, saving model to weights.best.hdf5
Epoch 00001: val_acc did not improve
Epoch 00002: val_acc did not improve
Epoch 00003: val_acc did not improve
Epoch 00004: val_acc improved from 0.67323 to 0.70472, saving model to weights.best.hdf5
Epoch 00005: val_acc did not improve
Epoch 00006: val_acc did not improve
Epoch 00007: val_acc did not improve
Epoch 00008: val_acc did not improve
Epoch 00009: val_acc did not improve
Epoch 00010: val_acc did not improve
Epoch 00011: val_acc improved from 0.70472 to 0.71260, saving model to weights.best.hdf5
Epoch 00012: val_acc did not improve
Epoch 00013: val_acc improved from 0.71260 to 0.72047, saving model to weights.best.hdf5
Epoch 00014: val_acc did not improve
Epoch 00015: val_acc did not improve
Epoch 00016: val_acc did not improve
Epoch 00017: val_acc did not improve
Epoch 00018: val_acc did not improve
Epoch 00019: val_acc did not improve
Epoch 00020: val_acc did not improve
Epoch 00021: val_a[...]<jupyter_text>Load weights<jupyter_code>from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import ModelCheckpoint
import matplotlib.pyplot as plt
import numpy as np

seed = 7
np.random.seed(seed)

# create model
# model = Sequential()
# model.add(Dense(12, input_dim=8, kernel_initializer='uniform', activation='relu'))
# model.add(Dense(8, kernel_initializer='uniform', activation='relu'))
# model.add(Dense(1, kernel_initializer='uniform', activation='sigmoid'))
# model.load_weights('weights.best.hdf5')

# モデル構造も含まれるので下記でOK
model = load_model('weights.best.hdf5')

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

dataset = np.loadtxt('pima-indians-diabetes.data', delimiter=',')
X = dataset[:, 0:8]
Y = dataset[:, 8]
scores = model.evaluate(X, Y, verbose=0)
print('%s: %.2f%%' % (model.metrics_names[1], scores[1] * 100))<jupyter_output>acc: 77.99%
{'fraction_non_alphanumeric': 0.12158054711246201, 'fraction_numerical': 0.07408814589665653, 'mean_word_length': 4.601063829787234, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '490746', 'license': 'MIT', 'n_tokens_mistral': 2102, 'n_tokens_neox': 1861, 'n_words': 386, 'path': 'keras/180102-checkpoint.ipynb', 'repo_name': 'aidiary/notebooks', 'size': '5264'}