Get error "ValueError: Input contains NaN" when MLP regression model is exploding numerically and when early_stopping=True

### Describe the bug

Hello,

I was doing dummy tests with a very basic MLP regressor, and I got an error I was not expecting: "ValueError Input contains NaN".

Full traceback:

```
File ~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/base.py:1473, in _fit_context.<locals>.decorator.<locals>.wrapper(estimator, *args, **kwargs)
   [1466](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/base.py:1466)     estimator._validate_params()
   [1468](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/base.py:1468) with config_context(
   [1469](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/base.py:1469)     skip_parameter_validation=(
   [1470](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/base.py:1470)         prefer_skip_nested_validation or global_skip_validation
   [1471](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/base.py:1471)     )
   [1472](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/base.py:1472) ):
-> [1473](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/base.py:1473)     return fit_method(estimator, *args, **kwargs)

File ~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:752, in BaseMultilayerPerceptron.fit(self, X, y)
    [734](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:734) @_fit_context(prefer_skip_nested_validation=True)
    [735](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:735) def fit(self, X, y):
    [736](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:736)     """Fit the model to data matrix X and target(s) y.
    [737](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:737) 
    [738](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:738)     Parameters
   (...)
    [750](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:750)         Returns a trained MLP model.
    [751](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:751)     """
--> [752](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:752)     return self._fit(X, y, incremental=False)

File ~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:475, in BaseMultilayerPerceptron._fit(self, X, y, incremental)
    [473](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:473) # Run the Stochastic optimization solver
    [474](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:474) if self.solver in _STOCHASTIC_SOLVERS:
--> [475](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:475)     self._fit_stochastic(
    [476](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:476)         X,
    [477](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:477)         y,
    [478](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:478)         activations,
    [479](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:479)         deltas,
    [480](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:480)         coef_grads,
    [481](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:481)         intercept_grads,
    [482](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:482)         layer_units,
    [483](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:483)         incremental,
    [484](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:484)     )
    [486](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:486) # Run the LBFGS solver
    [487](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:487) elif self.solver == "lbfgs":

File ~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:660, in BaseMultilayerPerceptron._fit_stochastic(self, X, y, activations, deltas, coef_grads, intercept_grads, layer_units, incremental)
    [657](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:657) # update no_improvement_count based on training loss or
    [658](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:658) # validation score according to early_stopping
    [659](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:659) print("validation:", X_val, y_val)
--> [660](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:660) self._update_no_improvement_count(early_stopping, X_val, y_val)
    [662](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:662) # for learning rate that needs to be updated at iteration end
    [663](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:663) self._optimizer.iteration_ends(self.t_)

File ~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:708, in BaseMultilayerPerceptron._update_no_improvement_count(self, early_stopping, X_val, y_val)
    [705](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:705) def _update_no_improvement_count(self, early_stopping, X_val, y_val):
    [706](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:706)     if early_stopping:
    [707](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:707)         # compute validation score, use that for stopping
--> [708](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:708)         self.validation_scores_.append(self._score(X_val, y_val))
    [710](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:710)         if self.verbose:
    [711](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:711)             print("Validation score: %f" % self.validation_scores_[-1])

File ~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:1619, in MLPRegressor._score(self, X, y)
   [1617](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:1617) # Input validation would remove feature names, so we disable it
   [1618](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:1618) y_pred = self._predict(X, check_input=False)
-> [1619](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:1619) return r2_score(y, y_pred)

File ~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/_param_validation.py:186, in validate_params.<locals>.decorator.<locals>.wrapper(*args, **kwargs)
    [184](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/_param_validation.py:184) global_skip_validation = get_config()["skip_parameter_validation"]
    [185](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/_param_validation.py:185) if global_skip_validation:
--> [186](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/_param_validation.py:186)     return func(*args, **kwargs)
    [188](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/_param_validation.py:188) func_sig = signature(func)
    [190](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/_param_validation.py:190) # Map *args/**kwargs to the function signature

File ~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/metrics/_regression.py:1204, in r2_score(y_true, y_pred, sample_weight, multioutput, force_finite)
   [1198](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/metrics/_regression.py:1198) xp, _, device_ = get_namespace_and_device(
   [1199](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/metrics/_regression.py:1199)     y_true, y_pred, sample_weight, multioutput
   [1200](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/metrics/_regression.py:1200) )
   [1202](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/metrics/_regression.py:1202) dtype = _find_matching_floating_dtype(y_true, y_pred, sample_weight, xp=xp)
-> [1204](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/metrics/_regression.py:1204) _, y_true, y_pred, multioutput = _check_reg_targets(
   [1205](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/metrics/_regression.py:1205)     y_true, y_pred, multioutput, dtype=dtype, xp=xp
   [1206](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/metrics/_regression.py:1206) )
   [1207](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/metrics/_regression.py:1207) check_consistent_length(y_true, y_pred, sample_weight)
   [1209](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/metrics/_regression.py:1209) if _num_samples(y_pred) < 2:

File ~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/metrics/_regression.py:113, in _check_reg_targets(y_true, y_pred, multioutput, dtype, xp)
    [111](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/metrics/_regression.py:111) check_consistent_length(y_true, y_pred)
    [112](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/metrics/_regression.py:112) y_true = check_array(y_true, ensure_2d=False, dtype=dtype)
--> [113](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/metrics/_regression.py:113) y_pred = check_array(y_pred, ensure_2d=False, dtype=dtype)
    [115](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/metrics/_regression.py:115) if y_true.ndim == 1:
    [116](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/metrics/_regression.py:116)     y_true = xp.reshape(y_true, (-1, 1))

File ~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:1059, in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator, input_name)
   [1053](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:1053)     raise ValueError(
   [1054](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:1054)         "Found array with dim %d. %s expected <= 2."
   [1055](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:1055)         % (array.ndim, estimator_name)
   [1056](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:1056)     )
   [1058](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:1058) if force_all_finite:
-> [1059](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:1059)     _assert_all_finite(
   [1060](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:1060)         array,
   [1061](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:1061)         input_name=input_name,
   [1062](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:1062)         estimator_name=estimator_name,
   [1063](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:1063)         allow_nan=force_all_finite == "allow-nan",
   [1064](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:1064)     )
   [1066](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:1066) if copy:
   [1067](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:1067)     if _is_numpy_namespace(xp):
   [1068](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:1068)         # only make a copy if `array` and `array_orig` may share memory`

File ~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:126, in _assert_all_finite(X, allow_nan, msg_dtype, estimator_name, input_name)
    [123](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:123) if first_pass_isfinite:
    [124](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:124)     return
--> [126](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:126) _assert_all_finite_element_wise(
    [127](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:127)     X,
    [128](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:128)     xp=xp,
    [129](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:129)     allow_nan=allow_nan,
    [130](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:130)     msg_dtype=msg_dtype,
    [131](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:131)     estimator_name=estimator_name,
    [132](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:132)     input_name=input_name,
    [133](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:133) )

File ~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:175, in _assert_all_finite_element_wise(X, xp, allow_nan, msg_dtype, estimator_name, input_name)
    [158](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:158) if estimator_name and input_name == "X" and has_nan_error:
    [159](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:159)     # Improve the error message on how to handle missing values in
    [160](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:160)     # scikit-learn.
    [161](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:161)     msg_err += (
    [162](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:162)         f"\n{estimator_name} does not accept missing values"
    [163](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:163)         " encoded as NaN natively. For supervised learning, you might want"
   (...)
    [173](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:173)         "#estimators-that-handle-nan-values"
    [174](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:174)     )
--> [175](https://file+.vscode-resource.vscode-cdn.net/Users/datategy/Documents/o2_ml_2/~/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py:175) raise ValueError(msg_err)

ValueError: Input contains NaN.
```

Basically, setting `early_stopping=True` will enable a model prediction and a r2_score computing on that prediction. However, when the network overflows and outputs NaN, it will crash when computing the r2_score.

Conversely, when `early_stopping=False`, it will not call that score and will gracefully raise the expected error: "Solver produced non-finite parameter weights. The input data may contain large values and need to be preprocessed."

### Steps/Code to Reproduce

```py
# code changed on 2024-07-19
from sklearn.neural_network import MLPRegressor
from sklearn.datasets import make_regression

x, y = make_regression(bias=100, random_state=0)

model = MLPRegressor(
    hidden_layer_sizes=100,
    activation="identity",
    solver="sgd",
    alpha=0.00009999999747378752,
    learning_rate="constant",
    learning_rate_init=1,
    shuffle=True,
    max_iter=200,
    early_stopping=True,
    n_iter_no_change=10,
    verbose=True
)
model.fit(x, y)
```

### Expected Results

ValueError: Input contains NaN.

### Actual Results

ValueError: Solver produced non-finite parameter weights. The input data may contain large values and need to be preprocessed.


### Versions

```shell
System:
    python: 3.10.11 (v3.10.11:7d4cc5aa85, Apr  4 2023, 19:05:19) [Clang 13.0.0 (clang-1300.0.29.30)]
executable: /Users/datategy/Documents/o2_ml_2/.venv/bin/python
   machine: macOS-14.4.1-arm64-arm-64bit

Python dependencies:
      sklearn: 1.5.0
          pip: 24.1.2
   setuptools: 65.5.0
        numpy: 1.24.4
        scipy: 1.8.1
       Cython: 3.0.10
       pandas: 1.5.3
   matplotlib: 3.8.4
       joblib: 1.2.0
threadpoolctl: 3.5.0

Built with OpenMP: True

threadpoolctl info:
       user_api: blas
   internal_api: openblas
    num_threads: 8
         prefix: libopenblas
       filepath: /Users/datategy/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/numpy/.dylibs/libopenblas64_.0.dylib
        version: 0.3.21
threading_layer: pthreads
   architecture: armv8

       user_api: blas
   internal_api: openblas
    num_threads: 8
         prefix: libopenblas
       filepath: /Users/datategy/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/scipy/.dylibs/libopenblas.0.dylib
        version: 0.3.17
threading_layer: pthreads
   architecture: armv8

       user_api: openmp
   internal_api: openmp
    num_threads: 8
         prefix: libomp
       filepath: /Users/datategy/Documents/o2_ml_2/.venv/lib/python3.10/site-packages/sklearn/.dylibs/libomp.dylib
        version: None
```

COMMENT:
In my initial experiment, I had set the learning rate to such a low value. But I was asked to create a reproductible example without external data. The bug is not that the model does not train, it is that the wrong error is displayed.

Labels: Bug, help wanted