Reputation: 4260
While fitting a model using sklearn.neural_network.MLPClassifier
I sometimes receive a warning printed in the console:
ConvergenceWarning: Stochastic Optimizer: Maximum iterations (300) reached and the optimization hasn't converged yet.
Is there a way to detect the warning during runtime so I can act on it?
Upvotes: 8
Views: 3012
Reputation: 16079
You can catch the warning in realtime with warnings.catch_warnings
import warnings
with warnings.catch_warnings():
warnings.filterwarnings('error')
try:
model.fit(X, y)
except Warning:
# do something in response
This structure will catch any Warning in line and allow you to respond to it however you see fit. In this case that may be modifying some hyperparameter to make it easier for the model to converge.
You can also ignore warnings with warnings.filterwarnings
and can specify the type of warning to ignore.
To ignore ConvergenceWarning
:
from sklearn.exceptions import ConvergenceWarning
...
warnings.filterwarnings('ignore', category=ConvergenceWarning)
...
Upvotes: 9
Reputation: 1822
Let's say that you want to train your scikit-learn model and you want to be able to store the warnings (if any). Let's say that you fit the model as follows:
clf.fit(X_train, y)
If you want to catch the warning. Then you can run the model:
with warnings.catch_warnings(record=True) as caught_warnings:
clf.fit(X_train, y)
Finally, you can get the warnings by iterating the caught_warnings
as follows:
for warn in caught_warnings:
print(warn)
Upvotes: 1
Reputation: 146
Check the n_iter_
attribute after fitting. If it is less than the maximum number of iterations you configured (max_iter
), then it converged.
Upvotes: 2