Repository navigation
Description
Hi, I'm unable to turn off convergence warnings from GraphicalLassoCV.
I've tried most of the solutions from, and none of them worked (see below for actual implementations):
https://stackoverflow.com/questions/879173/how-to-ignore-deprecation-warnings-in-python
https://stackoverflow.com/questions/32612180/eliminating-warnings-from-scikit-learn/33812427#33812427
https://stackoverflow.com/questions/53968004/how-to-silence-all-sklearn-warning
https://stackoverflow.com/questions/14463277/how-to-disable-python-warnings
Contrary to what the designers of the sklearn's exceptions must have thought when it was implemented, some of us actually use stdout to log important information of the host program for diagnostics purposes. Flooding it with garbage that cannot be turned off, as is in the case with cross-validation, is not ok.
To briefly speak to the severity of the issue, the above sklearn-specific questions relating to suppressing warnings have been viewed ~500K times with combined ~400 upvotes, and dates back 7 years.
I've tried the following (n_jobs parameter does not appear to affect the result):
from sklearn.covariance import GraphicalLassoCV
import warnings
warnings.filterwarnings("ignore", category=ConvergenceWarning)
model = GraphicalLassoCV(n_jobs=4)
model = model.fit(data)from sklearn.covariance import GraphicalLassoCV
import warnings
warnings.filterwarnings(action='ignore')
model = GraphicalLassoCV(n_jobs=4)
model = model.fit(data)import warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore", ConvergenceWarning)
model = GraphicalLassoCV(n_jobs=4)
model = model.fit(data)from sklearn.covariance import GraphicalLassoCV
def warn(*args, **kwargs):
pass
import warnings
warnings.warn = warn
model = GraphicalLassoCV(n_jobs=4)
model = model.fit(data)import contextlib
import os, sys
@contextlib.contextmanager
def suppress_stdout():
with open(os.devnull, 'w') as fnull:
old_stdout = sys.stdout
sys.stdout = fnull
try:
yield
finally:
sys.stdout = old_stdout
with suppress_stdout():
model = GraphicalLassoCV(n_jobs=4)
model = model.fit(data)import logging
logging.captureWarnings(True)
logging.getLogger("py.warnings").setLevel(logging.ERROR)
model = GraphicalLassoCV(n_jobs=4)
model = model.fit(data)
Activity
lesteve commentedon Jun 19, 2024
If you give a fully stand-alone example, in particular some
datathat triggers a warning, this may give you a lot more chance that someone tries to look into this 🙏.I would expect that
warnings.simplefilterwould work (I am guessing you did the right import to haveConvergenceWarningdefined although you have not included it), but maybe there are subtleties involved with subprocesses, not sure ...lesteve commentedon Jun 19, 2024
Hmmm looking at SO myself, it seems like indeed this is subprocesses with joblib, see this answer.
Basically
warningsfilters only applied to the main process and not the subprocesses. One proposed solution in the SO answer is to:warnings.simplefilterfor the warnings in the main processPYTHONWARNINGSfor the warnings in the subprocesses launched by joblib@u3Izx9ql7vW4 can you confirm this fixes the issue for you?
In an ideal world, you would not need this work-around. In our imperfect world, I don't really know if there is some magic we can do in joblib and/or loky to have the subprocesses inherit the warnings filters from the main process ...
In any cases this should be an issue in joblib I think: I opened joblib/joblib#1591.
An alternative may actually be to use our already existing joblib wrappers like delayed to apply the warning filters to subprocesses, not sure how doable this is and that would be scikit-learn specific.
ogrisel commentedon Jun 19, 2024
In the future we will all use free-threading CPython 3.13 or later and use a threading backend instead of subprocesses :)
lesteve commentedon Jun 19, 2024
Yeah that's the ideal world future 🤞 but still some road to get there 😉
jeremiedbb commentedon Jun 20, 2024
We could add an option to disable convergence warnings (or warning of a given category) in the global config and define our own
warnfunction that would check the global config to trigger or not the warning. It should work in all settings since the global config is now propagated to sub-processes.lesteve commentedon Jun 20, 2024
Good point indeed! We could indeed, whether we should is another question, since free-threading is coming 🤞 and there is some kind of not so great but at least existing work-around 😰 ...
Edit: thinking about it more, the config-based solution, may be a reasonable stop-gap solution, I guess someone maybe should have a closer look? Maybe an issue for a sprint? Personally I am not planning to look at it in the near-future. I am going to set the label "Help Wanted"
erickdcohen commentedon Oct 13, 2024
Ran into this issue as well. Dropping a reproducible example:
output repeated:
[/Users/~/.venv/lib/python3.12/site-packages/sklearn/linear_model/_sag.py:349](https://file+.vscode-resource.vscode-cdn.net/Users/~/.venv/lib/python3.12/site-packages/sklearn/linear_model/_sag.py:349):ConvergenceWarning: The max_iter was reached which means the coef_ did not converge warnings.warn(Machine info
Adds convergence warning suppression via global config issue scikit-l…
ogrisel commentedon Nov 18, 2024
I think this might be a better option, no? This would fix the problem for any kind of warnings and not just convergence warnings.
Alternatively, we could make joblib itself do this automatically, but I am a bit worried about the extra complexity and potential overhead.
erickdcohen commentedon Nov 21, 2024
I agree that this seems like a joblib issue. I was thinking of just adding the stop-gap as suggested under Steve's comment that "free-threading" is in the works:
I am unclear about "free threading" and how this would solve the issue, but agree that as a stop-gap this works. Can someone explain what is in the works for "free threading"?
lesteve commentedon Nov 29, 2024
Here is a slightly less hacky work-around than #29294 (comment) based on the fact that
loky(the default joblib backend) is trying to reuse the same processes. It may also work forbackend='multiprocessing'on POSIX, since the default start method isfork(until Python <= 3.13) even though workers are not reused (not sure and not tested).The reproducer from @erickdcohen (thanks by the way!) does not give any warnings for me with this:
Longer-term I think that tackling this in joblib adding
initializersfunction to all the backend joblib/joblib#381 seems one way to go although this is definitely not going to happen overnight.@erickdcohen about free-threading, this is an experimental interpreter in CPython 3.13 that allows you to have efficient multi-threading by removing the GIL (Global Interpreter Lock), see https://py-free-threading.github.io/ for more details. If you are using multi-threading
warnings.filtersare shared in all workers in contrary to multi-processing where you have separate Python processes with separatewarnings.filters.erickdcohen commentedon Dec 2, 2024
I see! Thank you @lesteve for the explanation and the work around!