Welcome to GBDTMO’s documentation!¶
GBDTMO is a gradient boosted decision tree method which supports learning multiple outputs within a single tree. GBDTMO constructs the predicts of all outputs or a subset of automatically selected outputs on a leaf. Compared with GBDT for single output, GBDTMO has better generalization ability and faster training speed. See our paper for technical details.
Python API¶
load_lib¶
load_lib(path): |
|
---|
create_graph¶
create_graph(file_name, tree_index=0, value_list=[]): | |
---|---|
This function generate a
|
GBDTMulti¶
GBDTMulti(lib, out_dim=1, params={}): | |||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Create an instance of GBDTMO model.
|
GBDTSingle¶
GBDTSO is our own implementation of GBDT for single output. It is used to compare the training speed and accuracy with GBDTMO.
GBDTSingle(lib, out_dim, params={}): | |||||||
---|---|---|---|---|---|---|---|
Create an instance of GBDTSO model. Most of method is shared with GBDTSO. Here we only list the specific methods of GBDTSO.
|
Parameters¶
This page contains descriptions of all parameters in GBDTMO.
Meta¶
verbose
: default = True, type = bool- If True, print loss information every round. Otherwise, print nothing.
seed
: default = 0, type = int.- Random seed. No effect currently.
num_threads
: default = 2, type = int.- Number of threads for training.
hist_cache
: default = 16, type = int.- Maximum number of histogram cache
topk
: default = 0.- Sparse factors for sparse split finding.
- If 0, non-sparse split finding is used.
one_side
: default = True, type = bool.- Algorithm type for sparse split finding.
- If True, the restricted one is used.
- Only used when topk not equal to 0.
max_bins
: default = 32, type = int.- Maximum number of bins for each input variable.
Tree¶
max_depth
: default = 4, type = int.- Maximum depth of trees, at least 1.
max_leaves
: default = 32, type = int.- Maximum leaves of each tree.
min_samples
: default = 20, type = int.- Minimum number of samples of each leaf.
- Stop growth if current number of samples smaller than this value.
early_stop
: default = 0, type = int.- Number of rounds for early stop.
- If 0, early stop is not used.
Learning¶
base_score
: default = 0.0, type = double.- Initial value of prediction.
subsample
: default = 1.0, type = double.- Column sample rate. No effect currently.
lr
: default = 0.2, type = double.- Learning rate.
reg_l1
: default = 0.0, type = double.- L1 regularization.
- Not used for sparse split finding currently.
reg_l2
: default = 1.0, type = double.- L2 regularization.
gamma
: default = 1e-3, type = double.- Minimum objective gain to split.
loss
: default = ‘mse’, type = string.- Must be binary coding. For example, b’mse’ in Python.
- Must be one of ‘mse’ (mean square error), ‘bce’ (binary cross entropy), ‘ce’ (cross entropy), and ‘ce_column’ ( only for
GBDTSingle
).
Examples¶
Plotting tree¶
Suppose the model is dumped into gbdtmo.txt
, plot 5th tree by:
>>> from gbdtmo import create_graph
>>> from graphviz import Digraph
>>> graph = create_graph("gbdtmo.txt", 5, [0, 3])
>>> graph.render("tree_5", format='pdf')
Then tree_5.pdf
will be generated.
Using GBDTMO¶
First import gbdtmo
>>> from gbdtmo import GBDTMulti, load_lib
Load from gbdtmo.so
>>> LIB = load_lib("path to gbdtmo.so")
Build an instance of GBDTMO. Here the out_dim
is set to 10 and MSE loss is used.
>>> inp_dim, out_dim = 10, 5
>>> params = {"max_depth": 5, "lr": 0.1, 'loss': b"mse"}
>>> booster = GBDTMulti(LIB, out_dim=out_dim, params=params)
Set the training and eval datasets.
>>> x_train, y_train = np.random.rand(10000, inp_dim), np.random.rand(10000, out_dim)
>>> x_valid, y_valid = np.random.rand(10000, inp_dim), np.random.rand(10000, out_dim)
>>> booster.set_data((x_train, y_train), (x_valid, y_valid))
Training with 30 rounds and dump it into text file.
>>> booster.train(30)
>>> booster.dump(b"tree.txt")
Custom loss¶
We show how to train GBDTMO via custom loss. Here is an example of MSE.
def MSE(x, y):
g = x - y
h = np.ones_like(x)
return g, h
>>> g, h = MSE(booster.preds_train.copy(), booster.label.copy())
>>> booster._set_gh(g, h)
>>> booster.boost()
In this way, a new tree is constructed and the predictions are updated.
Feel free to contact with us if you have any questions or suggestions.