How to plot training and test convergence of a multilayer perceptron

Viewed 269

I couldn't find anything helpful about plotting the process of converging test and training data of Sklearn.neural_network.MLPrgressor. I found that there is loss_curve_ attribute, but what about validation data?

I have built a simple model in which both inputs and outputs are randomly selected (say x = numpy.linspace(0, numpy.pi, 100), y = numpy.sin(x). I wrote this one to obtain variation of sklearn.metrics.mean_squared_error for a different number of hidden layers.

How can I overcome this problem?

from sklearn.preprocessing import RobustScaler

inputs /= 10
ERE /= 10
scaler = RobustScaler()
inputs = scaler.fit_transform(inputs)

X_train, X_test, y_train, y_test = train_test_split(inputs, ERE,
                                                    train_size=0.8,
                                                    random_state=123)
from sklearn.neural_network import MLPRegressor

hidden_layer_size = (10, )
activation = "tanh"
solver = "adam"
alpha = 1e-4
batch_size = 6
learning_rate = "adaptive"
learning_rate_init = 1e-4
power_t = "sgd"
max_iter = 1000
shuffle = True
random_state = 123
verbose = True
early_stopping = True
validation_fraction = 0.15
n_iter_no_change = 35

from sklearn.metrics import mean_squared_error as mse
import numpy as np

error_scores = np.zeros(shape = (11,))

for _iterator, hidden_layer_size in enumerate(range(1, 110, 10)):
  mlr = MLPRegressor(hidden_layer_sizes=hidden_layer_size,
                     activation=activation,
                     solver=solver,
                     batch_size=batch_size,
                     learning_rate=learning_rate,
                     learning_rate_init=learning_rate_init,
                     shuffle=shuffle,
                     random_state=random_state,
                     early_stopping=early_stopping,
                     validation_fraction=validation_fraction,
                     n_iter_no_change=n_iter_no_change,
                     alpha=alpha)

  mlr.fit(X_train, y_train)
  error_scores[_iterator] = mse(y_test, mlr.predict(X_test))
1 Answers

Class MLPrgressor (well, BaseMultilayerPerceptron really) has an undocumented validation_scores_ attribute which keeps track of scores on validation data. However, it is only populated if you pass True as parameter early_stopping when initialising the solver object.

Related