我无法使用KerasClassifier正确保存或加载最佳检查点模型。加载模型时,进行预测时不会得到相同的结果。这是我的模型的架构:
def create_baseline():
model = Sequential()
model.add(Dense(500, input_dim=train_X.shape[1], kernel_initializer='normal', activation='sigmoid'))
model.add(Dense(1, kernel_initializer='normal', activation='sigmoid'))
sgd = SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='mse', optimizer='rmsprop', metrics = ['accuracy'])
return model
# FIT AND TRAIN THE MODEL:
checkpoint = ModelCheckpoint('models/model.hdf5', monitor='val_acc', mode='max', verbose=0, save_best_only=True)
classifier = KerasClassifier(build_fn=create_baseline, epochs=100, batch_size=10, verbose=0)
classifier.fit(train_X, train_Y, validation_split=0.33, epochs=10, batch_size=10, callbacks=[checkpoint], verbose=0)
# Predict 1:
scores = classifier.predict(test_X)
# LOAD AND USE THE SAVED MODEL:
classifier2 = KerasClassifier(build_fn=create_baseline, epochs=10, batch_size=10, verbose=0)
classifier2.model = load_model('models/model.hdf5')
# Predict 2:
scores2 = classifier2.model.predict(test_X)
结果得分和scores2不同。
非常感谢您的帮助!