如何使用AdaBoost增强基于Keras的神经网络?

ish*_*ido 9 python neural-network adaboost keras boosting

假设我符合以下神经网络的二进制分类问题:

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])
Run Code Online (Sandbox Code Playgroud)

如何使用AdaBoost增强神经网络?keras有没有这方面的命令?

ow*_*ise 8

This can be done as follows: First create a model (for reproducibility make it as a function):

def simple_model():                                           
    # create model
    model = Sequential()
    model.add(Dense(25, input_dim=x_train.shape[1], kernel_initializer='normal', activation='relu'))
    model.add(Dropout(0.2, input_shape=(x_train.shape[1],)))
    model.add(Dense(10, kernel_initializer='normal', activation='relu'))
    model.add(Dense(1, kernel_initializer='normal'))
    # Compile model
    model.compile(loss='mean_squared_error', optimizer='adam')
    return model
Run Code Online (Sandbox Code Playgroud)

Then put it inside the sklearn wrapper:

ann_estimator = KerasRegressor(build_fn= simple_model, epochs=100, batch_size=10, verbose=0)
Run Code Online (Sandbox Code Playgroud)

Then and finally boost it:

boosted_ann = AdaBoostRegressor(base_estimator= ann_estimator)
boosted_ann.fit(rescaledX, y_train.values.ravel())# scale your training data 
boosted_ann.predict(rescaledX_Test)
Run Code Online (Sandbox Code Playgroud)

  • 你将如何重新调整数据? (2认同)

Ish*_*ael 3

Keras 本身并没有实现 adaboost。但是,Keras 模型与 scikit-learn 兼容,因此您可能可以AdaBoostClassifier从那里使用:链接。编译后使用您的modelas ,并使用实例代替。base_estimatorfitAdaBoostClassifiermodel

但是,这样您将无法使用传递给 的参数fit,例如纪元数或batch_size,因此将使用默认值。如果默认值不够好,您可能需要构建自己的类,在模型之上实现 scikit-learn 接口,并将适当的参数传递给fit.