Skip to content

Commit

Permalink
fix behavior for default objective and metric (#4660)
Browse files Browse the repository at this point in the history
  • Loading branch information
StrikerRUS committed Oct 13, 2021
1 parent 5c8a331 commit d130bb1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
4 changes: 2 additions & 2 deletions R-package/tests/testthat/test_lgb.Booster.R
Expand Up @@ -415,10 +415,10 @@ test_that("Creating a Booster from a Dataset with an existing predictor should w
expect_true(lgb.is.Booster(bst))
expect_equal(bst$current_iter(), nrounds)
expect_equal(bst$eval_train()[[1L]][["value"]], 0.1115352)
expect_true(lgb.is.Booster(bst_from_ds))
expect_equal(bst_from_ds$current_iter(), nrounds)
expect_equal(bst_from_ds$eval_train()[[1L]][["value"]], 5.65704892)
dumped_model <- jsonlite::fromJSON(bst$dump_model())
expect_identical(bst_from_ds$eval_train(), list())
expect_equal(bst_from_ds$current_iter(), nrounds)
})

test_that("Booster$eval() should work on a Dataset stored in a binary file", {
Expand Down
9 changes: 3 additions & 6 deletions src/io/config.cpp
Expand Up @@ -85,18 +85,15 @@ void GetObjectiveType(const std::unordered_map<std::string, std::string>& params
}
}

void GetMetricType(const std::unordered_map<std::string, std::string>& params, std::vector<std::string>* metric) {
void GetMetricType(const std::unordered_map<std::string, std::string>& params, const std::string& objective, std::vector<std::string>* metric) {
std::string value;
if (Config::GetString(params, "metric", &value)) {
std::transform(value.begin(), value.end(), value.begin(), Common::tolower);
ParseMetrics(value, metric);
}
// add names of objective function if not providing metric
if (metric->empty() && value.size() == 0) {
if (Config::GetString(params, "objective", &value)) {
std::transform(value.begin(), value.end(), value.begin(), Common::tolower);
ParseMetrics(value, metric);
}
ParseMetrics(objective, metric);
}
}

Expand Down Expand Up @@ -208,8 +205,8 @@ void Config::Set(const std::unordered_map<std::string, std::string>& params) {

GetTaskType(params, &task);
GetBoostingType(params, &boosting);
GetMetricType(params, &metric);
GetObjectiveType(params, &objective);
GetMetricType(params, objective, &metric);
GetDeviceType(params, &device_type);
if (device_type == std::string("cuda")) {
LGBM_config_::current_device = lgbm_device_cuda;
Expand Down
21 changes: 21 additions & 0 deletions tests/python_package_test/test_engine.py
Expand Up @@ -2029,6 +2029,27 @@ def test_multiple_feval_cv():
assert 'decreasing_metric-stdv' in cv_results


def test_default_objective_and_metric():
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
train_dataset = lgb.Dataset(data=X_train, label=y_train)
validation_dataset = lgb.Dataset(data=X_test, label=y_test, reference=train_dataset)
evals_result = {}
params = {'verbose': -1}
lgb.train(
params=params,
train_set=train_dataset,
valid_sets=validation_dataset,
num_boost_round=5,
callbacks=[lgb.record_evaluation(evals_result)]
)

assert 'valid_0' in evals_result
assert len(evals_result['valid_0']) == 1
assert 'l2' in evals_result['valid_0']
assert len(evals_result['valid_0']['l2']) == 5


@pytest.mark.skipif(psutil.virtual_memory().available / 1024 / 1024 / 1024 < 3, reason='not enough RAM')
def test_model_size():
X, y = load_boston(return_X_y=True)
Expand Down

0 comments on commit d130bb1

Please sign in to comment.