-
Notifications
You must be signed in to change notification settings - Fork 0
/
neural_network_keras.R
375 lines (251 loc) · 12.1 KB
/
neural_network_keras.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
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
##############################################
######### Fit neural network model with Keras
##############################################
source("./feature_preprocessing.R")
#############################
## import features ##########
#############################
# traning e test set
set.seed(100)
ll <- sample(c(1:nrow(dat_nn)), round(0.9*nrow(dat_nn)), replace = FALSE)
learn <- dat_nn[ll,]
test <- dat_nn[-ll,]
# design matrix (omit Area)
features <- c(14:ncol(dat_nn))
learn.XX <- as.matrix(learn[,features])
test.XX <- as.matrix(test[,features])
lambda.0 <- sum(learn$ClaimNb)/sum(learn$Exposure)
###################################
## network architectures ##########
###################################
# plot of gradient descent performance
plot.loss <- function(loss, title = T){
plot(loss$val_loss,col="blue", type = "l",
main=if(title == T) list("gradient descent loss") else "",
xlab="epoch", ylab="loss",
lty = 1, lwd = 2,
ylim=c(min(unlist(loss)), max(unlist(loss))))
lines(loss$loss,col="red", lty = 1, lwd = 2)
}
# shallow neural network
shallow.plain.vanilla <- function(seed, q0, y0, act_fun){
set.seed(seed)
design <- layer_input(shape = c(q0[1]), dtype = 'float32', name = 'design')
output = design %>%
layer_dense(units=q0[2], activation=act_fun, name='layer1') %>%
layer_dense(units=q0[3], activation='exponential', name='output',
weights=list(array(0, dim=c(q0[2],q0[3])), array(y0, dim=c(q0[3]))))
model <- keras_model(inputs = c(design), outputs = c(output))
model
}
# deep neural network
deep3.plain.vanilla <- function(seed, q0, y0){
set.seed(seed)
design <- layer_input(shape = c(q0[1]), dtype = 'float32', name = 'design')
output = design %>%
layer_dense(units=q0[2], activation='tanh', name='layer1') %>%
layer_dense(units=q0[3], activation='tanh', name='layer2') %>%
layer_dense(units=q0[4], activation='tanh', name='layer3') %>%
layer_dense(units=q0[5], activation='exponential', name='output',
weights=list(array(0, dim=c(q0[4],q0[5])), array(y0, dim=c(q0[5]))))
model <- keras_model(inputs = c(design), outputs = c(output))
model
}
# deep network with normalization & dropout
deep3.norm.dropout <- function(seed, q0, w0, y0){
set.seed(seed)
design <- layer_input(shape = c(q0[1]), dtype = 'float32', name = 'design')
output = design %>%
layer_dense(units=q0[2], activation='tanh', name='layer1') %>%
layer_batch_normalization() %>%
layer_dropout(rate = w0) %>%
layer_dense(units=q0[3], activation='tanh', name='layer2') %>%
layer_batch_normalization() %>%
layer_dropout(rate = w0) %>%
layer_dense(units=q0[4], activation='tanh', name='layer3') %>%
layer_dropout(rate = w0) %>%
layer_dense(units=q0[5], activation='exponential', name='output',
weights=list(array(0, dim=c(q0[4],q0[5])), array(y0, dim=c(q0[5]))))
model <- keras_model(inputs = c(design), outputs = c(output))
model
}
# deep network with regularization
deep3.ridge <- function(seed, q0, w0, y0){
set.seed(seed)
design <- layer_input(shape = c(q0[1]), dtype = 'float32', name = 'design')
output = design %>%
layer_dense(units=q0[2], kernel_regularizer=regularizer_l2(w0[1]), activation='tanh', name='layer1') %>%
layer_dense(units=q0[3], kernel_regularizer=regularizer_l2(w0[2]), activation='tanh', name='layer2') %>%
layer_dense(units=q0[4], kernel_regularizer=regularizer_l2(w0[3]), activation='tanh', name='layer3') %>%
layer_dense(units=q0[5], activation='exponential', name='output',
weights=list(array(0, dim=c(q0[4],q0[5])), array(y0, dim=c(q0[5]))))
model <- keras_model(inputs = c(design), outputs = c(output))
model
}
###################################
## shallow network ##########
###################################
q1 <- 20 # number of neurons
qqq <- c(length(features), c(q1), 1) # dimension of all layers including input and output
seed <- 100 # set seed
epoch0 <- 100
## choose optimizer and activation function ##########
ottimizzatori = c("sgd", "adagrad", "adadelta", "rmsprop",
"adam", "adamax", "nadam")
act_list = c("tanh", "sigmoid")
loss_fin = list()
for (j in 1:length(act_list)){
model <- shallow.plain.vanilla(seed, qqq, log(lambda.0), act_list[j])
losses = matrix(0, ncol = 2, nrow = length(ottimizzatori))
for(i in 1:length(ottimizzatori)){
model %>% compile(loss = 'poisson', optimizer = ottimizzatori[i])
fit <- model %>% fit(list(learn.XX), learn$ClaimNb, validation_split=0.1,
batch_size=10000, epochs=epoch0, verbose=0)
learn.y <- as.vector(model %>% predict(list(learn.XX)))
test.y <- as.vector(model %>% predict(list(test.XX)))
losses[i, ] = c(Poisson.loss(learn.y, learn$ClaimNb),
Poisson.loss(test.y, test$ClaimNb))
}
colnames(losses) = c("in sample", "out of sample")
rownames(losses) = ottimizzatori
loss_fin[[j]] = losses
}
names(loss_fin) = act_list
### choose batch size ###########
sequenza = c(5, 10, 50, 100, 200, 500, 1000)
losses = matrix(0, ncol = 2, nrow = length(sequenza))
fit = list()
for(i in 1:length(sequenza)){
bb = nrow(learn.XX)/sequenza[i]
model <- shallow.plain.vanilla(seed, qqq, log(lambda.0), "tanh")
model %>% compile(loss = 'poisson', optimizer = "nadam")
start_time <- Sys.time()
fit[[i]] <- model %>% fit(list(learn.XX), learn$ClaimNb, validation_split=0.1,
batch_size=bb, epochs=epoch0, verbose=0)
end_time <- Sys.time()
time_taken = end_time - start_time
learn.y <- as.vector(model %>% predict(list(learn.XX)))
test.y <- as.vector(model %>% predict(list(test.XX)))
losses[i, ] = c(Poisson.loss(learn.y, learn$ClaimNb),
Poisson.loss(test.y, test$ClaimNb))
}
## choose numbers of neurons ##########
neur = c(5, 10, 20, 30)
losses = matrix(0, ncol = 2, nrow = length(neur))
fit = list()
for(i in 1:length(neur)){
q1 <- neur[i]
qqq <- c(length(features), c(q1), 1)
model <- shallow.plain.vanilla(seed, qqq, log(lambda.0), "tanh")
model %>% compile(loss = 'poisson', optimizer = "nadam")
start_time <- Sys.time()
fit[[i]] <- model %>% fit(list(learn.XX), learn$ClaimNb, validation_split=0.1,
batch_size=600, epochs=epoch0, verbose=0)
end_time <- Sys.time()
time_taken = end_time - start_time
learn.y <- as.vector(model %>% predict(list(learn.XX)))
test.y <- as.vector(model %>% predict(list(test.XX)))
losses[i, ] = c(Poisson.loss(learn.y, learn$ClaimNb),
Poisson.loss(test.y, test$ClaimNb))
}
## choose number of epochs ##########
q1 <- 20
qqq <- c(length(features), c(q1), 1)
epoch_list = c(100, 200, 500, 1000)
losses = matrix(0, ncol = 2, nrow = length(epoch_list))
fit = list()
for(i in 1:length(epoch_list)){
epoch0 = epoch_list[i]
model <- shallow.plain.vanilla(seed, qqq, log(lambda.0), "tanh")
model %>% compile(loss = 'poisson', optimizer = "nadam")
start_time <- Sys.time()
fit[[i]] <- model %>% fit(list(learn.XX), learn$ClaimNb, validation_split=0.1,
batch_size=600, epochs=epoch0, verbose=0)
end_time <- Sys.time()
time_taken = end_time - start_time
learn.y <- as.vector(model %>% predict(list(learn.XX)))
test.y <- as.vector(model %>% predict(list(test.XX)))
losses[i, ] = c(Poisson.loss(learn.y, learn$ClaimNb),
Poisson.loss(test.y, test$ClaimNb))
}
###################################
## deep neural network ##########
###################################
q1 <- c(20,15,10) # number of neurons
qqq <- c(length(features), c(q1), 1) # dimension of all layers including input and output
seed <- 200 # set seed
## early stopping with callback ################
model <- deep3.plain.vanilla(seed, qqq, log(lambda.0))
model
path0 <- paste("./parametri/deep3_plain_vanilla", sep="")
CBs <- callback_model_checkpoint(path0, monitor = "val_loss",
verbose = 0, save_best_only = TRUE,
save_weights_only = FALSE)
model %>% compile(loss = 'poisson', optimizer = 'nadam')
epoch0 <- 1000
{t1 <- proc.time()
fit <- model %>% fit(list(learn.XX), learn$ClaimNb, validation_split=0.1,
batch_size=6000, epochs=epoch0, verbose=0, callbacks = CBs)
(proc.time()-t1)[3]}
learn.y <- as.vector(model %>% predict(list(learn.XX)))
test.y <- as.vector(model %>% predict(list(test.XX)))
losses = c(Poisson.loss(learn.y, learn$ClaimNb),Poisson.loss(test.y, test$ClaimNb))
# results on best validation model (callback)
new_model = load_model_hdf5(path0)
w1 <- get_weights(model)
learn.y <- as.vector(new_model %>% predict(list(learn.XX)))
test.y <- as.vector(new_model %>% predict(list(test.XX)))
# losses
losses = c(Poisson.loss(learn.y, learn$ClaimNb),Poisson.loss(test.y, test$ClaimNb))
## l2 regularization ############
q1 <- c(20,15,10) # number of neurons
(qqq <- c(length(features), c(q1), 1)) # dimension of all layers including input and output
seed <- 200 # set seed
# deep3 network with ridge regularizer
w0 <- rep(0.00001,3) # regularization parameter
model <- deep3.ridge(seed, qqq, w0, log(lambda.0))
model
path0 <- paste("./parametri/deep3_plain_vanilla", sep="")
CBs <- callback_model_checkpoint(path0, monitor = "val_loss",
verbose = 0, save_best_only = TRUE,
save_weights_only = FALSE)
model %>% compile(loss = 'poisson', optimizer = 'nadam')
epoch0 <- 1000
{t1 <- proc.time()
fit <- model %>% fit(list(learn.XX), learn$ClaimNb, validation_split=0.1,
batch_size=6000, epochs=epoch0, verbose=0, callbacks=CBs)
(proc.time()-t1)[3]}
learn.y <- as.vector(model %>% predict(list(learn.XX)))
test.y <- as.vector(model %>% predict(list(test.XX)))
losses = c(Poisson.loss(learn.y, learn$ClaimNb),Poisson.loss(test.y, test$ClaimNb))
# results on best validation model (callback)
new_model = load_model_hdf5(path0)
learn.y <- as.vector(new_model %>% predict(list(learn.XX)))
test.y <- as.vector(new_model %>% predict(list(test.XX)))
losses = c(Poisson.loss(learn.y, learn$ClaimNb),Poisson.loss(test.y, test$ClaimNb))
## dropout & normalization ############
q1 <- c(20,15,10) # number of neurons
(qqq <- c(length(features), c(q1), 1)) # dimension of all layers including input and output
seed <- 200 # set seed
epoch0 <- 500
# define deep3 network with normalization layers and dropouts
w0 <- c(0.01, 0.02, 0.05, 0.1) # dropout rate
losses_callback = list()
losses = list()
for (i in 1:length(w0)){
model <- deep3.norm.dropout(seed, qqq, 0.05, log(lambda.0))
path0 <- paste("./parametri/deep3_plain_vanilla", sep="")
CBs <- callback_model_checkpoint(path0, monitor = "val_loss", verbose = 0,
save_best_only = TRUE, save_weights_only = FALSE)
model %>% compile(loss = 'poisson', optimizer = 'nadam')
fit <- model %>% fit(list(learn.XX), learn$ClaimNb, validation_split=0.1,
batch_size=6000, epochs=epoch0, verbose=0, callbacks=CBs)
learn.y <- as.vector(model %>% predict(list(learn.XX)))
test.y <- as.vector(model %>% predict(list(test.XX)))
losses[[i]] = c(Poisson.loss(learn.y, learn$ClaimNb),Poisson.loss(test.y, test$ClaimNb))
# results on best validation model (callback)
new_model = load_model_hdf5(path0)
learn.y <- as.vector(new_model %>% predict(list(learn.XX)))
test.y <- as.vector(new_model %>% predict(list(test.XX)))
losses_callback[[i]] = c(Poisson.loss(learn.y, learn$ClaimNb),Poisson.loss(test.y, test$ClaimNb))
}