pythonmachine-learningscikit-learnneural-networkmlp

Tuning MLPRegressor hyper parameters


I have been trying to tune hyper parameters of a MLP model to solve a regression problem but I always get a convergence warning.

This is my code

def mlp_model(X, Y):

estimator=MLPRegressor()


param_grid = {'hidden_layer_sizes': [(50,50,50), (50,100,50), (100,1)],
          'activation': ['relu','tanh','logistic'],
          'alpha': [0.0001, 0.05],
          'learning_rate': ['constant','adaptive'],
          'solver': ['adam']}

gsc = GridSearchCV(
    estimator,
    param_grid,
    cv=5, scoring='neg_mean_squared_error', verbose=0, n_jobs=-1)

grid_result = gsc.fit(X, Y)


best_params = grid_result.best_params_

best_mlp = MLPRegressor(hidden_layer_sizes = best_params["hidden_layer_sizes"], 
                        activation =best_params["activation"],
                        solver=best_params["solver"],
                        max_iter= 5000, n_iter_no_change = 200
              )

scoring = {
           'abs_error': 'neg_mean_absolute_error',
           'squared_error': 'neg_mean_squared_error',
           'r2':'r2'}

scores = cross_validate(best_mlp, X, Y, cv=10, scoring=scoring, return_train_score=True, return_estimator = True)
return scores

The warnings I get are

ConvergenceWarning: Stochastic Optimizer: Maximum iterations (5000) reached and the optimization hasn't converged yet.% self.max_iter, ConvergenceWarning)

I have 87 features and 1384 rows in my dataset, all are numeric and have been scaled using MinMaxScaler. I would be grateful if you could guide me in tuning the hyperparameters.


Solution

  • Well, there are three options that you can try, one being obvious that you increase the max_iter from 5000 to a higher number since your model is not converging within 5000 epochs, secondly, try using batch_size, since you've got 1384 training examples, you can use a batch size of 16,32 or 64, this can help in converging your model within 5000 iterations, and lastly, you can always increasing learning_rate_init to a slightly higher value, cause it seems like learning rate is low as your model hasn't converged even after 5000 iterations. Hope this helps