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

fix calculation of weighted gamma loss (fixes #4174) #4283

Merged
merged 8 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions R-package/tests/testthat/test_weighted_loss.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
context("Case weights are respected")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test looks good to me, thank you! This might be the first unit test we've added that covers weighted training at all in the R package, so really appreciate it.

It's ok with me for this to live in a new test file as you've set it up. test_basic.R (where most of the other lgb.train() tests live) has gotten kind of big, and there are already uses of lgb.train() outside of that file in their own files (such as https://github.com/microsoft/LightGBM/blob/c629cb0b6bd2830894b710cbd4d8241b82ac3105/R-package/tests/testthat/test_learning_to_rank.R or https://github.com/microsoft/LightGBM/blob/c629cb0b6bd2830894b710cbd4d8241b82ac3105/R-package/tests/testthat/test_custom_objective.R).


test_that("Gamma regression reacts on 'weight'", {
n <- 100L
set.seed(87L)
X <- matrix(runif(2L * n), ncol = 2L)
y <- X[, 1L] + X[, 2L] + runif(n)
X_pred <- X[1L:5L, ]

params <- list(objective = "gamma")

# Unweighted
dtrain <- lgb.Dataset(X, label = y)
bst <- lgb.train(
params = params
, data = dtrain
, nrounds = 4L
, verbose = 0L
)
pred_unweighted <- predict(bst, X_pred)

# Constant weight 1
dtrain <- lgb.Dataset(
X
, label = y
, weight = rep(1.0, n)
)
bst <- lgb.train(
params = params
, data = dtrain
, nrounds = 4L
, verbose = 0L
)
pred_weighted_1 <- predict(bst, X_pred)

# Constant weight 2
dtrain <- lgb.Dataset(
X
, label = y
, weight = rep(2.0, n)
)
bst <- lgb.train(
params = params
, data = dtrain
, nrounds = 4L
, verbose = 0L
)
pred_weighted_2 <- predict(bst, X_pred)

# Non-constant weights
dtrain <- lgb.Dataset(
X
, label = y
, weight = seq(0.0, 1.0, length.out = n)
)
bst <- lgb.train(
params = params
, data = dtrain
, nrounds = 4L
, verbose = 0L
)
pred_weighted <- predict(bst, X_pred)

expect_equal(pred_unweighted, pred_weighted_1)
expect_equal(pred_weighted_1, pred_weighted_2)
expect_false(all(pred_unweighted == pred_weighted))
})
2 changes: 1 addition & 1 deletion src/objective/regression_objective.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ class RegressionGammaLoss : public RegressionPoissonLoss {
} else {
#pragma omp parallel for schedule(static)
for (data_size_t i = 0; i < num_data_; ++i) {
gradients[i] = static_cast<score_t>(1.0 - label_[i] * std::exp(-score[i]) * weights_[i]);
gradients[i] = static_cast<score_t>((1.0 - label_[i] * std::exp(-score[i])) * weights_[i]);
hessians[i] = static_cast<score_t>(label_[i] * std::exp(-score[i]) * weights_[i]);
}
}
Expand Down