rodrigo-arenas / Sklearn-genetic-opt

ML hyperparameters tuning and features selection, using evolutionary algorithms.
https://sklearn-genetic-opt.readthedocs.io
MIT License
289 stars 73 forks source link

Tuples as categorical parameters - "ValueError: a must be 1-dimensional" #102

Closed jordan-bird closed 1 year ago

jordan-bird commented 1 year ago

System information OS Platform and Distribution: Windows 10 Sklearn-genetic-opt version: 0.9.0 Scikit-learn version: 1.0.2 Python version: 3.7.9

Describe the bug Passing tuples as hyperparameters causes the simulation to crash at the start with "ValueError: a must be 1-dimensional"

To Reproduce Messy code but this will throw the error:

clf = MLPClassifier()
hidden_layers_1 = list([l1, l2, l3] for l1 in range(1, 129) for l2 in range(0, 129) for l3 in range(0, 129))
hidden_layers = []
for hl in hidden_layers_1:
    hidden_layers.append(tuple(hl))
param_grid = {'hidden_layer_sizes': Categorical(hidden_layers)}

Expected behavior The algorithm should select one of the tuples as the hidden layers and neuron counts e.g. (50,100,10) would create three hidden layers in the network of 50, 100, and 10 neurons, respectively

is there any way around this error? Thanks in advance!

jordan-bird commented 1 year ago

I think I managed to fix this with Pull Request #103

Switching from NumPy to Python's built-in random.choice enables selection of 2D categorical parameters. I now have this algorithm searching for Neural Net Hyperparameters.

rodrigo-arenas commented 1 year ago

Hi @jordan-bird thanks for the report! I just left a couple of comments on the pull request, let me know if you have any questions

santialferez commented 1 year ago

Is it possible with the hidden layer parameter of MLP use a tuple of integers something like (Integer(10,100), Integer(20,30)) to define the options for two hidden layers?

rodrigo-arenas commented 1 year ago

Hi @santialferez, currently there is already a PR working on this issue, I'll take into account the tuple suggestion while evaluating it

Thanks

rodrigo-arenas commented 1 year ago

This has been merged to master in #103 , it will be available in the next release