I can't get parRF
working, even though other things like parApply
work just fine.
I've tried makeCluster
as well as makePSOCKcluster
and a few variations like that.
It keeps returning the error task 1 failed - could not find function getDoParWorkers
cores_2_use <- detectCores() - 2
cl <- makeCluster(cores_2_use, useXDR = F)
clusterSetRNGStream(cl, 9956)
registerDoParallel(cl, cores_2_use)
rf_train <- train(y=y, x=x,
method='parRF', tuneGrid = data.frame(mtry = ncol(x)), na.action = na.omit,
trControl=trainControl(method='oob',number=10, allowParallel = TRUE)
)
Error in { : task 1 failed - "could not find function "getDoParWorkers""
I can reproduce your error message. Solving it required a bit of hacking. I'm not sure if this is a bug or something else.
But I managed to get it working by copying the model and adjusting the fit function. I added the require(foreach)
in the fit function.
Strangely enough once the train model has run with the new parRF_Mod as a method, the original train where the error appeared is working without any errors. Start with a clean session and the error appears again. So somewhere something is not going as it should.
library(doParallel)
cl = makeCluster(parallel::detectCores()-1, type = "SOCK")
registerDoParallel(cl)
getDoParWorkers()
library(caret)
library(randomForest)
y <- mtcars$mpg
x <- mtcars[, -mtcars$mpg ]
parRF_mod <- getModelInfo("parRF", regex = FALSE)[[1]]
parRF_mod$fit <- function (x, y, wts, param, lev, last, classProbs, ...)
{
# added the requirement of foreach
require(foreach)
workers <- getDoParWorkers()
theDots <- list(...)
theDots$ntree <- if (is.null(theDots$ntree))
250
else theDots$ntree
theDots$x <- x
theDots$y <- y
theDots$mtry <- param$mtry
theDots$ntree <- ceiling(theDots$ntree/workers)
out <- foreach(ntree = 1:workers, .combine = combine) %dopar%
{
library(randomForest)
do.call("randomForest", theDots)
}
out$call["x"] <- "x"
out$call["y"] <- "y"
out
}
rf_train <- train(y=y, x=x,
method=parRF_mod, tuneGrid = data.frame(mtry = ncol(x)), na.action = na.omit,
trControl=trainControl(method='oob',number=10, allowParallel = TRUE)
)
stopcluster(cl)
my sessionInfo:
R version 3.2.2 (2015-08-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=Dutch_Netherlands.1252 LC_CTYPE=Dutch_Netherlands.1252 LC_MONETARY=Dutch_Netherlands.1252 LC_NUMERIC=C
[5] LC_TIME=Dutch_Netherlands.1252
attached base packages:
[1] parallel stats graphics grDevices utils datasets methods base
other attached packages:
[1] randomForest_4.6-12 e1071_1.6-7 caret_6.0-58 ggplot2_1.0.1 lattice_0.20-33 doParallel_1.0.10 iterators_1.0.8
[8] foreach_1.4.3
loaded via a namespace (and not attached):
[1] Rcpp_0.12.1 magrittr_1.5 splines_3.2.2 MASS_7.3-44 munsell_0.4.2 colorspace_1.2-6 minqa_1.2.4 stringr_1.0.0
[9] car_2.1-0 plyr_1.8.3 tools_3.2.2 nnet_7.3-11 pbkrtest_0.4-2 grid_3.2.2 gtable_0.1.2 nlme_3.1-122
[17] mgcv_1.8-8 quantreg_5.19 snow_0.3-13 class_7.3-14 MatrixModels_0.4-1 lme4_1.1-10 digest_0.6.8 Matrix_1.2-2
[25] nloptr_1.0.4 reshape2_1.4.1 codetools_0.2-14 stringi_1.0-1 compiler_3.2.2 scales_0.3.0 stats4_3.2.2 SparseM_1.7
[33] proto_0.3-10
UPDATE: Topepo has updated the code on Github to fix this bug! Just install_github("/topepo/caret/pkg/caret/")
MY PRIOR ANSWER BELOW IS DEPRECATED
Someone from Github also presented this workaround:
# parallel
require(caret); library(doParallel);
cl <- makePSOCKcluster(detectCores());
clusterEvalQ(cl, library(foreach)); registerDoParallel(cl)
y <- mtcars$mpg; x <- mtcars[, -mtcars$mpg];
#--------------------------------------------------------------
rf_train <- train(y=y, x=x,
method='parRF', tuneGrid = data.frame(mtry = ncol(x)), na.action = na.omit,
trControl=trainControl(method='oob',number=10, allowParallel = TRUE)
)
rf_train
#--------------------------------------------------------------
stopCluster(cl);
Be sure to start fresh before running this version of the code. Even after stopCluster(cl)
and stopImplicitCluster()
after another attempt at parRF, this method didn't work for me until I completely restarted R and RStudio.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With