Parallelizing Caret code

痞子三分冷 提交于 2019-12-06 14:57:44

问题


I am having hard time to figure out why this code does not parallelize. I am taking the reproducible example straight from the caret web page.

library(caret)
library(mlbench)
library(Hmisc)
library(randomForest)
library(doMC)
registerDoMC(cores = 3)

n <- 100
p <- 40
sigma <- 1
set.seed(1)
sim <- mlbench.friedman1(n, sd = sigma)
colnames(sim$x) <- c(paste("real", 1:5, sep = ""),
                 paste("bogus", 1:5, sep = ""))
bogus <- matrix(rnorm(n * p), nrow = n)
colnames(bogus) <- paste("bogus", 5+(1:ncol(bogus)), sep = "")
x <- cbind(sim$x, bogus)
y <- sim$y

normalization <- preProcess(x)
x <- predict(normalization, x)
x <- as.data.frame(x)
subsets <- c(1:5, 10, 15, 20, 25)

set.seed(10)
ctrl <- rfeControl(functions = lmFuncs,
               method = "repeatedcv",
               repeats = 50,
               verbose = FALSE)
lmProfile <- rfe(x, y,
             sizes = subsets,
             rfeControl = ctrl)
plot(lmProfile)

All run smooth but with the lines of codes for parallelization:

library(doMC)
registerDoMC(cores = 3)

the previous example is using 1 core. Without the parallelizing code, the previous example use 2 cores. I am using 'top' and 'htop' to check the number of cores. I want using all my 4 cores and I don't understand where is problem.

> sessionInfo()
  R version 3.1.0 (2014-04-10)
  Platform: x86_64-pc-linux-gnu (64-bit)

  locale:
  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8           LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8     LC_PAPER=en_US.UTF-8      
  [8] LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

  attached base packages:
  [1] stats     graphics  grDevices utils     datasets  methods   base     

  loaded via a namespace (and not attached):
  [1] BradleyTerry2_1.0-5 brglm_0.5-9         car_2.0-20          caret_6.0-30        codetools_0.2-8     colorspace_1.2-4    digest_0.6.4        foreach_1.4.2       ggplot2_1.0.0      
  [10] grid_3.1.0          gtable_0.1.2        gtools_3.4.1        iterators_1.0.7     kernlab_0.9-19      lattice_0.20-29     lme4_1.1-7          MASS_7.3-31         Matrix_1.1-3       
  [19] minqa_1.2.3         munsell_0.4.2       nlme_3.1-117        nloptr_1.0.0        nnet_7.3-8          plyr_1.8.1          proto_0.3-10        Rcpp_0.11.1         reshape2_1.4       
  [28] scales_0.2.4        splines_3.1.0       stringr_0.6.2   

Update: With R version 3.0.3 all works fine. Seems a bug. Solution -> Downgrade R

来源:https://stackoverflow.com/questions/25308532/parallelizing-caret-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!