Skip to content

Custom logger on CatBoostClassifier doesn't work #2277

@francescoalongi

Description

@francescoalongi

Problem:
I don't fully understand how to use the log_cout parameter of the CatBoostClassifier.fit method.
The doc says that this parameter should take as input the "Output stream or callback for logging".
I tried to pass a custom logger in order to log the training details on a specific handler, but it seems that either I am doing it wrong, or the log_cout doesn't work as it should. Here is a minimal script to reproduce the error:

import catboost
import logging


x = [[0,45,45],[4,2,6],[78,1,35]]
y = [0,1,0]

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)

logger.addHandler(handler)

logging.info("Starting training...")

model = catboost.CatBoostClassifier()
model.fit(x, y, log_cout=logger.info)

This code outputs:

Starting training...
Traceback (most recent call last):
  File "_catboost.pyx", line 5950, in _catboost._WriteLog
  File "_catboost.pyx", line 5950, in _catboost._WriteLog
  File "_catboost.pyx", line 5950, in _catboost._WriteLog
  [Previous line repeated 997 more times]
AttributeError: '_GeneratorContextManager' object has no attribute 'write'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/francesco*/Documents/sample_script.py", line 20, in <module>
    model.fit(x, y, log_cout=logging.info)
  File "/home/francesco*/miniconda3/lib/python3.9/site-packages/catboost/core.py", line 5128, in fit
    self._fit(X, y, cat_features, text_features, embedding_features, None, sample_weight, None, None, None, None, baseline, use_best_model,
  File "/home/francesco*/miniconda3/lib/python3.9/site-packages/catboost/core.py", line 2355, in _fit
    self._train(
  File "/home/francesco*/miniconda3/lib/python3.9/site-packages/catboost/core.py", line 1759, in _train
    self._object._train(train_pool, test_pool, params, allow_clear_pool, init_model._object if init_model else None)
SystemError: <method '_train' of '_catboost._CatBoost' objects> returned a result with an error set

The tests that can be found in the catboost/python-package/ut/medium/test.py file only mention the case where the log_cout implements the write() method, what about a logger?

catboost version: 1.1.1
Operating System: Ubuntu 22.04.1 LTS
CPU: 11th Gen Intel® Core™ i7-1165G7 @ 2.80GHz × 8
GPU: x

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions