-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData - Performance Examples.Rmd
More file actions
402 lines (296 loc) · 9.7 KB
/
Copy pathData - Performance Examples.Rmd
File metadata and controls
402 lines (296 loc) · 9.7 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
---
title: "Data - Performance Examples"
author: "Pablo Morala"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
####################################
# 1 - Load all needed packages and functions
####################################
library(nn2poly)
library(nn2poly.tools)
library(keras)
library(tensorflow)
library(cowplot)
library(ggplot2)
library(patchwork)
IEEE_width_inches <- 3.5
my_width <- IEEE_width_inches
my_seed <- 1
set.seed(my_seed)
tensorflow::tf$random$set_seed(my_seed) # Needed to have reproducible results with keras
```
In this Rmd file we are going to show how to use NN2Poly in 2 different situations with synthetically generated data and store the results to plot them in `Figures - Performance examples.Rmd`.
# Data generation
Data will be held the same in the following examples.
```{r}
source("data_generation2.R")
source("plot_taylor_and_synaptic_potentials2.R")
```
Here we create the polynomial : $4x_1 - 3 x_2x_3$:
```{r}
# Define the polynomial to be used in the desired form:
original_polynomial <- list()
original_polynomial$labels <- list(c(1), c(2,3))
original_polynomial$values <- c(4,-3)
```
Then we generate the data:
```{r}
# Define number of variables p and sample n
p <- 3
n_sample <- 500
data <- data_generation2(n_sample, p, original_polynomial, error_var = 0.1)
head(data)
```
```{r}
# Data scaling
maxs <- apply(data, 2, max)
mins <- apply(data, 2, min)
data <- as.data.frame(scale(data, center = mins + (maxs - mins) / 2, scale = (maxs - mins) / 2))
# Divide in train (0.75) and test (0.25)
index <- sample(1:nrow(data), round(0.75 * nrow(data)))
train <- data[index, ]
test <- data[-index, ]
train_x <- as.matrix(subset(train, select = -c(Y)))
train_y <- as.matrix(train$Y)
test_x <- as.matrix(subset(test, select = -c(Y)))
test_y <- as.matrix(test$Y)
# Scale the data in the desired interval and separate train and test
scale_method <- "-1,1"
data_scaled <- scale_data(data, scale_method)
aux <- divide_train_test(data_scaled, train_proportion = 0.75)
train <- aux$train
test <- aux$test
plot(data_scaled)
```
# Keras and nn2poly hyperparameters
These parameters will also be held constant over the networks
```{r}
# keras hyperparameters
my_loss <- "mse"
my_metrics <- "mse"
my_epochs <- 3000
my_batch <- 50
my_validation_split <- 0.2
my_verbose <- 0
# NN structure
L <- 3
h_neurons_at_each_layer <- 100
af <- "tanh"
my_optimizer <- optimizer_adam()
# h neurons with 1 at the end for regression
h_neurons_vector <- rep(h_neurons_at_each_layer,L)
h_neurons_vector <- c(h_neurons_vector,1)
# list with the af at each layer:
af_string_list <- vector(mode = "list", length = L+1)
for (i in 1:L){
af_string_list[i] <- af
}
# Add linear at the end so we have regression setting.
af_string_list[L+1] <- "linear"
# nn2poly hyperparameter
forced_max_Q <- 3
# q taylor with 1 at the end for regression
q_taylor_at_each_layer <- 8 # We can set a high value as we are going
# to limit it with a forced max Q
q_taylor_vector <- rep(q_taylor_at_each_layer,L)
q_taylor_vector <- c(q_taylor_vector,1)
```
# Function to generate the desired graphs:
```{r}
perform_example_from_train_test<- function(train,
test,
af_string_list,
h_neurons_vector,
q_taylor_vector,
forced_max_Q,
my_max_norm,
my_optimizer,
my_loss,
my_metrics,
my_epochs,
my_batch,
my_validation_split,
my_verbose,
all_partitions) {
# Obtain parameters:
p <- ncol(train) - 1
# Divide again in x and y
train_x <- as.matrix(subset(train, select = -c(p+1)))
train_y <- as.matrix(subset(train, select = c(p+1)))
test_x <- as.matrix(subset(test, select = -c(p+1)))
test_y <- as.matrix(subset(test, select = c(p+1)))
# Build the nn
nn <- build_keras_model(
p,
af_string_list,
h_neurons_vector,
my_max_norm
)
# Compile the model
compile(nn,
loss = my_loss,
optimizer = my_optimizer,
metrics = my_metrics
)
# Fit the model
history <- fit(nn,
train_x,
train_y,
verbose = my_verbose,
epochs = my_epochs,
validation_split = my_validation_split,
batch_size = my_batch
)
plot_history <- plot(history)
# Obtain the predicted values with the NN to compare them
prediction_NN <- predict(nn, test_x)
# plot that comparison between NN and original
plot_NN_performance <- plot_NN_PR_comparison(unname(test_y), prediction_NN)
plot_NN_performance <- plot_NN_performance +
ggplot2::labs(x = "Original Y") +
theme_half_open()
# MSE between NN and Poly
n_test <- length(test_y)
MSE_NN_vs_original<- sum((prediction_NN - test_y)^2) / n_test
# Extract the weights:
keras_weights <- keras::get_weights(nn)
n <- length(keras_weights)
if(my_max_norm[[1]]=="no_constraint"){
n2 <- n/2
nn_weights <- vector(mode = "list", length = n2)
for (i in 1:n2){
nn_weights[[i]] <- rbind(keras_weights[[2*i]], keras_weights[[2*i-1]])
}
} else {
nn_weights <- keras_weights[1:(n - 2)]
nn_weights[[n - 1]] <- rbind(keras_weights[[n]], keras_weights[[n - 1]])
}
# use the nn2poly algorithm
all_layers_coeffs <- nn2poly::nn2poly_algorithm(
weights_list = nn_weights,
af_string_list = af_string_list,
q_taylor_vector = q_taylor_vector,
forced_max_Q = forced_max_Q
)
final_poly <- all_layers_coeffs[[length(all_layers_coeffs)]]
prediction_poly <- as.vector(eval_poly(x = test_x, poly = final_poly))
# MSE between NN and Poly
MSE_NN_vs_poly <- sum((prediction_NN - prediction_poly)^2) / n_test
# Plot the PR vs NN predictions
plot_PR_vs_NN <- plot_NN_PR_comparison(prediction_poly, prediction_NN) +
theme_half_open()
# Plot the taylor expansion at each layer:
plot_taylor <- plot_taylor_and_synpatic_potentials2(
data = train,
weights_list = nn_weights,
af_string_list = af_string_list,
q_taylor_vector = q_taylor_vector,
forced_max_Q = forced_max_Q,
my_max_norm
)
output <- vector(mode = "list", length = 0)
output$MSE_NN_vs_poly <- MSE_NN_vs_poly
output$MSE_NN_vs_original <- MSE_NN_vs_original
output$plot_NN_performance <- plot_NN_performance
output$plot_PR_vs_NN <- plot_PR_vs_NN
output$plot_taylor <- plot_taylor
output$plot_history <- plot_history
output$final_poly <- final_poly
output$train_x <- train_x
return(output)
}
```
# Example 1
Tanh AF, l1-norm constraint, 3 hidden layers, 50 neurons per layer.
```{r, warning=FALSE}
my_max_norm <- list("l1_norm", 1)
example1 <- perform_example_from_train_test(
train = train,
test = test,
af_string_list = af_string_list,
h_neurons_vector = h_neurons_vector,
q_taylor_vector = q_taylor_vector,
forced_max_Q = forced_max_Q,
my_max_norm = my_max_norm,
my_optimizer = my_optimizer,
my_loss = my_loss,
my_metrics = my_metrics,
my_epochs = my_epochs,
my_batch = my_batch,
my_validation_split = my_validation_split,
my_verbose
)
```
```{r}
example1
```
```{r}
# Remove history plot as it is not used in the Figures
example1$plot_history <- NULL
saveRDS(example1,"temporal/nn_performance_example_l1")
```
# Example 2
Same as before, no constraints
```{r, warning=FALSE}
my_max_norm <- list("no_constraint", 1)
example2 <- perform_example_from_train_test(
train = train,
test = test,
af_string_list = af_string_list,
h_neurons_vector = h_neurons_vector,
q_taylor_vector = q_taylor_vector,
forced_max_Q = forced_max_Q,
my_max_norm = my_max_norm,
my_optimizer = my_optimizer,
my_loss = my_loss,
my_metrics = my_metrics,
my_epochs = my_epochs,
my_batch = my_batch,
my_validation_split = my_validation_split,
my_verbose
)
```
```{r}
example2
```
```{r}
# Remove history plot as it is not used in the Figures
example2$plot_history <- NULL
saveRDS(example2,"temporal/nn_performance_example_nc")
```
# Repeating example 1, 10 diferent times to have average coefficients:
```{r}
my_max_norm <- list("l1_norm", 1)
my_epochs <- 3000
n_sims <- 10
simulations <- vector(mode="list", length = n_sims)
simulations_plot <- vector(mode="list", length = n_sims)
for (sims in 1:n_sims){
print(sims)
result <- perform_example_from_train_test(
train = train,
test = test,
af_string_list = af_string_list,
h_neurons_vector = h_neurons_vector,
q_taylor_vector = q_taylor_vector,
forced_max_Q = forced_max_Q,
my_max_norm = my_max_norm,
my_optimizer = my_optimizer,
my_loss = my_loss,
my_metrics = my_metrics,
my_epochs = my_epochs,
my_batch = my_batch,
my_validation_split = my_validation_split,
my_verbose
)
simulations[[sims]] <- result$final_poly
simulations_plot[[sims]] <- result$plot_PR_vs_NN
}
```
```{r}
# Remove history plot as it is not used in the Figures
simulations
saveRDS(simulations,"temporal/nn_performance_example_10_repetitions")
```