Registered doParallel cluster does not work with model par /

I can’t work parRF, although other things like parApplywork fine.

I tried makeCluster, as well as makePSOCKclusterseveral such options.

It returns an 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""
+4
source share
2 answers

I can reproduce your error message. To solve this, it took a little hacking. I am not sure if this is a mistake or something else.

But I managed to get it to work by copying the model and adjusting the fit function. I added require(foreach)fit to the function.

, parRF_Mod , , , . , . - , .

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      
+3

: Topepo Github, ! install_github("/topepo/caret/pkg/caret/")

- Github :

# 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);

. stopCluster(cl) stopImplicitCluster() parRF , R RStudio.

+4

Source: https://habr.com/ru/post/1614834/


All Articles