-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_code.r
192 lines (150 loc) · 4.04 KB
/
examples_code.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# Load packages
library(parallel)
library(foreach)
library(doParallel)
library(palmerpenguins)
library(ranger)
library(ggplot2)
library(dplyr)
library(future)
library(purrr)
library(furrr)
# Prepare data-----------------------------------
# Create a vector with 1 billion elements
data <- 1:1e9
# Make a list of 4 of these vectors
data_list <- list("1" = data,
"2" = data,
"3" = data,
"4" = data)
# Make an apply funtion parallel-------------------
# Calculate the "serial" mean
time_benchmark <- system.time(
lapply(data_list, mean)
)
time_benchmark
# Check how many cores are available
parallel::detectCores()
# Create a local cluster
cl <- parallel::makeCluster(8)
# Calculate the "parallel" mean
time_parallel <- system.time(
parallel::parLapply(cl,
data_list,
mean)
)
time_parallel
# Stop the cluster
parallel::stopCluster(cl)
# Running a loop in parallel-----------------------
# Activate the cluster again
cl <- parallel::makeCluster(4)
doParallel::registerDoParallel(cl)
# Calculate the "parallel" mean using foreach------
time_foreach <- system.time(
r <- foreach::foreach(i = 1:length(data_list),
.combine = rbind) %dopar% {
mean(data_list[[i]])
}
)
time_foreach
# Stop the cluster
parallel::stopCluster(cl)
# A real example: Random forest with ranger--------
# Prepare data
penguins <- as.data.frame(
na.omit(
penguins[, c("species",
"bill_length_mm",
"bill_depth_mm",
"flipper_length_mm",
"body_mass_g"
)]
)
)
# Fit a random forest model
m <- ranger::ranger(
data = penguins,
dependent.variable.name = "species",
importance = "permutation"
)
# Check results
m
m$variable.importance
# Hyperparameter optimization
# Create a data frame with all the possible combinations of hyperparameters
sensitivity.df <- expand.grid(
num.trees = c(500, 1000, 1500),
mtry = 2:4,
min.node.size = c(1, 10, 20)
)
# Create and register cluster
cl <- parallel::makeCluster(8)
doParallel::registerDoParallel(cl)
# Fit models in parallel
prediction.error <- foreach(
# Iterate over the rows of the hyperparameter data frame
num.trees = sensitiviy.df$num.trees,
mtry = sensitiviy.df$mtry,
min.node.size = sensitiviy.df$min.node.size,
.combine = "c",
.packages = "ranger"
) %dopar% {
# Fit the model
m.i <- ranger::ranger(
data = penguins,
dependent.variable.name = "species",
num.trees = num.trees,
mtry = mtry,
min.node.size = min.node.size
)
# Return the prediction error
return(m.i$prediction.error * 100)
}
# Add the prediction error to the data frame
sensitivity.df$prediction.error <- prediction.error
# Stop the cluster
parallel::stopCluster(cl)
# Plot the results
ggplot2::ggplot(data = sensitivity.df) +
ggplot2::aes(
x = mtry,
y = as.factor(min.node.size),
fill = prediction.error
) +
ggplot2::facet_wrap(as.factor(sensitivity.df$num.trees)) +
ggplot2::geom_tile() +
ggplot2::scale_y_discrete(breaks = c(1, 10, 20)) +
ggplot2::scale_fill_viridis_c() +
ggplot2::ylab("min.node.size")
# Find the combination with the lower prediction error
best.hyperparameters <- sensitivity.df %>%
dplyr::arrange(prediction.error) %>%
dplyr::slice(1)
# Parallel computing + the tidyverse: future and furrr------------
# Remember the first example?
# Serial mean
time_benchmark <- system.time(
lapply(data_list, mean)
)
time_benchmark
# For those of you that don't know how to use map
time_map <- system.time(
purrr::map(data_list, mean)
)
time_map
# Let's make map parallel
# Check number of cores
future::availableCores()
# Windows
plan(cluster, workers = 4)
# Or explicitly
cl <- parallel::makeCluster(4)
plan(cluster, workers = cl)
# Mac and Linux
plan(multicore, workers = 4)
# Calculate the mean in parallel
time_future <- system.time(
furrr::future_map(data_list, mean)
)
time_future