SKLearner Home | About | Contact | Examples

Configure MLPRegressor "alpha" Parameter

The alpha parameter in scikit-learn’s MLPRegressor controls the L2 regularization term strength.

MLPRegressor is a multi-layer perceptron neural network for regression tasks. The alpha parameter adds L2 regularization to the loss function, helping to prevent overfitting.

Higher alpha values increase regularization strength, leading to simpler models with potentially better generalization. Lower values allow more complex models that may fit the training data more closely.

The default value for alpha is 0.0001.

In practice, values are often tuned in a range from 1e-5 to 1, adjusting based on the specific dataset and problem.

from sklearn.neural_network import MLPRegressor
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import numpy as np

# Generate synthetic dataset
X, y = make_regression(n_samples=1000, n_features=20, noise=0.1, random_state=42)

# Split into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train with different alpha values
alpha_values = [1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1]
mse_scores = []

for alpha in alpha_values:
    mlp = MLPRegressor(hidden_layer_sizes=(100,), alpha=alpha, max_iter=1000, random_state=42)
    mlp.fit(X_train, y_train)
    y_pred = mlp.predict(X_test)
    mse = mean_squared_error(y_test, y_pred)
    mse_scores.append(mse)
    print(f"alpha={alpha}, MSE: {mse:.4f}")

Running the example gives an output like:

alpha=1e-05, MSE: 120.1624
alpha=0.0001, MSE: 120.1619
alpha=0.001, MSE: 120.1677
alpha=0.01, MSE: 119.8356
alpha=0.1, MSE: 119.9557
alpha=1, MSE: 120.1506

The key steps in this example are:

  1. Generate a synthetic regression dataset
  2. Split the data into train and test sets
  3. Train MLPRegressor models with different alpha values
  4. Evaluate the mean squared error of each model on the test set
  5. Plot the results to visualize the impact of alpha

Tips for setting alpha:

Issues to consider:



See Also