插入符号 - 是否可以从调整中保存每个模型? [英] Caret - is it possible to save each models from tuning?

查看:32
本文介绍了插入符号 - 是否可以从调整中保存每个模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用插入符号来训练模型重采样并调整学习参数,我可以询问每个测试的概率,这很棒.但我也很想保留模型对象并在以后使用它们而无需重新训练——这可能吗?基本上,不仅仅是 mdl$finalModel 对象,我想要每次调整迭代的模型对象.

I'm using caret to train models over resamples and tune learning parameters, and I can interrogate the probabilities for each test, which is great. But I'm also keen to retain the model objects and use them later without retraining -- is this possible? Basically rather than just the mdl$finalModel object, I'd like the model object for each iteration of tuning.

推荐答案

谢谢 Max.我正在使用您的建议,因此如果其他人想尝试此操作,我将在此处发布我的代码.我稍后通过保存 rownames(x) 来计算重采样.

Thanks Max. I'm using your suggestion so I'm posting my code here should anyone else want to try this. I am working out the resample later by also saving rownames(x).

# Copy all model structure info from existing model type
cust.mdl <- getModelInfo("rf", regex=FALSE)[[1]]

# Override fit function so that we can save the iteration
cust.mdl$fit <- function(x=x, y=y, wts=wts, param=param, lev=lev, last=last, classProbs=classProbs, ...) {
  # Dont save the final pass (dont train the final model across the entire training set)
  if(last == TRUE) return(NULL) 

  # Fit the model
  fit.obj <- getModelInfo("rf", regex=FALSE)[[1]]$fit(x, y, wts, param, lev, last, classProbs, ...)

  # Create an object with data to save and save it
  fit.data <- list(resample=rownames(x),
                   mdl=fit.obj,
                   #x, y, wts,
                   param=param, lev=lev, last=last, classProbs=classProbs, 
                   other=list(...))

  # Create a string representing the tuning params
  param.str <- paste(lapply(1:ncol(param), function(x) {
                     paste0(names(param)[x], param[1,x])
                    }), collapse="-")

  save(fit.data, file=paste0("rf_modeliter_", sample(1000:9999,1), "_", param.str, ".RData"))
  return (fit.obj)
}

这篇关于插入符号 - 是否可以从调整中保存每个模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆