我使用包caret和ranger来拟合随机森林模型,并试图加速使用并行进程。然而,速度增益非常小。我使用的是MacBook Pro(Retina,13英寸,2013年末),2.4 GHz Intel Core i5,8 GB 1600 MHz DDR3,macOS Sierra 10.12。一个可重复的例子:
library(caret)
library(mlbench)
data("Sonar")
start <- Sys.time()
mod_1 <- train(Class ~ ., data = Sonar, method = "ranger", num.trees = 10000)
stop <- Sys.time()
duration1 <- stop - start
duration1
这在3.47分钟内运行。在它们期间,在Activity Monitor中我看到一个R进程,CPU使用率约为300-330%。并行:
library(parallel)
library(doParallel)
cluster <- makeCluster(detectCores() - 1)
registerDoParallel(cluster)
start <- Sys.time()
mod_2 <- train(Class ~ ., data = Sonar, method = "ranger", num.trees = 10000)
stop <- Sys.time()
duration2 <- stop - start
duration2
这在3.06分钟内运行。在它们期间,在活动监视器中,我看到3 R进程,每个进程的CPU使用率约为100-120%。我还测试了插入符号文档(http://topepo.github.io/caret/parallel-processing.html)中建议的doMC包,耗时3.10分钟。根据插入符号文档中的图表,这个速度增益比我预期的要小得多。有什么想法吗?
sessionInfo:
R version 3.3.2 (2016-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Sierra 10.12
locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
attached base packages:
[1] parallel stats graphics grDevices utils datasets methods base
other attached packages:
[1] doParallel_1.0.10 ranger_0.6.0 e1071_1.6-7 doMC_1.3.4 iterators_1.0.8
[6] foreach_1.4.3 mlbench_2.1-1 caret_6.0-73 ggplot2_2.2.1 lattice_0.20-34
loaded via a namespace (and not attached):
[1] Rcpp_0.12.9 magrittr_1.5 splines_3.3.2 MASS_7.3-45 munsell_0.4.3
[6] colorspace_1.3-2 minqa_1.2.4 stringr_1.1.0 car_2.1-4 plyr_1.8.4
[11] tools_3.3.2 nnet_7.3-12 pbkrtest_0.4-6 grid_3.3.2 gtable_0.2.0
[16] nlme_3.1-130 mgcv_1.8-16 quantreg_5.29 class_7.3-14 MatrixModels_0.4-1
[21] lme4_1.1-12 lazyeval_0.2.0 assertthat_0.1 tibble_1.2 Matrix_1.2-8
[26] nloptr_1.0.4 reshape2_1.4.2 ModelMetrics_1.1.0 codetools_0.2-15 stringi_1.1.2
[31] compiler_3.3.2 scales_0.4.1 stats4_3.3.2 SparseM_1.74
更新 在slonopotam的回复之后,我使用包randomForest(版本4.6-12)测试了上面相同的模型。顺序(不平行)运行需要8.14分钟。在它们期间,在Activity Monitor中我看到一个R进程,CPU为95-100%。并行运行需要3.72分钟,其间有3个R进程,每个进程CPU为95-100%。添加此信息只是为了完成。谢谢,slonopotam!
答案 0 :(得分:1)
您正在使用的软件包'ranger'确实具有内部多线程支持。这就是为什么你在第一种情况下观察CPU使用率为300..330%的原因 - 这意味着它已经使用至少3个核心进行培训。
当使用doParallel时,使用多处理而不是多线程,但是训练中使用的计算资源总数几乎相同,因此您没有看到太多的收益。