Search code examples
rkerasforeachdoparallel

Assign to common variable inside foreach and doParallel


I am using doParallel and foreach packages to train 400 small Keras models of a data set I have.

#1. Pre-make a blank data frame to be filled with values in the loop
Summary<-data.frame(ticker=character(), Today=numeric(), Tommorow=numeric(), MAPE=numeric(), stringsAsFactors=FALSE)

#2. Initialize cores to work
registerDoParallel(detectCores()-1)

#3. Implement for loop with parallel
foreach (i=1:400, .combine=c, .packages = c("keras", "tensorflow","dplyr","data.table")) %dopar% {
          ... code that filters data dynamically on [i] and fits the NN model...
          Summary[i,]<-c(tickers[i],
                         as.numeric(Predict[length(Predict)]),
                         model%>%predict(PredictorsT)%>%as.numeric(),
                         MAPE)
}

The parallel process runs but at the end of the loop the Summary data frame remains blank which menas that each iteration is not able to populate the data. How may I achive this?


Solution

  • Put your result in a return statement.

    Summary = foreach (i=1:400, .combine=c, .packages = c("keras", "tensorflow","dplyr","data.table")) %dopar% {
              ... code that filters data dynamically on [i] and fits the NN model...
              return(c(tickers[i],
                       as.numeric(Predict[length(Predict)]),
                       model%>%predict(PredictorsT)%>%as.numeric(),
                       MAPE))
    

    Your combine probably should be .combine=rbind