Randomized grid search

To explore the hyperparameter space, we specify values for key parameters that we would like to test in combination. The sklearn library supports RandomizedSearchCV to cross-validate a subset of parameter combinations that are sampled randomly from specified distributions. We will implement a custom version that allows us to leverage early stopping while monitoring the current best-performing combinations so we can abort the search process once satisfied with the result rather than specifying a set number of iterations beforehand.

To this end, we specify a parameter grid according to each library's parameters as before, generate all combinations using the built-in Cartesian product generator provided by the itertools library, and randomly shuffle the result. In the case of LightGBM, we automatically set max_depth as a function of the current num_leaves value, as shown in the following code:

param_grid = dict(
# common options
learning_rate=[.01, .1, .3],
colsample_bytree=[.8, 1], # except catboost

# lightgbm
num_leaves=[2 ** i for i in range(9, 14)],
boosting=['gbdt', 'dart'],
min_gain_to_split=[0, 1, 5], # not supported on GPU

all_params = list(product(*param_grid.values()))
n_models = len(all_params) # max number of models to cross-validate
shuffle(all_params)

We then execute cross-validation as follows:

GBM = 'lightgbm'
for
test_param in all_params:
cv_params = get_params(GBM)
cv_params.update(dict(zip(param_grid.keys(), test_param)))
if GBM == 'lightgbm':
cv_params['max_depth'] = int(ceil(np.log2(cv_params['num_leaves'])))
results[n] = run_cv(test_params=cv_params,
data=datasets,
n_splits=n_splits,
gb_machine=GBM)

The run_cv function implements cross-validation for all three libraries. For the light_gbm example, the process looks as follows:

def run_cv(test_params, data, n_splits=10):
"""Train-Validate with early stopping"""
result = []
cols = ['rounds', 'train', 'valid']
for fold in range(n_splits):
train = data[fold]['train']
valid = data[fold]['valid']

scores = {}
model = lgb.train(params=test_params,
train_set=train,
valid_sets=[train, valid],
valid_names=['train', 'valid'],
num_boost_round=250,
early_stopping_rounds=25,
verbose_eval=50,
evals_result=scores)

result.append([model.current_iteration(),
scores['train']['auc'][-1],
scores['valid']['auc'][-1]])

return pd.DataFrame(result, columns=cols)

The train() method also produces validation scores that are stored in the scores dictionary. When early stopping takes effect, the last iteration is also the best score. See the full implementation on GitHub for additional details.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.216.229