首页 > 解决方案 > 在多个线程中使用 caret::trainControl 是否安全?

问题描述

我反复使用 caret::train 函数来生成许多独立的模型。我让它并行运行,我想知道是否需要为 caret::train 的每次迭代创建 caret::trainControl 对象。caret::train 函数在什么情况下会修改 trainControl 对象吗?

在代码中,我猜它可以简化为...

require(caret)

models <- c("mpg ~ wt", "mpg ~ hp")

# Do I have to do this?
for (m in models) {
  tc <- caret::trainControl(method="repeatedcv", number=5, repeats=5)
  model <- caret::train(as.formula(m), data=mtcars, trControl=tc, method="lm")
}

# Or can I get away with this
tc <- caret::trainControl(method="repeatedcv", number=5, repeats=5)
for (m in models) {
  model <- caret::train(as.formula(m), data=mtcars, trControl=tc, method="lm")
}

显然,我所有的列车控制参数对于每个模型都是相同的。

谢谢!

标签: rparallel-processingr-caret

解决方案


推荐阅读