From SKLearn to Keras - What's the Difference?

I am trying to move from SKLearn to Keras to make certain improvements to my models. However, I cannot get the same performance as my SKLearn model :

mlp = MLPClassifier(
    solver='adam', activation='relu',
    beta_1=0.9, beta_2=0.999, learning_rate='constant',
    alpha=0, hidden_layer_sizes=(238,),
    max_iter=300
)
dev_score(mlp)

      

Gives ~ 0.65 points every time

Here is my corresponding Keras code:

def build_model(alpha):
    level_moreargs = {'kernel_regularizer':l2(alpha), 'kernel_initializer': 'glorot_uniform'}
    model = Sequential()
    model.add(Dense(units=238, input_dim=X.shape[1], **level_moreargs))
    model.add(Activation('relu'))
    model.add(Dense(units=class_names.shape[0], **level_moreargs)) # output
    model.add(Activation('softmax'))

    model.compile(loss=keras.losses.categorical_crossentropy, # like sklearn
                  optimizer=keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0),
                  metrics=['accuracy'])
    return model

k_dnn = KerasClassifier(build_fn=build_model, epochs=300, batch_size=200, validation_data=None, shuffle=True, alpha=0.5, verbose=0)

dev_score(k_dnn)

      

Studying the documentation (and digging into the SKLearn code) this should be exactly the same thing.

However, I get ~ 0.5 precision when I run this model, which is very bad.

And if I set alpha to 0, SKLearn's score barely changed (0.63), and Keras's score was random from 0.2 to 0.4.

What is the difference between these models? Why is Keras, although it should be better than SKLearn, outdone so far here? What's my mistake?

Thank,

+3


source to share





All Articles