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

[python] add flag of displaying train loss for lgb.cv() #2089

Merged
merged 7 commits into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions .idea/LightGBM.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 17 additions & 7 deletions python-package/lightgbm/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ def handler_function(*args, **kwargs):
return handler_function


def _make_n_folds(full_data, folds, nfold, params, seed, fpreproc=None, stratified=True, shuffle=True):
def _make_n_folds(full_data, folds, nfold, params, seed, fpreproc=None, stratified=True,
shuffle=True, disp_train_loss=False):
"""Make a n-fold list of Booster from random indices."""
full_data = full_data.construct()
num_data = full_data.num_data()
Expand Down Expand Up @@ -315,19 +316,25 @@ def _make_n_folds(full_data, folds, nfold, params, seed, fpreproc=None, stratifi
else:
tparam = params
cvbooster = Booster(tparam, train_set)
if disp_train_loss:
cvbooster.add_valid(train_set, 'train')
cvbooster.add_valid(valid_set, 'valid')
ret.append(cvbooster)
return ret


def _agg_cv_result(raw_results):
def _agg_cv_result(raw_results, disp_train_loss=False):
"""Aggregate cross-validation results."""
cvmap = collections.defaultdict(list)
metric_type = {}
for one_result in raw_results:
for one_line in one_result:
metric_type[one_line[1]] = one_line[3]
cvmap[one_line[1]].append(one_line[2])
if disp_train_loss:
key = "{} {}".format(one_line[0], one_line[1])
else:
key = one_line[1]
metric_type[key] = one_line[3]
cvmap[key].append(one_line[2])
return [('cv_agg', k, np.mean(v), metric_type[k], np.std(v)) for k, v in cvmap.items()]


Expand All @@ -337,7 +344,7 @@ def cv(params, train_set, num_boost_round=100,
feature_name='auto', categorical_feature='auto',
early_stopping_rounds=None, fpreproc=None,
verbose_eval=None, show_stdv=True, seed=0,
callbacks=None):
callbacks=None, disp_train_loss=False):
"""Perform the cross-validation with given paramaters.

Parameters
Expand Down Expand Up @@ -408,6 +415,8 @@ def cv(params, train_set, num_boost_round=100,
callbacks : list of callables or None, optional (default=None)
List of callback functions that are applied at each iteration.
See Callbacks in Python API for more information.
disp_train_loss : bool, (default=False)
StrikerRUS marked this conversation as resolved.
Show resolved Hide resolved
If True, training loss will be displayed during model training.

Returns
-------
Expand Down Expand Up @@ -455,7 +464,8 @@ def cv(params, train_set, num_boost_round=100,
results = collections.defaultdict(list)
cvfolds = _make_n_folds(train_set, folds=folds, nfold=nfold,
params=params, seed=seed, fpreproc=fpreproc,
stratified=stratified, shuffle=shuffle)
stratified=stratified, shuffle=shuffle,
disp_train_loss=disp_train_loss)

# setup callbacks
if callbacks is None:
Expand Down Expand Up @@ -485,7 +495,7 @@ def cv(params, train_set, num_boost_round=100,
end_iteration=num_boost_round,
evaluation_result_list=None))
cvfolds.update(fobj=fobj)
res = _agg_cv_result(cvfolds.eval_valid(feval))
res = _agg_cv_result(cvfolds.eval_valid(feval), disp_train_loss)
for _, key, mean, _, std in res:
results[key + '-mean'].append(mean)
results[key + '-stdv'].append(std)
Expand Down
11 changes: 11 additions & 0 deletions tests/python_package_test/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,17 @@ def test_cv(self):
callbacks=[lgb.reset_parameter(learning_rate=lambda i: 0.1 - 0.001 * i)])
self.assertIn('l1-mean', cv_res)
self.assertEqual(len(cv_res['l1-mean']), 10)

# enable display training loss
cv_res = lgb.cv(params_with_metric, lgb_train, num_boost_round=10,
nfold=3, stratified=False, shuffle=False,
metrics='l1', verbose_eval=False, disp_train_loss=True)
self.assertIn('train l1-mean', cv_res)
self.assertIn('valid l1-mean', cv_res)
self.assertNotIn('train l2-mean', cv_res)
self.assertNotIn('valid l2-mean', cv_res)
self.assertEqual(len(cv_res['valid l1-mean']), 10)
StrikerRUS marked this conversation as resolved.
Show resolved Hide resolved

# self defined folds
tss = TimeSeriesSplit(3)
folds = tss.split(X_train)
Expand Down