6

我想问是否可以保存在参数调整期间创建的所有模型,例如tuneParams功能。我想从每个超参数集的每个交叉验证中保存模型。

我可以看到和函数都有models参数,但我找不到一个或类似的函数,我真的无法找到一种使用其他函数来模仿这种行为的方法(我是 mlr 的新手)。resamplebenchmarktuneParams

有没有办法做到这一点?

PS我知道这听起来可能很疯狂,但我需要它来进行一些内部验证。

PS2 不幸的是,似乎还没有“mlr”标签,而且我没有足够的代表来创建一个。

4

1 回答 1

9

我想有更短的解决方案,但以下不是那么 hacky。我们使用 Wrapper 来获取模型,以便我们可以将其保存在全局环境中的列表中。或者,您可以将该行更改为更复杂的内容并将其保存在硬盘上。这可能是值得的,因为模型可以变得很大。

library(mlr)

# Define the tuning problem
ps = makeParamSet(
  makeDiscreteParam("C", values = 2^(-2:2)),
  makeDiscreteParam("sigma", values = 2^(-2:2))
)
ctrl = makeTuneControlGrid()
rdesc = makeResampleDesc("Holdout")
lrn = makeLearner("classif.ksvm")


# Define a wrapper to save all models that were trained with it
makeSaveWrapper = function(learner) {
  mlr:::makeBaseWrapper(
    id = paste0(learner$id, "save", sep = "."),
    type = learner$type,
    next.learner = learner,
    par.set = makeParamSet(),
    par.vals = list(),
    learner.subclass = "SaveWrapper",
    model.subclass = "SaveModel")
}

trainLearner.SaveWrapper = function(.learner, .task, .subset, ...) {
  m = train(.learner$next.learner, task = .task, subset = .subset)
  stored.models <<- c(stored.models, list(m)) # not very efficient, maybe you want to save on hard disk here?
  mlr:::makeChainModel(next.model = m, cl = "SaveModel")
}

predictLearner.SaveWrapper = function(.learner, .model, .newdata, ...) {
  NextMethod(.newdata = .newdata)
}

stored.models = list() # initialize empty list to store results
lrn.saver = makeSaveWrapper(lrn)

res = tuneParams(lrn.saver, task = iris.task, resampling = rdesc, par.set = ps, control = ctrl)

stored.models[[1]] # the normal mlr trained model
stored.models[[1]]$learner.model # the underlying model
getLearnerParVals(stored.models[[1]]$learner) # the hyper parameter settings
stored.models[[1]]$subset # the indices used to train the model
于 2016-10-26T15:09:06.510 回答