yanyachen / rBayesianOptimization

Bayesian Optimization of Hyperparameters
81 stars 21 forks source link

Minimizing - v2 #18

Closed OOlys closed 7 years ago

OOlys commented 7 years ago

Hello,

I have read the thread of Dragie about Logloss Minimization, but I don't understand well how to implement it. Could it be possible to provide a short exemple of code to minimize instead of maximise? If understand well, we need to change the Score definition. Is that it?

yanyachen commented 7 years ago

Several details about xgboost is changed, so I'm not sure whether you can run code below successfully, but you can get the idea from it.

library(xgboost)
data(agaricus.train, package = "xgboost")
dtrain <- xgb.DMatrix(agaricus.train$data,
                      label = agaricus.train$label)
cv_folds <- KFold(agaricus.train$label, nfolds = 5,
                  stratified = TRUE, seed = 0)
xgb_cv_bayes <- function(max.depth, min_child_weight, subsample) {
  cv <- xgb.cv(params = list(booster = "gbtree", eta = 0.01,
                             max_depth = max.depth,
                             min_child_weight = min_child_weight,
                             subsample = subsample, colsample_bytree = 0.3,
                             lambda = 1, alpha = 0,
                             objective = "binary:logistic",
                             eval_metric = "logloss"),
               data = dtrain, nround = 100,
               folds = cv_folds, prediction = TRUE, showsd = TRUE,
               early_stopping_rounds = 5, maximize = FALSE, verbose = 0)
  list(Score = cv$evaluation_log[, min(test_logloss_mean)] * (-1),
       Pred = cv$pred)
}
OPT_Res <- BayesianOptimization(xgb_cv_bayes,
                                bounds = list(max.depth = c(2L, 6L),
                                              min_child_weight = c(1L, 10L),
                                              subsample = c(0.5, 0.8)),
                                init_grid_dt = NULL, init_points = 10, n_iter = 20,
                                acq = "ucb", kappa = 2.576, eps = 0.0,
                                verbose = TRUE)
OOlys commented 7 years ago

Many thanks! I have also changed the xgb.cv to maximize=false, and keep for the score the min(test_logloss_mean), that I multiply by -1. Then after the Bayesian optimzation try to maximize the -min(logloss), so it minizes the logloss.

yanyachen commented 7 years ago

Yes, you are right. Updated accordingly.