pythonmachine-learningneural-networkhyperopt

How to obtain the hidden layer sizes from HyperoptEstimator?


I am trying to use the hyperopt-sklearn library to get the best hyperparameters from a multilayer perceptron. The documentation is scarce and most of the answers seem hidden between layers and layers of code. Many of these lines, surpass my coding understanding.

I defined a space for the Hyperopt estimator, however, I have not come to find the correct function to get which was the selected configuration for layers and neurons. The ".best_model()" method outputs other relevant parameters, but not the ones I am looking for. For example:

>>> print(mlp_clf.best_model())
... {'learner': MLPClassifier(activation='tanh', alpha=0.005856877343088139,
              beta_1=0.846494371322821, beta_2=0.9945077173668336,
              epsilon=4.015091558071884e-06, learning_rate='adaptive',
              learning_rate_init=0.0942011641931547, max_fun=28556,
              max_iter=286, momentum=0.9551365399897995,
              power_t=0.13309940833978537, random_state=0,
              tol=0.008930401946599196,
              validation_fraction=0.16933029546511605), 'preprocs': (PCA(n_components=18, whiten=True),),      'ex_preprocs': ()}

My current code is:

clf = mlp_classifier(hp.choice('number_of_layers',[
                                                        {"1layer": scope.int(hp.qloguniform("1.1", np.log(1), np.log(1000), 1))},
                                                        {"2layer": [scope.int(hp.qloguniform("1.2", np.log(1), np.log(1000), 1)), 
                                                                    scope.int(hp.qloguniform("2.2", np.log(1), np.log(1000), 1))]},
                                                        {"3layer": [scope.int(hp.qloguniform("1.3", np.log(1), np.log(1000), 1)), 
                                                                    scope.int(hp.qloguniform("2.3", np.log(1), np.log(1000), 1)), 
                                                                    scope.int(hp.qloguniform("3.3", np.log(1), np.log(1000), 1))]}
                                                        ]))
mlp_clf = HyperoptEstimator(classifier= clf,
                            algo=tpe.suggest, 
                            max_evals=20, trial_timeout=200,seed=np.random.seed(42),
                            n_jobs=-1,verbose=True)
mlp_clf.fit(X_train,y_train)

I acknowledge that perhaps the estimator is just getting one option, given a bad implementation of the hp.choice, still, I'd even appreciate that remark.


Solution

  • You can find the hyperparameter space for MLPClassifier in hpsklearn here in the source code. The hyperparameter names match those of sklearn's MLPClassifier, so you can review their documentation for the formats those are expected to take. At the least, you need your space to name hidden_layer_sizes, not number_of_layers and 1layer etc.

    The hpsklearn object has an attribute trials which should contain the hyperopt Trials, which maybe will show the hyperparameters as you pass them. I guess hyperparameters that sklearn doesn't actually accept (like number_of_layers) just get discarded?