我有几个不同模型的保存点,我想在另一台机器(Kaggle)上进行评估,但似乎找不到任何关于如何恢复 hdf5 文件并从该点启动 BayesSearchCV 的文档。
这是我创建 BayesSearchCV 并将回调保存为 .hdf5 的方法
任何帮助深表感谢!
search_space = dict()
search_space["epochs"] = Integer(125, 155, 'uniform')
search_space["batch_size"] = Integer(165, 220, 'uniform')
def bayes_search(filepath, model):
checkpoint_callback = skopt.callbacks.CheckpointSaver(filepath)
NN_start = process_time()
estimator= KerasClassifier (build_fn=model, verbose=0)# , epochs=220, batch_size=256
bayes = BayesSearchCV(estimator, search_spaces=search_space, cv=3)
fit_CV = bayes.fit(X_train, y_train, callback=[checkpoint_callback])
NN_stop = process_time()
print("Elapsed time during BayesCV in minutes:", ((NN_stop - NN_start) /60))
print(fit_CV.best_params_)
return fit_CV, bayes
# Define Model
def buildmodel1(optimizer = 'adam'):
model = Sequential()
model.add(Dense(784, activation='selu',input_dim=784, kernel_initializer="he_normal", use_bias = True))
layers.Dropout(rate=0.4)
model.add(Dense(150, activation='selu', kernel_initializer="he_normal", use_bias = True))
layers.Dropout(rate=0.25)
model.add(Dense(150, activation='selu', kernel_initializer="he_normal"))
layers.Dropout(rate=0.25)
model.add(Dense(150, activation='selu', kernel_initializer="he_normal"))
layers.Dropout(rate=0.25)
model.add(Dense(150, activation='selu', kernel_initializer="he_normal"))
layers.Dropout(rate=0.25)
model.add(Dense(150, activation='selu', kernel_initializer="he_normal"))
layers.Dropout(rate=0.25)
model.add(Dense(10, activation='softmax', kernel_initializer="he_normal"))
# compile the keras model
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit the keras model on the dataset
#model.fit(X_train, y_train, epochs=152, batch_size=176)
# evaluate the keras model
#_, accuracy = model.evaluate(X_val, y_val)
return(model)
# Save for import to Kaggle
filepath = 'my_best_model1.hdf5'
model1_bayesCV, cv_instance = bayes_search(filepath, buildmodel1)