Sub topic 5
LightGBM: GOSS and Exclusive Feature Bundling
This chapter assumes you know how gradient boosted trees work and how LightGBM builds feature histograms. It also assumes you can train a model with lgb.train. If histogram binning is unfamiliar, read that chapter first.
Histogram binning makes each split search cheap. Two costs remain. Every boosting round still scans all rows to build histograms, and every round scans all features. LightGBM's original paper proposed two techniques that attack these costs:
- GOSS (Gradient-based One-Side Sampling) reduces the number of rows used per round.
- EFB (Exclusive Feature Bundling) reduces the number of features by merging sparse ones.
Both are speed optimizations. Neither is meant to raise accuracy, and both can lower it slightly when misused.
GOSS: sampling rows by gradient size
In gradient boosting, each row has a gradient: how far the current prediction is from the target, in the loss function's terms. Rows with small gradients are already well fit. Rows with large gradients are poorly fit and carry most of the information for the next tree.
GOSS exploits this. In each round it:
- Sorts rows by the absolute value of their gradient.
- Keeps the top
afraction (the large-gradient rows) in full. - Draws a random sample of fraction
bfrom the remaining rows. - Multiplies the gradients and hessians of the sampled small-gradient rows by
(1 - a) / b. - Builds the tree on the kept and sampled rows only.
Step 4 matters. Without it, small-gradient rows would be under-represented and the estimated split gain would be biased. The multiplier restores their total contribution, so the gain estimate stays close to the one from the full data.
GOSS parameters
| Parameter | Default | Meaning |
|---|---|---|
| `data_sample_strategy` | `bagging` | Set to `goss` to enable GOSS (LightGBM 4.0 and later). |
| `top_rate` | 0.2 | Fraction `a` of large-gradient rows always kept. |
| `other_rate` | 0.1 | Fraction `b` of the whole dataset sampled from the small-gradient rows. |
With the defaults, each round uses about 30% of the rows. other_rate is a fraction of the whole dataset, not of the remaining rows. top_rate + other_rate must not exceed 1.
In LightGBM versions before 4.0, you enabled GOSS with boosting="goss". Version 4.x still accepts that spelling but marks it as deprecated.
GOSS gotchas
- No bagging. GOSS replaces row subsampling. Setting
bagging_fractionorbagging_freqtogether with GOSS raises an error or is ignored, depending on the version. Pick one row-sampling strategy. - Warm-up. LightGBM does not apply GOSS in the first
1 / learning_raterounds. Withlearning_rate=0.1that is 10 rounds. Early gradients are large for almost every row, so sampling then has little value. - Small data. On a few thousand rows, GOSS saves little time and the sampling noise can hurt. It pays off on large datasets, where histogram construction dominates the training time.
- Noisy labels. Mislabeled rows keep large gradients forever, so GOSS keeps selecting them. On noisy data it can emphasize the noise.
- Plain GBDT is the default for a reason. Treat GOSS as an option to test, not as an upgrade.
EFB: bundling features that are rarely active together
Wide, sparse tables are common. One-hot encoded columns are the typical case: for any row, at most one dummy column of a category is nonzero. Such features are mutually exclusive, meaning they almost never take non-default values at the same time.
EFB merges several exclusive features into one bundle. The bundle is a single column that stores each original feature in its own range of histogram bins.
Suppose feature f1 has bins 0 to 9 and f2 has bins 0 to 4. The bundle gives f1 the bins 0 to 9 and shifts f2 by an offset of 10. A bundle value of 3 then means f1 is in bin 3. A value of 12 means f2 is in bin 2. A row where both are at their default value lands in bin 0. Because the original features are exclusive, no information is lost, and the tree can still split between any two original bins.
Histogram cost depends on the number of columns, so fewer columns means less work per round.
Finding the bundles
The optimal assignment of features to bundles is equivalent to graph coloring, which is NP-hard. LightGBM uses a greedy approximation:
- Build a graph where features are nodes and edges are weighted by how often two features are both non-default in the same row.
- Sort features by degree, so the most conflicted ones come first.
- Put each feature in the first bundle where its conflict count stays under a limit, or start a new bundle.
The limit is set by max_conflict_rate, which defaults to 0. With 0, only strictly exclusive features share a bundle and the result is lossless. A larger value allows a few conflicting rows per bundle. That gives more bundling and more speed, but conflicting values overwrite each other and the model sees slightly corrupted data.
EFB parameters
| Parameter | Default | Meaning |
|---|---|---|
| `enable_bundle` | `true` | Turns EFB on or off. |
| `max_conflict_rate` | 0.0 | Fraction of rows allowed to conflict inside a bundle. |
EFB runs when the Dataset is built, so these are dataset parameters. Passing them to lgb.train works, because the dataset is built at that point.
Comparing the options in code
The script below builds a sparse matrix with 5 dense columns and a 200-column one-hot block. It then trains four configurations and reports validation AUC and training time. Exact numbers depend on your machine. The relative pattern is what you should look at.
import time
import numpy as np
import scipy.sparse as sp
import lightgbm as lgb
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import train_test_split
rng = np.random.default_rng(0)
n = 50000
dense = rng.normal(size=(n, 5))
cat = rng.integers(0, 200, n)
onehot = sp.csr_matrix((np.ones(n), (np.arange(n), cat)), shape=(n, 200))
X = sp.hstack([sp.csr_matrix(dense), onehot]).tocsr()
effect = rng.normal(size=200)
logit = dense[:, 0] + effect[cat]
y = (logit + rng.normal(size=n) > 0).astype(int)
X_train, X_valid, y_train, y_valid = train_test_split(
X, y, test_size=0.2, random_state=0
)
base = {
"objective": "binary",
"metric": "auc",
"learning_rate": 0.1,
"num_leaves": 31,
"verbose": -1,
}
configs = {
"gbdt + EFB": {},
"gbdt, no EFB": {"enable_bundle": False},
"goss + EFB": {"data_sample_strategy": "goss", "top_rate": 0.2, "other_rate": 0.1},
"goss, no EFB": {
"data_sample_strategy": "goss",
"top_rate": 0.2,
"other_rate": 0.1,
"enable_bundle": False,
},
}
for name, extra in configs.items():
train_set = lgb.Dataset(X_train, label=y_train)
start = time.perf_counter()
booster = lgb.train({**base, **extra}, train_set, num_boost_round=200)
elapsed = time.perf_counter() - start
auc = roc_auc_score(y_valid, booster.predict(X_valid))
print(f"{name:14s} AUC={auc:.4f} time={elapsed:.2f}s")You should see the AUC values stay close to each other. The runs with EFB should be faster than the ones without it, and the GOSS runs should be faster again. On a dataset this small the gaps are modest, and they grow with rows and columns.
When to use each
- Leave EFB on. It is lossless at
max_conflict_rate=0, and it costs almost nothing on dense data, where no bundles form. Turn it off only to debug or to compare timings. - Try GOSS when training time is the bottleneck. That usually means millions of rows. Compare validation scores against plain
gbdtwith the same early stopping setup, and keep GOSS only if the score loss is acceptable. - Use
bagging_fractionfor regularization. If you want row subsampling to reduce overfitting rather than save time, stay with bagging. GOSS is a speed tool and gives no comparable regularization benefit.
Exercise
You train with data_sample_strategy="goss", top_rate=0.1, other_rate=0.2 on 1,000,000 rows. How many rows does each tree use after the warm-up, and by what factor are the sampled small-gradient rows reweighted?
Show answer
Each tree uses about 300,000 rows: 100,000 large-gradient rows (10%) plus 200,000 sampled rows (20% of the whole dataset). The sampled small-gradient rows have their gradients and hessians multiplied by (1 - a) / b = (1 - 0.1) / 0.2 = 4.5.
Exercise
Why can EFB bundle one-hot columns without losing information, and what happens if you raise max_conflict_rate above 0?
Show answer
One-hot columns are mutually exclusive: at most one of them is nonzero in any row. Each original feature gets its own offset range of bins in the bundle, so every original value can still be recovered. With max_conflict_rate above 0, LightGBM also bundles features that are sometimes non-default in the same row. In those rows one value overwrites the other, so the bundle holds slightly wrong data in exchange for more bundling and more speed.