R neuralnet包对于数百万条记录来说太慢了

时间:2016-01-18 17:58:07

标签: r parallel-processing neural-network

我正在尝试用R包神经网训练神经网络进行客户流失预测。这是代码:

data <- read.csv('C:/PredictChurn.csv') 
maxs <- apply(data, 2, max) 
mins <- apply(data, 2, min)
scaled_temp <- as.data.frame(scale(data, center = mins, scale = maxs - mins))
scaled <- data
scaled[, -c(1)] <- scaled_temp[, -c(1)]
index <- sample(1:nrow(data),round(0.75*nrow(data)))
train_ <- scaled[index,]
test_ <- scaled[-index,]
library(neuralnet)
n <- names(train_[, -c(1)])
f <- as.formula(paste("CHURNED_F ~", paste(n[!n %in% "CHURNED_F"], collapse = " + ")))
nn <- neuralnet(f,data=train_,hidden=c(5),linear.output=F)

它可以正常工作,但是当使用完整数据集(在数百万行的范围内)进行训练时,它只需要太长时间。所以我知道R默认是单线程的,所以我尝试研究如何将工作并行化到所有核心。甚至可以并行执行此功能吗?我尝试了各种套餐但没有成功。

有没有人能够做到这一点? 它不一定是神经网络包,任何让我训练神经网络的解决方案都可行。

谢谢

2 个答案:

答案 0 :(得分:1)

我对包Rmpi有很好的体验,也可能适用于您的情况。

library(Rmpi)

简而言之,它的用法如下:

nproc = 4  # could be automatically determined
# Specify one master and nproc-1 slaves
Rmpi:: mpi.spawn.Rslaves(nslaves=nproc-1)
# Execute function "func_to_be_parallelized" on multiple CPUs; pass two variables to function
my_fast_results = Rmpi::mpi.parLapply(var1_passed_to_func,
                                      func_to_be_parallelized,
                                      var2_passed_to_func)
# Close slaves
Rmpi::mpi.close.Rslaves(dellog=T)

答案 1 :(得分:0)

您可以尝试使用插入符号和doParallel软件包。这就是我一直在使用的。它适用于某些模型类型,但可能不适用于所有模型类型。

  layer1 = c(6,12,18,24,30)
  layer2 = c(6,12,18,24,30)
  layer3 = c(6,12,18,24,30)

  cv.folds = 5

  # In order to make models fully reproducible when using parallel processing, we need to pass seeds as a parameter
  # https://stackoverflow.com/questions/13403427/fully-reproducible-parallel-models-using-caret

  total.param.permutations = length(layer1) * length(layer2) * length(layer3)

  seeds <- vector(mode = "list", length = cv.folds + 1)
  set.seed(1)  
  for(i in 1:cv.folds) seeds[[i]]<- sample.int(n=1, total.param.permutations, replace = TRUE)
  seeds[[cv.folds + 1]]<-sample.int(1, 1, replace = TRUE) #for the last model

  nn.grid <- expand.grid(layer1 = layer1, layer2 = layer2, layer3 = layer3)

  cl <- makeCluster(detectCores()*0.5) # use 50% of cores only, leave rest for other tasks
  registerDoParallel(cl)

  train_control <- caret::trainControl(method = "cv" 
                                       ,number=cv.folds 
                                       ,seeds = seeds # user defined seeds for parallel processing
                                       ,verboseIter = TRUE
                                       ,allowParallel = TRUE
                                       )

  stopCluster(cl)
  registerDoSEQ()

  tic("Total Time to NN Training: ")
  set.seed(1)
  model.nn.caret = caret::train(form = formula,
                       data = scaled.train.data,
                       method = 'neuralnet',
                       tuneGrid = nn.grid,
                       trControl = train_control
                       )
 toc()