5
$\begingroup$

In the SciKit documentation of the MLP classifier, there is the early_stopping flag which allows to stop the learning if there is not any improvement in several iterations. However, it does not seem specified if the best weights found are restored or the final weights are those obtained at the last iteration. If not, is possible to restore the best weights found?

$\endgroup$

2 Answers 2

2
$\begingroup$

Yes.

Inspecting the source code on GitHub, we see that the internal function _update_no_improvement_count keeps track of the best coefficients in cases where the validation score doesn't improve

(line 580)

    def _update_no_improvement_count(self, early_stopping, X_val, y_val):
        if early_stopping:
            # compute validation score, use that for stopping
            self.validation_scores_.append(self.score(X_val, y_val))

            if self.verbose:
                print("Validation score: %f" % self.validation_scores_[-1])
            # update best parameters
            # use validation_scores_, not loss_curve_
            # let's hope no-one overloads .score with mse
            last_valid_score = self.validation_scores_[-1]

            if last_valid_score < (self.best_validation_score_ +
                                   self.tol):
                self._no_improvement_count += 1
            else:
                self._no_improvement_count = 0

            if last_valid_score > self.best_validation_score_:
                self.best_validation_score_ = last_valid_score
                self._best_coefs = [c.copy() for c in self.coefs_]
                self._best_intercepts = [i.copy()
                                         for i in self.intercepts_]

and that the internal function _fit_stochastic restores these coefficients if early_stopping==True

(line 567)


        if early_stopping:
            # restore best weights
            self.coefs_ = self._best_coefs
            self.intercepts_ = self._best_intercepts
$\endgroup$
2
$\begingroup$

Yes, the function will restore best weights, see the snippet below, taken from this link.

if early_stopping:
            # restore best weights
            self.coefs_ = self._best_coefs
            self.intercepts_ = self._best_intercepts

Please do make sure to set a reasonable validation_fraction, default is set to 0.1.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.