Note
Go to the end to download the full example code
Neural Network (MLP) Optimization#
Hyperparameter optimization for Multi-Layer Perceptron neural networks.
from sklearn.datasets import load_breast_cancer
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, f1_score
from sklearn.preprocessing import StandardScaler
import numpy as np
import plotly
from mloptimizer.interfaces import HyperparameterSpaceBuilder, GeneticSearch
from mloptimizer.application.reporting.plots import plotly_search_space, plotly_logbook
Load and prepare the dataset
Loading Breast Cancer dataset...
Dataset shape: (569, 30)
Split the data
Define the hyperparameter space
hyperparam_space = HyperparameterSpaceBuilder.get_default_space(
estimator_class=MLPClassifier
)
Configure and run the genetic optimization
genetic_params = {
'generations': 5,
'population_size': 8,
'n_elites': 2,
'seed': 42,
'use_mlflow': False,
'use_parallel': False
}
opt = GeneticSearch(
estimator_class=MLPClassifier,
hyperparam_space=hyperparam_space,
cv=3,
scoring='accuracy',
**genetic_params
)
print("Starting MLPClassifier optimization...")
opt.fit(X_train, y_train)
/home/docs/checkouts/readthedocs.org/user_builds/mloptimizer/checkouts/master/examples/plot_mlp_neural_network.py:52: UserWarning: Expected mutations per offspring is very low (0.48). With mutpb=0.8, indpb=0.2, and 3 hyperparameters, the population will converge prematurely. Recommended: mutpb >= 0.8, indpb >= 0.2 (gives ~0.5 mutations/offspring).
opt = GeneticSearch(
/home/docs/checkouts/readthedocs.org/user_builds/mloptimizer/checkouts/master/examples/plot_mlp_neural_network.py:52: UserWarning: Some hyperparameters have very small integer ranges (< 10 distinct values): 'activation' (3 values: 0 to 2). Small ranges limit search granularity. Consider increasing the range or scale for float types.
opt = GeneticSearch(
Starting MLPClassifier optimization...
Genetic execution: 0%| | 0/6 [00:00<?, ?it/s, best fitness=?]
Genetic execution: 17%|█▋ | 1/6 [00:00<00:01, 2.59it/s, best fitness=0.976]
Genetic execution: 17%|█▋ | 1/6 [00:00<00:04, 1.10it/s, best fitness=0.98]
Genetic execution: 33%|███▎ | 2/6 [00:02<00:05, 1.29s/it, best fitness=0.98]
Genetic execution: 50%|█████ | 3/6 [00:04<00:04, 1.43s/it, best fitness=0.98]
Genetic execution: 67%|██████▋ | 4/6 [00:05<00:02, 1.38s/it, best fitness=0.98]
Genetic execution: 83%|████████▎ | 5/6 [00:06<00:01, 1.31s/it, best fitness=0.98]
Genetic execution: 100%|██████████| 6/6 [00:07<00:00, 1.27s/it, best fitness=0.98]
Genetic execution: 100%|██████████| 6/6 [00:09<00:00, 1.51s/it, best fitness=0.98]
Evaluate the optimized model
best_clf = opt.best_estimator_
y_pred = best_clf.predict(X_test)
test_accuracy = accuracy_score(y_test, y_pred)
test_f1 = f1_score(y_test, y_pred, average='binary')
print(f"\nOptimization completed!")
print(f"Best parameters: {opt.best_params_}")
print(f"Test accuracy: {test_accuracy:.4f}")
print(f"Test F1: {test_f1:.4f}")
Optimization completed!
Best parameters: {'activation': 'relu', 'alpha': 0.076, 'batch_size': 'auto', 'beta_1': 0.9, 'beta_2': 0.999, 'early_stopping': False, 'epsilon': 1e-08, 'hidden_layer_sizes': [100], 'learning_rate': 'constant', 'learning_rate_init': 0.0036, 'max_fun': 15000, 'max_iter': 500, 'momentum': 0.9, 'n_iter_no_change': 10, 'nesterovs_momentum': True, 'power_t': 0.5, 'random_state': 42, 'shuffle': True, 'solver': 'adam', 'tol': 0.0001, 'validation_fraction': 0.1, 'verbose': False, 'warm_start': False}
Test accuracy: 0.9561
Test F1: 0.9645
Visualize the search space
population_df = opt.populations_
top_params = ['alpha', 'learning_rate_init', 'activation', 'fitness']
df_filtered = population_df[top_params]
g_search_space = plotly_search_space(df_filtered, top_params)
g_search_space.update_layout(
title="MLPClassifier Hyperparameter Search Space",
autosize=True,
width=None,
height=650
)
plotly.io.show(g_search_space, config={'responsive': True})
Visualize the optimization evolution
g_logbook = plotly_logbook(opt.logbook_, population_df)
g_logbook.update_layout(
title="MLPClassifier Optimization Evolution",
autosize=True,
width=None,
height=500
)
plotly.io.show(g_logbook, config={'responsive': True})
Analyze optimization performance
print("\n=== Optimization Performance ===")
print(f"Unique evaluations performed: {opt.n_trials_}")
print(f"Total individuals in population history: {len(opt.populations_)}")
print(f"Optimization time: {opt.optimization_time_:.4f} seconds")
print(f"Time per evaluation: {opt.optimization_time_ / opt.n_trials_:.4f} seconds")
=== Optimization Performance ===
Unique evaluations performed: 33
Total individuals in population history: 48
Optimization time: 9.0605 seconds
Time per evaluation: 0.2746 seconds
Total running time of the script: (0 minutes 10.158 seconds)