Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using sklearn-genetic with neural networks #22

Closed
chemckenna opened this issue Jun 30, 2021 · 3 comments
Closed

Using sklearn-genetic with neural networks #22

chemckenna opened this issue Jun 30, 2021 · 3 comments

Comments

@chemckenna
Copy link

chemckenna commented Jun 30, 2021

Hi @manuel-calzolari

I am looking to use sklearn-genetic with a neural network, currently attempting to use with Keras NNs, although I am not necessarily tied to Keras.

I get the following error:

ValueError: Input 0 of layer sequential_2086 is incompatible with the layer: expected axis -1 of input shape to have value 180 but received input with shape (None, 118)

I understand why this is occurring - my NN input layer is expecting 180 features. Is there some way I can provide the number of features that sklearn-genetic is attempting to train with?

My KerasClassifier is defined as: estimator = KerasClassifier(lambda: create_nn_model(features=num_features), epochs=100) so I can dynamically supply this.

Can you suggest how I might use sklearn-genetic to select features for use in a NN?

Thanks for any help you can give.

@chemckenna
Copy link
Author

chemckenna commented Jun 30, 2021

My pipeline would be as follows:

        fs_step = GeneticSelectionCV(estimator,cv=5,
                                  verbose=1,
                                  scoring="r2", 
                                  max_features=180,
                                  n_population=50,
                                  crossover_proba=0.5,
                                  mutation_proba=0.2,
                                  n_generations=40,
                                  crossover_independent_proba=0.5,
                                  mutation_independent_proba=0.05,
                                  tournament_size=3,
                                  n_gen_no_change=10,
                                  caching=True,
                                  n_jobs=1)

        model = KerasClassifier(build_fn=lambda: create_nn_model(features=num_features, classes = 4, problem_type = 'multi_class', hl_act = 'relu', optimizer = 'Adam'), epochs=epoch, verbose=0, batch_size = 225)
        scale = StandardScaler()
        clf = Pipeline([('scale', scale),
                        ('fs_step', fs_step),
                            ('model', model)])

I can get your code running and have also been able to count the number of selected features with:

from collections import Counter
Counter(list(selector.support_))[1]

135

but I don't know how to feed that number into my pipeline and model.

@manuel-calzolari
Copy link
Owner

Sorry for the late reply.

Did you try to use the delayed-build pattern (no input shape specified) with keras? See https://www.tensorflow.org/api_docs/python/tf/keras/Sequential#examples_3

@chemckenna
Copy link
Author

Sorry for the late reply.

Did you try to use the delayed-build pattern (no input shape specified) with keras? See https://www.tensorflow.org/api_docs/python/tf/keras/Sequential#examples_3

Hi - I did not, I proceeded with scikit-learn's MLPClassifier and MLPRegressor. Lesson learned, thanks for pointing that out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants