Sub topic 5
LightGBM: GOSS and Exclusive Feature Bundling
This chapter assumes you know how gradient boosted trees work and how LightGBM builds histograms. You should also be comfortable with lgb.train and early stopping. GOSS and EFB are the two techniques from the LightGBM paper that cut training cost. GOSS reduces the number of rows used per tree. EFB reduces the number of features.
GOSS: Gradient-based One-Side Sampling
In boosting, each row has a gradient that shows how badly the current model fits it. Rows with small gradients are already well fit and add little information to the next tree. Rows with large gradients matter most.
GOSS keeps the informative rows and samples the rest:
- Sort rows by absolute gradient.
- Keep the top
top_ratefraction (for example 20%). - Randomly sample an
other_ratefraction (for example 10%) of the remaining rows. - Multiply the weight of the sampled small-gradient rows by
(1 - top_rate) / other_rate. This keeps the gradient sums close to what the full data would give.
EFB: Exclusive Feature Bundling
Wide, sparse data, such as one-hot encoded columns, has many features that are rarely nonzero together. Two such features are mutually exclusive. EFB merges them into one bundle. It gives each feature its own range of bins by adding an offset, so the original values can still be told apart.
Histogram construction then costs time proportional to the number of bundles instead of the number of features. Finding the best bundling is NP-hard, so LightGBM uses a greedy graph-coloring heuristic. It tolerates a small fraction of conflicts, which are rows where bundled features are nonzero together. The max_conflict_rate parameter controls that fraction.
Using them in LightGBM
EFB is on by default (enable_bundle=true). GOSS is opt-in. In LightGBM 4.0 and later, select it with data_sample_strategy. Older versions used boosting="goss", which is now deprecated.
import numpy as np
import lightgbm as lgb
from sklearn.model_selection import train_test_split
rng = np.random.default_rng(0)
n, k = 20000, 50
group = rng.integers(0, k, n)
X = np.zeros((n, k))
X[np.arange(n), group] = 1.0 # one-hot: columns are mutually exclusive
y = ((group % 3 == 0) ^ (rng.random(n) < 0.1)).astype(int)
X_train, X_valid, y_train, y_valid = train_test_split(
X, y, test_size=0.2, random_state=0
)
train_set = lgb.Dataset(X_train, label=y_train)
valid_set = lgb.Dataset(X_valid, label=y_valid, reference=train_set)
params = {
"objective": "binary",
"metric": "auc",
"data_sample_strategy": "goss",
"top_rate": 0.2,
"other_rate": 0.1,
"enable_bundle": True,
"learning_rate": 0.1,
"verbose": -1,
}
booster = lgb.train(
params,
train_set,
num_boost_round=500,
valid_sets=[valid_set],
callbacks=[lgb.early_stopping(30)],
)
print(booster.best_iteration)Common errors and gotchas
- Combining GOSS with bagging. GOSS cannot be used with
bagging_fractionorbagging_freq. Leave those unset. - Setting
top_rate + other_rateabove 1. The two fractions come from the same rows, so their sum must not exceed 1. - Expecting a speedup on small data. On a few thousand rows, sampling overhead and noise can outweigh the savings. GOSS pays off on large datasets.
- Expecting EFB to help on dense data. With few zeros there is nothing to bundle, so the gain is small.
- Changing nothing else. GOSS adds sampling noise. Check the validation metric against a run without GOSS before you adopt it.
Exercise
In GOSS, why are the sampled small-gradient rows multiplied by (1 - toprate) / otherrate?
Show answer
Sampling only other_rate of the small-gradient rows would shrink their share of the gradient sums. The factor restores that share, so the sampled data estimates the same information gain as the full data.
Exercise
A dataset has 500 dense numeric columns with almost no zeros. Will EFB reduce training time much?
Show answer
No. EFB bundles features that are rarely nonzero together. Dense columns conflict with each other, so few or no bundles form.