【发布时间】:2016-09-08 11:28:16
【问题描述】:
我正在尝试训练几个随机森林(用于回归),让它们进行竞争,看看哪些特征选择和哪些参数可以提供最佳模型。
但是培训似乎花费了很多时间,我想知道我是否做错了什么。
我用于训练的数据集(下面称为 train)有 217k 行和 58 列(其中只有 21 个用作随机森林中的预测变量。它们都是 numeric 或 integer,除了布尔值,它属于 character 类。y 输出是 numeric)。
我运行以下代码四次,将值4、100、500、2000 赋予nb_trees:
library("randomForest")
nb_trees <- #this changes with each test, see above
ptm <- proc.time()
fit <- randomForest(y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9
+ x10 + x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19
+ x20 + x21,
data = train,
ntree = nb_trees,
do.trace=TRUE)
proc.time() - ptm
这是他们每个人训练的时间:
nb_trees | time
4 4mn
100 1h 41mn
500 8h 40mn
2000 34h 26mn
由于我公司的服务器有 12 个内核和 125Go 的 RAM,我想我可以尝试按照 this answer 并行化训练(但是,我使用了 doParallel 包,因为它似乎与 doSNOW 一起永远运行,我不知道为什么。我找不到在哪里看到 doParallel 也可以工作,抱歉)。
library("randomForest")
library("foreach")
library("doParallel")
nb_trees <- #this changes with each test, see table below
nb_cores <- #this changes with each test, see table below
cl <- makeCluster(nb_cores)
registerDoParallel(cl)
ptm <- proc.time()
fit <- foreach(ntree = rep(nb_trees, nb_cores), .combine = combine, .packages = "randomForest")
%dopar% {
randomForest(y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9
+ x10 + x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19
+ x20 + x21,
data = train,
ntree = ntree,
do.trace=TRUE)}
proc.time() - ptm
stopCluster(cl)
当我运行它时,它比非并行代码花费的时间更短:
nb_trees | nb_cores | total number of trees | time
1 4 4 2mn13s
10 10 100 52mn
9 12 108 (closest to 100 with 12 cores) 59mn
42 12 504 (closest to 500 with 12 cores) I won't be running this one
167 12 2004 (closest to 2000 with 12 cores) I'll run it next week-end
但是,我认为这仍然需要很多时间,不是吗?我知道将树木组合成最终的森林需要时间,所以我没想到它在 12 核时会快 12 倍,但它只快了 ~2 倍......
- 这正常吗?
- 如果不是,我可以用我的数据和/或代码做些什么来从根本上减少运行时间吗?
- 如果不是,我应该告诉负责服务器的人,它应该更快吗?
感谢您的回答。
注意事项:
- 我是唯一使用此服务器的人
- 在接下来的测试中,我将删除随机森林中未使用的列
- 我很晚才意识到我可以通过调用
randomForest(predictors,decision)而不是randomForest(decision~.,data=input)来提高运行时间,我将从现在开始这样做,但我认为我上面的问题仍然存在。
【问题讨论】:
标签: r parallel-processing random-forest doparallel parallel-foreach