【发布时间】:2014-11-10 08:56:35
【问题描述】:
我正在使用插入符号来训练模型重采样和调整学习参数,我可以询问每个测试的概率,这很棒。但我也很想保留模型对象并在以后使用它们而无需重新训练——这可能吗?基本上,我想要的不仅仅是 mdl$finalModel 对象,而是每次调优迭代的模型对象。
【问题讨论】:
我正在使用插入符号来训练模型重采样和调整学习参数,我可以询问每个测试的概率,这很棒。但我也很想保留模型对象并在以后使用它们而无需重新训练——这可能吗?基本上,我想要的不仅仅是 mdl$finalModel 对象,而是每次调优迭代的模型对象。
【问题讨论】:
不是真的。您可以编写 custom method 并修改 fit 函数以将它们保存到文件中。在 fit 函数中,您将知道调整参数值,但不知道构建模型的重采样。
最大
【讨论】:
谢谢马克斯。我正在使用您的建议,因此如果其他人想尝试此操作,我将在此处发布我的代码。我稍后会通过保存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)
}
【讨论】: