mlr-org/mlrMBO

error in objective function with makeTuneControlMBO

AleBitetto opened this issue · 3 comments

Hi,

I'm trying tuning with MBO but I get Infinite value for objective function. If I use grid search everything works fine. Here's the minimal reproducible example.

Thanks a lot

library(randomForest)
library(mlr)
library(mlrMBO)


# define task for outer resampling
task = makeRegrTask(id = "Regression",
                    data = bh.task$env$data,
                    target = 'crim',
                    fixup.data = "warn", 
                    check.data = T
)

learner_type = "regr.randomForest"
n_var = ncol(task$env$data) - 1
n_obs = nrow(task$env$data)
param_set = makeParamSet(
  makeNumericParam("ntree", 10, 200),
  makeNumericParam("mtry", 1, n_var),
  makeNumericParam("nodesize", 1, n_obs-10)
)
# settings
params = list(replace = F,
              importance = T,
              localImp = F,
              proximity = T,
              do.trace = F,
              keep.forest = T)

# tuned parameters
params = c(params,
           ntree = 50,
           mtry = 3,
           nodesize = 5)

# set learner
learner = makeLearner(cl = learner_type,
                      par.vals = params,
                      predict.type = 'response',
                      fix.factors.prediction = T)

# define optimization strategy
control = makeTuneControlMBO()
# control = makeTuneControlGrid(resolution = 3)

# define INNER and OUTER resampling strategy
inner = makeResampleDesc(method = "CV",
                         iters = 5,
                         predict = "both"
                         # stratify.cols = c('country')
)

outer = makeResampleDesc(method = "CV",
                         iters = 5,
                         predict = "both"
                         # stratify.cols = c('country')
)

# make learner wrapper
learner_t = makeTuneWrapper(learner = learner,
                            resampling = inner,
                            par.set = param_set,
                            control = control,
                            measures = rmse,
                            show.info = F)

# tuning
res = resample(learner = learner_t,
               task = task,
               resampling = outer,
               extract = getTuneResult,
               show.info = F,
               models = T)

that returns

Error in evalTargetFun.OptState(opt.state, xs, extras) : 
  Objective function output must be a numeric of length 1, but we got: Inf

it is not a good idea to disable logging in cases like above.

otherwise you would have seen yourself:

[Tune-x] Setting hyperpars failed: Error in setHyperPars2.Learner(
learner, insert(par.vals, args)) :
  32.72 is not feasible for parameter 'ntree'!

You need to call makeIntegerParam not makeNumericParam for the integer param in your code above

i did that change in your code locally for me, and then it worked

thanks a lot, it works