我想将训练有素的模型保存到磁盘中。据我所知,我可以使用以下代码保存模型:
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")
但是我使用keras classifier
和kfold
并在后台拟合模型,我的代码是:
def baseline_model(optimizer='adam', init='random_uniform'):
# create model
model = Sequential()
model.add(Dense(40, input_dim=18260, activation="relu", kernel_initializer=init))
model.add(Dense(10, activation="sigmoid", kernel_initializer=init))
model.add(Dense(4, activation="softmax", kernel_initializer=init))
model.summary()
# Compile model
model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
return model
estimator = KerasClassifier(build_fn=baseline_model, validation_split=0.33, nb_epoch=100, batch_size=10, verbose=1)
kfold = KFold(n_splits=10, shuffle=True, random_state=seed)
results = cross_val_score(estimator, X, Y, cv=kfold)
print("Accuracy: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))
在这种情况下,如何保存经过训练的模型? 另外,如果我要进行预测,鉴于我没有拟合模型,该怎么做?