-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParalell_Computing.R
53 lines (46 loc) · 1.94 KB
/
Paralell_Computing.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#####################################################################################
##
## Parallel computing for cross vallidation(Substitude of For Loop)
## Features:
## 1. for both loop and nested loop
## 2. including the processing bar(better visualizatoin of the computing prcess)
##
#####################################################################################
library(doSNOW)
library(foreach)
##########
## Initializing parallel for loop parameters
##########
kFold = 5 # Total number of folds
trFold = 3 # Number of training folds
cv_times = ncol(combn(kFold ,trFold)) # Total number of cross validation
lam = seq(0,5, length.out = 100) # tuning parameter sequence(please custmerized it)
Param = expand.grid(fold_idx=1:cv_times, lambda=lam) # Gathering of parameters
## Initalize Parallel backend
core <- parallel:::detectCores()
cl <- makeCluster((core - 1)) # Don't use all of the cores, more efficeint
## Initialization process bar in computing process
registerDoSNOW(cl)
pb <- txtProgressBar(max = length(lam), style = 3)# max: the number of threads during the computing
progress <- function(n) setTxtProgressBar(pb, n)
opts <- list(progress = progress)
## parallel loop
loop <- foreach(i=1:nrow(Param), .combine=rbind, .packages = 'package/used/during/the/parallel/computing', .options.snow = opts) %dopar% {
# Data Analysis
## output and tabulate results
results = cbind(Lambda = Param$lambda[i],
validMSE = ,
trainMSE = )
}
# ## Nested Parallel Computing(comment out if necessary)
# loop <- foreach(i = 1:nrow('dataframe/firstDimension'), .combine = 'rbind') %:%
# foreach(j=1:ncol('dataframe/secondDimension'), .packages = c(), .combine='cbind') %dopar% {
# # Data Analysis
#
# ## output and tabulate results
# results = cbind(Lambda = Param$lambda[i],
# validMSE = ,
# trainMSE = )
# }
stopCluster(cl)
close(pb)