Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture LGBM model's .fit messages and redirect them to python logger #1493

Closed
MTDzi opened this issue Jul 5, 2018 · 13 comments
Closed

Capture LGBM model's .fit messages and redirect them to python logger #1493

MTDzi opened this issue Jul 5, 2018 · 13 comments

Comments

@MTDzi
Copy link

MTDzi commented Jul 5, 2018

I'm using the logging Python module in my project and would like to be able to capture the messages produced by the lightgbm.sklearn.LGBMClassifier.fit method.

So far, I've re-directed the STDOUT to the logging file:

logging.basicConfig(filename=log_filename, level=logging.INFO)
channel = logging.StreamHandler(sys.stdout)
sys.stdout = open(log_filename, 'a', 1)
channel.setLevel(logging.INFO)
root = logging.getLogger()
root.setLevel(logging.INFO)
root.addHandler(channel)

but in doing so, I'm also re-directing output from the debugger -- not good.

How can I do it better?

@StrikerRUS
Copy link
Collaborator

At present practically all messages (except a few warnings) come directly from the C++ code to the stdout/stderr. So, there is no way to manage them by Python's setLevel() functionality. As a workaround you can write a function that will filter messages you don't want to write into a log and re-redirect them back to the console. The code of C++ logger may help you: https://github.com/Microsoft/LightGBM/blob/master/include/LightGBM/utils/log.h

@StrikerRUS
Copy link
Collaborator

@guolinke @chivee
Maybe we can have something similar?
dmlc/xgboost#3438

@MTDzi
Copy link
Author

MTDzi commented Jul 6, 2018

Hey,

Thanks for the reply!

As a work-around (because I was interested in the messages related to early stopping), I just took the LGBMClassifier.evals_result_ and passed that as a message to the logger.

But, for anyone else interested in the matter, I highly discourage from using my previous work-around, the one with the

...
sys.stdout = open(log_filename, 'a', 1)
...

hack.

@StrikerRUS
Copy link
Collaborator

@MTDzi Thank you for sharing your experience!

@StrikerRUS
Copy link
Collaborator

@guolinke @chivee
Is it possible to implement #1493 (comment) in LightGBM?
Such thing will allow, for example, to log right into Jupyter Notebook cell output.

@StrikerRUS
Copy link
Collaborator

ping @guolinke @chivee

@guolinke
Copy link
Collaborator

@StrikerRUS
Only need to redirect the output in training, without all other logs?

@StrikerRUS
Copy link
Collaborator

StrikerRUS commented Apr 11, 2019

@guolinke I mean, create a callback at cpp side for all logs, which can be used at Python/R/whatever_language side:
#1493 (comment)

@StrikerRUS
Copy link
Collaborator

@guolinke Is it possible?

@guolinke
Copy link
Collaborator

@StrikerRUS I think it is easy to implement. But I don't have time recently...

@StrikerRUS
Copy link
Collaborator

@guolinke

I think it is easy to implement.

It's great!

But I don't have time recently...

… and it's a pity.

BTW, will this also help to fix #1440?

@guolinke
Copy link
Collaborator

@StrikerRUS It seems different. The problem in R is to redirect the log to R's console, and need to interact with R's library.
I think this issue is only redirecting to file.

@StrikerRUS
Copy link
Collaborator

Closed in favor of being in #2302. We decided to keep all feature requests in one place.

Welcome to contribute this feature! Please re-open this issue (or post a comment if you are not a topic starter) if you are actively working on implementing this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants