-
Notifications
You must be signed in to change notification settings - Fork 0
/
holdout-eval-dnn.R
executable file
·174 lines (142 loc) · 5.16 KB
/
holdout-eval-dnn.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
#! /usr/bin/env Rscript
library(dplyr)
library(acceleep)
library(keras)
library(cliapp)
reticulate::use_condaenv(condaenv = "acceleep", required = TRUE)
# The first python-based action after session restart always fails:
reticulate:::ensure_python_initialized()
reticulate::dict(python = "says okay")
tick <- Sys.time()
# Declaring metadata ----
model_kind <- "DNN"
run_start <- format(tick, '%Y%m%d%H%M%S')
cliapp::cli_alert_info("Starting {model_kind} holdout eval on {run_start}")
metadata <- get_overview_table() %>%
distinct(model, placement) %>%
tidyr::expand_grid(outcome = c("kJ", "Jrel", "MET")) %>%
mutate(res = ifelse(model == "activpal", 20, 100))
# Make a progress bar
prog <- cli_progress_bar(
format = ":current of :total [:bar] (:percent, :elapsedfull)",
total = nrow(metadata)
)
# Initialize empty tibble to collect results
eval_result <- tibble::tibble()
# Big loop over accelerometers, placements, outcomes
for (row in seq_len(nrow(metadata))) {
prog$tick()
# hold current metadata
metaparams <- metadata[row, ]
cliapp::cli_alert_info("Starting {model_kind} on {metaparams$model} ({metaparams$placement}) / {metaparams$outcome}")
# browser()
# Collecting original training data only tpo get it's subject IDs
# resolution is small here because it doesn't matter, actual training data is read later
c(c(train_data, train_labels), c(test_data, test_labels)) %<-% keras_prep_regression(
model = metaparams$model, placement = metaparams$placement,
outcome = metaparams$outcome, random_seed = 19283, val_split = 1/3,
interval_length = 30, res = metaparams$res, normalize = TRUE
)
# This is easier for later
train_data$outcome <- train_labels
train_data <- train_data %>%
select(ID, interval, outcome, everything())
test_data$outcome <- test_labels
test_data <- test_data %>%
select(ID, interval, outcome, everything())
# Check
unique(train_data$ID)
unique(test_data$ID)
dim(train_data)
dim(test_data)
# Modelling ----
model_tick <- Sys.time()
strategy <- tensorflow::tf$distribute$MirroredStrategy(devices = NULL)
model_note <- "D1024-D512-BN-E30-ES_P10"
model_tick <- Sys.time()
with(strategy$scope(), {
model <- keras_model_sequential() %>%
# L1 --
layer_dense(
input_shape = 30,
name = "Dense1",
activation = "relu", units = 1024
) %>%
layer_batch_normalization() %>%
layer_dropout(rate = 0.2) %>%
# L2 --
layer_dense(
name = "Dense2",
activation = "relu", units = 512
) %>%
layer_batch_normalization() %>%
layer_dropout(rate = 0.2) %>%
# Output layer
layer_dense(units = 1, name = "output", activation = "linear")
})
model %>% compile(
loss = "mse",
optimizer = optimizer_adam(lr = 1e-3)
)
history <- model %>% fit(
as.matrix(train_data[-c(1:3)]), # Make sure to exclude ID, interval + outcome columns (1, 2, 3)
train_labels,
batch_size = 16,
epochs = 30,
validation_split = 0,
# Uncomment the following to monitor validation error during training w/ verbose = 1
validation_data =
list(
as.matrix(test_data[-c(1:3)]),
test_labels
),
verbose = 0,
callbacks =
list(
callback_early_stopping(
monitor = "val_loss",
min_delta = 0.1,
patience = 10,
mode = "min",
restore_best_weights = TRUE
)
)
)
# Evaluate, save results
# Make predictions
predicted_obs <- test_data %>%
select(ID, interval) %>%
# distinct() %>%
mutate(
outcome = test_labels,
predicted = as.numeric(predict(model, as.matrix(test_data[-c(1:3)])))
)
# prediction rmse
prediction_rmse <- predicted_obs %>%
summarize(rmse = sqrt(mean((predicted - outcome)^2))) %>%
pull(rmse)
model_tock <- Sys.time()
model_took <- hms::hms(seconds = round(as.numeric(difftime(model_tock, model_tick, units = "secs"))))
eval_result <- tibble::tibble(
rmse = prediction_rmse,
predicted_obs = list(predicted_obs),
model_note = model_note,
model_took = model_took,
epochs_completed = length(history$metrics$loss)
)
# Save per-device model
out_dir_models <- here::here("output", "holdout-validation", model_kind, run_start, "models")
if (!fs::dir_exists(out_dir_models)) fs::dir_create(out_dir_models)
filename_model <- glue::glue("holdout-eval-{model_kind}-{metaparams$model}-{metaparams$placement}-{metaparams$outcome}-{metaparams$res}-{run_start}.rds")
saveRDS(model, file = fs::path(out_dir_models, filename_model))
##}
# Save result tibble
filename <- glue::glue("holdout-eval-{model_kind}-{metaparams$model}-{metaparams$placement}-{metaparams$outcome}-{metaparams$res}-{run_start}.rds")
out_dir <- here::here("output", "holdout-validation", model_kind, run_start)
if (!fs::dir_exists(out_dir)) fs::dir_create(out_dir)
# Save RMSE results
saveRDS(object = eval_result, file = fs::path(out_dir, filename))
}
tock <- Sys.time()
took <- hms::hms(seconds = round(as.numeric(difftime(tock, tick, units = "secs"))))
pushoverr::pushover(glue::glue("{model_kind} holdout validation is done! Took {took}"), title = "Modelling Hell", priority = 1)