Skip to content

Commit

Permalink
update autotunning so does not fail
Browse files Browse the repository at this point in the history
note tests still fail though, possibly because random seed is different?
  • Loading branch information
jcharkow committed Nov 1, 2024
1 parent c99db26 commit fdb5513
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions pyprophet/classifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def objective(params):

clf = xgb.XGBClassifier(random_state=42, verbosity=0, objective='binary:logitraw', eval_metric='auc', **params)

score = cross_val_score(clf, X, y, scoring='roc_auc', n_jobs=self.threads, cv=KFold(n_splits=3, shuffle=True, random_state=np.random.RandomState(42))).mean()
rng = np.random.default_rng(42)
score = cross_val_score(clf, X, y, scoring='roc_auc', n_jobs=self.threads, cv=KFold(n_splits=3, shuffle=True, random_state=42)).mean()
# click.echo("Info: AUC: {:.3f} hyperparameters: {}".format(score, params))
return score

Expand All @@ -129,7 +130,8 @@ def objective(params):
xgb_params_complexity = self.xgb_params_tuned
xgb_params_complexity.update({k: self.xgb_params_space[k] for k in ('max_depth', 'min_child_weight')})

best_complexity = fmin(fn=objective, space=xgb_params_complexity, algo=tpe.suggest, max_evals=self.xgb_hyperparams['autotune_num_rounds'], rstate=np.random.RandomState(42))
rng = np.random.default_rng(42)
best_complexity = fmin(fn=objective, space=xgb_params_complexity, algo=tpe.suggest, max_evals=self.xgb_hyperparams['autotune_num_rounds'], rstate=rng)
best_complexity['max_depth'] = int(best_complexity['max_depth'])
best_complexity['min_child_weight'] = int(best_complexity['min_child_weight'])

Expand All @@ -139,31 +141,31 @@ def objective(params):
xgb_params_gamma = self.xgb_params_tuned
xgb_params_gamma['gamma'] = self.xgb_params_space['gamma']

best_gamma = fmin(fn=objective, space=xgb_params_gamma, algo=tpe.suggest, max_evals=self.xgb_hyperparams['autotune_num_rounds'], rstate=np.random.RandomState(42))
best_gamma = fmin(fn=objective, space=xgb_params_gamma, algo=tpe.suggest, max_evals=self.xgb_hyperparams['autotune_num_rounds'], rstate=rng)

self.xgb_params_tuned.update(best_gamma)

# Tune subsampling hyperparameters
xgb_params_subsampling = self.xgb_params_tuned
xgb_params_subsampling.update({k: self.xgb_params_space[k] for k in ('subsample', 'colsample_bytree', 'colsample_bylevel', 'colsample_bynode')})

best_subsampling = fmin(fn=objective, space=xgb_params_subsampling, algo=tpe.suggest, max_evals=self.xgb_hyperparams['autotune_num_rounds'], rstate=np.random.RandomState(42))
best_subsampling = fmin(fn=objective, space=xgb_params_subsampling, algo=tpe.suggest, max_evals=self.xgb_hyperparams['autotune_num_rounds'], rstate=rng)

self.xgb_params_tuned.update(best_subsampling)

# Tune regularization hyperparameters
xgb_params_regularization = self.xgb_params_tuned
xgb_params_regularization.update({k: self.xgb_params_space[k] for k in ('lambda', 'alpha')})

best_regularization = fmin(fn=objective, space=xgb_params_regularization, algo=tpe.suggest, max_evals=self.xgb_hyperparams['autotune_num_rounds'], rstate=np.random.RandomState(42))
best_regularization = fmin(fn=objective, space=xgb_params_regularization, algo=tpe.suggest, max_evals=self.xgb_hyperparams['autotune_num_rounds'], rstate=rng)

self.xgb_params_tuned.update(best_regularization)

# Tune learning rate
xgb_params_learning = self.xgb_params_tuned
xgb_params_learning['eta'] = self.xgb_params_space['eta']

best_learning = fmin(fn=objective, space=xgb_params_learning, algo=tpe.suggest, max_evals=self.xgb_hyperparams['autotune_num_rounds'], rstate=np.random.RandomState(42))
best_learning = fmin(fn=objective, space=xgb_params_learning, algo=tpe.suggest, max_evals=self.xgb_hyperparams['autotune_num_rounds'], rstate=rng)

self.xgb_params_tuned.update(best_learning)
click.echo("Info: Optimal hyperparameters: {}".format(self.xgb_params_tuned))
Expand Down

0 comments on commit fdb5513

Please sign in to comment.