LoginSignup
1
0

More than 5 years have passed since last update.

Rのsnowパッケージを使ってrandomForestを並列化

Last updated at Posted at 2016-03-25

:arrow_forward: 通常と並列化の比較

:small_blue_diamond:通常のランダムフォレスト

library(randomForest)
library(kernlab)

data(spam)
set.seed(777)
system.time(fit.rf<-randomForest(type~.,data=spam,ntree=1000))
ユーザ システム 経過
21.584 0.213 21.919

:small_blue_diamond:並列化のランダムフォレスト

library(snow)
cl <- makeCluster(4,type="SOCK")
invisible(clusterEvalQ(cl,{
        library(randomForest)
        library(kernlab)
        data(spam)
    }))
set.seed(777)

print(system.time(fit.rf <- clusterApply(cl,rep(250,4),function(ntree) randomForest(type~.,spam,ntree=ntree))))
fit.rf.all<-do.call("combine",fit.rf)
stopCluster(cl)
ユーザ システム 経過
0.053 0.036 11.525

:arrow_forward: 並列化方法解説

他のプロセスでもライブラリや変数を利用するために通常の読み込みとは別に事前準備が必要となる。また、関数内で並列化を行う場合引数を利用することはできない(引数はclusterExportすることができない)。

  • クラスターの生成
cl <- makeCluster(4,type="SOCK")

アクティビティモニタを利用するとプロセスが4つになっていることが確認できる。
use_snow.png

  • ライブラリなどの読み込み
invisible(clusterEvalQ(cl,{
    library(randomForest)
    library(kernlab)
    data(spam)
})))
  • 変数の利用方法
clusterExport(cl,"value1")
  • クラスターの終了
stopCluster(cl)

エラーなどによって正常に終了することができなくて放置すると
memory_snow.png
一度Rを終了させればリセットされる。

参考資料

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0