Skip to content

Commit 53218c1

Browse files
authored
[R-package] minor improvements on weight demo (#5212)
minor improvements on weight demo
1 parent 2871b53 commit 53218c1

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

R-package/demo/weight_param.R

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
library(lightgbm)
77

88
# We will train a model with the following scenarii:
9-
# - Run 1: sum of weights equal to 0.06513 without adjusted regularization (not learning)
10-
# - Run 2: sum of weights equal to 0.06513 with adjusted regularization (learning)
11-
# - Run 3: sum of weights equal to 6513 (x 1e5) with adjusted regularization (learning)
9+
# - Run 1: sum of weights equal to 6513 (x 1e-5) without adjusted regularization (not learning)
10+
# - Run 2: sum of weights equal to 6513 (x 1e-5) adjusted regularization (learning)
11+
# - Run 3: sum of weights equal to 6513 with adjusted regularization (learning)
1212

1313
# Setup small weights
14-
weights1 <- rep(1.0 / 100000.0, 6513L)
15-
weights2 <- rep(1.0 / 100000.0, 1611L)
14+
weights1 <- rep(1e-5, 6513L)
15+
weights2 <- rep(1e-5, 1611L)
1616

1717
# Load data and create datasets
1818
data(agaricus.train, package = "lightgbm")
@@ -23,7 +23,7 @@ test <- agaricus.test
2323
dtest <- lgb.Dataset.create.valid(dtrain, test$data, label = test$label, weight = weights2)
2424
valids <- list(test = dtest)
2525

26-
# Run 1: sum of weights equal to 0.06513 without adjusted regularization (not learning)
26+
# Run 1: sum of weights equal to 6513 (x 1e-5) without adjusted regularization (not learning)
2727
# It cannot learn because regularization is too large!
2828
# min_sum_hessian alone is bigger than the sum of weights, thus you will never learn anything
2929
params <- list(
@@ -47,7 +47,7 @@ model <- lgb.train(
4747
weight_loss <- as.numeric(model$record_evals$test$l2$eval)
4848
plot(weight_loss) # Shows how poor the learning was: a straight line!
4949

50-
# Run 2: sum of weights equal to 0.06513 with adjusted regularization (learning)
50+
# Run 2: sum of weights equal to 6513 (x 1e-5) with adjusted regularization (learning)
5151
# Adjusted regularization just consisting in multiplicating results by 1e4 (x10000)
5252
# Notice how it learns, there is no issue as we adjusted regularization ourselves
5353
params <- list(
@@ -71,15 +71,8 @@ model <- lgb.train(
7171
small_weight_loss <- as.numeric(model$record_evals$test$l2$eval)
7272
plot(small_weight_loss) # It learns!
7373

74-
# Run 3: sum of weights equal to 6513 (x 1e5) with adjusted regularization (learning)
75-
76-
# And now, we are doing as usual
77-
library(lightgbm)
78-
data(agaricus.train, package = "lightgbm")
79-
train <- agaricus.train
74+
# Run 3: sum of weights equal to 6513 with adjusted regularization (learning)
8075
dtrain <- lgb.Dataset(train$data, label = train$label)
81-
data(agaricus.test, package = "lightgbm")
82-
test <- agaricus.test
8376
dtest <- lgb.Dataset.create.valid(dtrain, test$data, label = test$label)
8477
valids <- list(test = dtest)
8578

0 commit comments

Comments
 (0)