【发布时间】:2013-11-26 17:59:18
【问题描述】:
我正在尝试使用 snow 包在 R 中对弹性网络模型进行评分,但我无法弄清楚如何让 predict 函数在集群中的多个节点上运行。下面的代码既包含时间基准测试,也包含产生错误的实际代码:
##############
#Snow example#
##############
library(snow)
library(glmnet)
library(mlbench)
data(BostonHousing)
BostonHousing$chas<-as.numeric(BostonHousing$chas)
ind<-as.matrix(BostonHousing[,1:13],col.names=TRUE)
dep<-as.matrix(BostonHousing[,14],col.names=TRUE)
fit_lambda<-cv.glmnet(ind,dep)
#fit elastic net
fit_en<<-glmnet(ind,dep,family="gaussian",alpha=0.5,lambda=fit_lambda$lambda.min)
ind_exp<-rbind(ind,ind)
#single thread baseline
i<-0
while(i < 2000){
ind_exp<-rbind(ind_exp,ind)
i = i+1
}
system.time(st<-predict(fit_en,ind_exp))
#formula for parallel execution
pred_en<-function(x){
x<-as.matrix(x)
return(predict(fit_en,x))
}
#make the cluster
cl<-makeSOCKcluster(4)
clusterExport(cl,"fit_en")
clusterExport(cl,"pred_en")
#parallel baseline
system.time(mt<-parRapply(cl,ind_exp,pred_en))
我已经能够通过使用多核的 Linux 机器上的 fork 进行并行化,但我最终不得不使用性能相当差的 mclapply 与 unlist 相结合,并正在寻找一种更好的方法来处理雪(顺便说一句也行)在我的开发 Windows PC 和我的 prod Linux 服务器上)。谢谢。
【问题讨论】:
标签: r parallel-processing multicore prediction snow