tensorflow / neural-structured-learning

Training neural models with structured signals.
https://www.tensorflow.org/neural_structured_learning
Apache License 2.0
982 stars 189 forks source link

Can't implement GridSearchCV #40

Closed sagihaider closed 4 years ago

sagihaider commented 4 years ago

Dear, I am trying to implement GridSearchCV on EEG-based BCI dataset. I am unable to implement GridSearchCV may be because of your model needs input in dict and GridSearchCV in not accepting it. Any suggestion?

My Colab notebook is: https://github.com/sagihaider/EEG_Deep/blob/master/main_Single_Trial_Classification_EEGNet_NSL_2A_Grid_Search.ipynb

csferng commented 4 years ago

@sagihaider, thanks for your question.

As you mentioned, sklearn.model_selection.GridSearchCV expects a scikit-learn "estimator," which is a single-input single-output model. And the wrapper converting a Keras model to a scikit-learn estimator, keras.wrappers.scikit_learn.KerasClassifier, only supports keras.Sequential models [1]. Unfortunately, this doesn't support nsl.keras.AdversarialRegularization models out of the box, because an AdversarialRegularization model is not a Sequential model.

One workaround could be that, instead of keras.wrappers.scikit_learn.KerasClassifier, you use a custom wrapper which take cares of converting the array-style input to dictionary-style. Another workaround might be that you subclass nsl.keras.AdversarialRegularization and overrides model.fit (and probably predict and evaluate) to convert their x and y arguments to something like {'feature': x, 'label': y}.

Hope this helps.

[1] https://keras.io/scikit-learn-api/

csferng commented 4 years ago

Closing this issue for now. Feel free to reopen if you have further questions.