Assuming I fit the following neural network for a binary classification problem:
model = Sequential()
model.add(Dense(21, input_dim=19, init='uniform', activation='relu'))
model.add(Dense(80, init='uniform', activation='relu'))
model.add(Dense(80, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(x2, training_target, nb_epoch=10, batch_size=32, verbose=0,validation_split=0.1, shuffle=True,callbacks=[hist])
How would I boost the neural network using AdaBoost? Does keras have any commands for this?
Keras itself does not implement adaboost. However, Keras models are compatible with scikit-learn, so you probably can use
AdaBoostClassifier
from there: link. Use yourmodel
as thebase_estimator
after you compile it, andfit
theAdaBoostClassifier
instance instead ofmodel
.This way, however, you will not be able to use the arguments you pass to
fit
, such as number of epochs or batch_size, so the defaults will be used. If the defaults are not good enough, you might need to build your own class that implements the scikit-learn interface on top of your model and passes proper arguments tofit
.This can be done as follows: First create a model (for reproducibility make it as a function):
Then put it inside the sklearn wrapper:
Then and finally boost it:
Apparently, neural networks are not compatible with the sklearn Adaboost, see https://github.com/scikit-learn/scikit-learn/issues/1752