SKLearner Home | About | Contact | Examples

Configure MLPRegressor "verbose" Parameter

The verbose parameter in scikit-learn’s MLPRegressor controls the amount of output displayed during model training.

MLPRegressor is a multi-layer perceptron regressor that uses backpropagation for training. The verbose parameter determines how much information about the training process is printed to the console.

Setting verbose to a higher value increases the verbosity, providing more detailed information about each iteration during training. This can be useful for monitoring convergence and debugging.

The default value for verbose is False, which suppresses all output. Common values are True (or equivalently, any positive integer) to enable verbose output, and False (or 0) to disable it.

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

# Generate synthetic dataset
X, y = make_regression(n_samples=1000, n_features=10, 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 verbose settings
verbose_settings = [False, True]
for verbose in verbose_settings:
    mlp = MLPRegressor(hidden_layer_sizes=(100, 50), max_iter=10, random_state=42, verbose=verbose)
    mlp.fit(X_train, y_train)
    y_pred = mlp.predict(X_test)
    mse = mean_squared_error(y_test, y_pred)
    print(f"\nverbose={verbose}, Mean Squared Error: {mse:.3f}")

    if verbose:
        print("Example of verbose output:")
        print(mlp.loss_curve_[:5])  # Print first 5 loss values

Running the example gives an output like:

verbose=False, Mean Squared Error: 16569.402
Iteration 1, loss = 8790.46187736
Iteration 2, loss = 8770.27205585
Iteration 3, loss = 8748.64604967
Iteration 4, loss = 8724.11408224
Iteration 5, loss = 8697.11864841
Iteration 6, loss = 8663.33598139
Iteration 7, loss = 8626.46402704
Iteration 8, loss = 8582.23869623
Iteration 9, loss = 8530.51887205
Iteration 10, loss = 8470.03341382

verbose=True, Mean Squared Error: 16569.402
Example of verbose output:
[8790.461877359214, 8770.272055848403, 8748.64604966654, 8724.1140822391, 8697.118648405774]

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 verbose settings
  4. Compare the output and final performance for each setting

Some tips for using the verbose parameter:

Issues to consider:



See Also