-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDL source code.R
More file actions
479 lines (396 loc) · 13.3 KB
/
Copy pathDL source code.R
File metadata and controls
479 lines (396 loc) · 13.3 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
##########################################################################
# Building a simple neural network
# import spreadsheet files
library(readr)
# deep learning package
library(keras)
# dynamic interactive tables
library(DT)
data <- read_csv("SimulatedBinaryClassificationDataset.csv",
col_names = TRUE)
summary(data)
# data.frame --> matrix
data <- as.matrix(data)
# remove the row and col names, leaving only numerical values
dimnames(data) = NULL
mode(data)
# train and test split index
set.seed(123)
index <- sample(2,
nrow(data),
replace = TRUE,
prob = c(0.9, 0.1))
table(index)
# data splitting
x_train <- data[index == 1, 1:10]
x_test <- data[index == 2, 1:10]
y_test_actual <- data[index == 2, 11]
# use teh to_categorical function in keras package for one-hot encoding
y_train <- to_categorical(data[index == 1, 11])
y_test <- to_categorical(data[index == 2, 11])
model <- keras_model_sequential()
model %>%
# layer_dense means a densely connected layer
layer_dense(name = "DeepLayer1",
units = 10, # hyperparameter: the number of nodes
activation = "relu",
# the first layer need to have specification about the input dimension
input_shape = c(10)) %>%
layer_dense(name = "DeepLayer2",
units = 10,
activation = "relu") %>%
layer_dense(name = "OutputLayer",
units = 2,
# softmax function will provide probabilities of the nodes
activation = "softmax")
summary(model)
model %>% compile(
# another way to calculate loss, besides mean-squared-error
loss = "categorical_crossentropy",
# a special way of gradient descent
optimizer = "adam",
# measurement of model performance - using accuracy to measure
metrics = c("accuracy"))
history <- model %>%
fit(x_train,
y_train,
# number of full forward & backward propagation
# (i.e. run 10 times back and forth of all samples)
epoch = 10,
# instead of propagating the whole dataset at one go, use smaller batches
batch_size = 256,
# splitting the training set to test itself during training
validation_split = 0.1,
verbose = 2)
# plot the training history
plot(history)
model %>%
evaluate(x_test,
# NOTE: here we are still using the one-hot encoded y_test
y_test)
# form predictions
pred <- model %>%
predict(x_test) %>%
k_argmax()
# reference for converting tensor to R data types
# https://torch.mlverse.org/technical/tensors/
pred <- as.array(pred)
table(Predicted = pred,
# NOTE: for confusion matrix, we are using the original y_test_actual, not encoded
Actual = y_test_actual)
##########################################################################
# Applying regularization to deal with overfitting
library(keras)
library(readr)
library(tidyr)
library(tibble)
library(plotly)
# specify the number of feature variables for the dataset to be downloaded
num_words <- 5000
imdb <- dataset_imdb(num_words = num_words)
# train test split
c(train_data, train_labels) %<-% imdb$train
c(test_data, test_labels) %<-% imdb$test
# training and test data are both stored as lists
length(train_data)
# multi-hot encoding
multi_hot_sequences <- function(sequences, dimension){
multi_hot <- matrix(0,
# the number of samples in the sequences
# sequences are stored as lists
nrow = length(sequences),
ncol = dimension)
for(i in 1 : length(sequences)){
# sequences[[i]] extracts the label of the words in the text sample i
# which ever word is included in that sequence will be assigned 1 at row i
multi_hot[i, sequences[[i]]] <- 1
}
multi_hot
}
train_data <- multi_hot_sequences(train_data, num_words)
test_data <- multi_hot_sequences(test_data, num_words)
baseline_model <-
keras_model_sequential() %>%
layer_dense(units = 16, activation = "relu", input_shape = num_words) %>%
layer_dense(units = 16, activation = "relu") %>%
layer_dense(units = 1, activation = "sigmoid")
baseline_model %>% compile(
optimizer = "adam",
loss = "binary_crossentropy",
metrics = list("accuracy")
)
baseline_model %>% summary()
baseline_history <- baseline_model %>% fit(
train_data,
train_labels,
epochs = 20,
# recall: batch size controls the number of training samples to work through before updating parameters
batch_size = 512,
validation_data = list(test_data, test_labels),
# get loss and accuracy reports
verbose = 2
)
plot(baseline_history)
l2_model <-
keras_model_sequential() %>%
layer_dense(units = 16, activation = "relu", input_shape = num_words,
# apply regularization in the layer_dense function's argument
kernel_regularizer = regularizer_l2(l = 0.001)) %>%
layer_dense(units = 16, activation = "relu",
kernel_regularizer = regularizer_l2(l = 0.001)) %>%
layer_dense(units = 1, activation = "sigmoid")
l2_model %>% compile(
optimizer = "adam",
loss = "binary_crossentropy",
metrics = list("accuracy")
)
l2_model %>% summary()
l2_history <- l2_model %>% fit(
train_data,
train_labels,
epoch = 20,
batch_size = 512,
validation_data = list(test_data, test_labels),
verbose = 2
)
plot(l2_history)
drop_model <- keras_model_sequential() %>%
layer_dense(units = 16, activation = "relu", input_shape = num_words) %>%
# a new layer to specify the dropout rate
layer_dropout(0.6) %>%
layer_dense(units = 16, activation = "relu") %>%
layer_dropout(0.6) %>%
layer_dense(units = 1, activation = "sigmoid")
drop_model %>% compile(
optimizer = "adam",
loss = "binary_crossentropy",
metrics = list("accuracy")
)
drop_model %>% summary()
drop_history <- drop_model %>% fit(
train_data,
train_labels,
epoch = 20,
batch_size = 512,
validation_data = list(test_data, test_labels),
verbose = 2
)
plot(drop_history)
##########################################################################
# Optimization of neural nets
library(keras)
library(readr)
train.import <- read_csv("ImprovementsTrain.csv")
test.import <- read_csv("ImprovementsTest.csv")
# recall that NN are constructed based on numerical matrices
# we need to cast dataframe into matrix and remove column names
train.import <- as.matrix(train.import)
dimnames(train.import) <- NULL
test.import <- as.matrix(test.import)
dimnames(test.import) <- NULL
# train & test sets
train_data <- train.import[, 1:12]
train_labels <- train.import[, 13]
test_data <- test.import[, 1:12]
test_labels <- test.import[, 13]
feature.means = vector(length = ncol(train_data))
for(i in 1:length(feature.means)){
# calculate the mean of each column in the training set
feature.means[i] = mean(train_data[, i])
}
feature.sds = vector(length = ncol(train_data))
for(i in 1:length(feature.sds)){
# calculate the standard deviation of each column in the training set
feature.sds[i] <- sd(train_data[, i])
}
# normalize the feature variables in the training set
train_data_normalized <- matrix(nrow = nrow(train_data),
ncol = ncol(train_data))
for(n in 1:ncol(train_data)){
for(m in 1:nrow(train_data)){
train_data_normalized[m, n] <- (train_data[m, n] - feature.means[n])/feature.sds[n]
}
}
# normalize the feature variables in the testing set
test_data_normalized <- matrix(nrow = nrow(test_data),
ncol = ncol(test_data))
for(n in 1:ncol(test_data)){
for(m in 1:nrow(test_data)){
test_data_normalized[m, n] <- (test_data[m, n] - feature.means[n])/feature.sds[n]
}
}
# use normal distribution to set the very first weights in tensor
init_w <- initializer_random_normal(mean = 0,
stddev = 0.05,
seed = 123)
# by default, all the bias terms are set to 0 initially
init_B <- initializer_zeros()
baseline_model <- keras_model_sequential() %>%
layer_dense(units = 48,
activation = "relu",
# the initial weights for the NN
kernel_initializer = init_w,
input_shape = c(12)) %>%
layer_dense(units = 48,
activation = "relu") %>%
layer_dense(units = 1,
activation = "sigmoid")
summary(baseline_model)
baseline_model %>% compile(
optimizer = optimizer_rmsprop(
# learning rate (i.e. step size)
lr = 0.001,
# the decay factor (i.e. the weight to the previous gradient, beta)
rho = 0.9
),
loss = "binary_crossentropy",
metrics = list("accuracy")
)
baseline_history <- baseline_model %>%
fit(train_data_normalized,
train_labels,
epochs = 40,
# conditions when to stop, save computation time
# avoid scenario of training without improvements
callbacks = list(callback_early_stopping(
# use change in loss to determine whether to stop
monitor = "loss",
# wait for two runs, if unchanged in loss, then stop
patience = 2
)),
batch_size = 512,
validation_data = list(test_data_normalized, test_labels),
verbose = 2)
plot(baseline_history)
##########################################################################
# Deep Neural Network for regression problem
library(readr)
library(keras)
setwd("~/Documents/Programming/R/Deep Learning with R/Datasets")
data.set <- read_csv("RegressionData.csv",
col_names = FALSE)
# transform dataframe to matrix
data.set <- as.matrix(data.set)
# remove column names
dimnames(data.set) <- NULL
# train test split
set.seed(123)
index <- sample(2,
nrow(data.set),
replace = TRUE,
prob = c(0.8, 0.2))
x_train <- data.set[index == 1, 1:10]
x_test <- data.set[index == 2, 1:10]
y_train <- data.set[index == 1, 11]
y_test <- data.set[index == 2, 11]
# normalizing data
mean.train <- apply(x_train, 2, mean)
sd.train <- apply(x_train, 2, sd)
x_train <- scale(x_train)
# use the normalizing parameters from training set to normalize testing set
x_test <- scale(x_test,
center = mean.train,
scale = sd.train)
# creating the model
model <- keras_model_sequential() %>%
layer_dense(units = 25,
activation = "relu",
input_shape = c(10)) %>%
layer_dropout(0.2) %>%
layer_dense(units = 25,
activation = "relu") %>%
layer_dropout(0.2) %>%
layer_dense(units = 25,
activation = "relu") %>%
layer_dropout(0.2) %>%
layer_dense(units = 1)
model %>% summary()
# compile the model
model %>% compile(
# the metric for propagation
loss = "mse",
optimizer = optimizer_rmsprop(),
# not for propagation, but for user feedback
# letting us know the model's performance
metrics = c("mean_absolute_error"))
# fitting the data
model_history <- model %>%
fit(x_train,
y_train,
epoch = 50,
batch_size = 32,
validation_split = 0.1,
callbacks = c(callback_early_stopping(monitor = "val_mean_absolute_error",
patience = 5)),
verbose = 2)
plot(model_history)
# testing the model
c(loss, mae) %<-% (model %>% evaluate(x_test, y_test, verbose = 0))
paste0("Mean Absolute Error on test set is:", mae)
##########################################################################
# Convolution Neural Network in R
library(keras)
# a dataset of numerous images of hand-written numbers from 0-9
mnist <- dataset_mnist()
x_train <- mnist$train$x
y_train <- mnist$train$y
x_test <- mnist$test$x
y_test <- mnist$test$y
dim(x_train)
img_row <- dim(x_train)[2]
img_col <- dim(x_test)[3]
# images should have three channels, but the data only has two, need transformation
# because this is gray-scale image, the third channel only has one layer
x_train <- array_reshape(x_train,
c(nrow(x_train),
img_row,
img_col, 1))
x_test <- array_reshape(x_test,
c(nrow(x_test),
img_row,
img_col, 1))
input_shape <- c(img_row, img_col, 1)
# there is one extra channel in x_train now
dim(x_train)
# normalize the datasets by dividing the number 255
# because the color gradient is from 0(black) to 255(white)
# dividing by 255 can transform the entries to values between 0 and 1
x_train <- x_train/255
x_test <- x_test/255
# use one-hot encoding to encode the y values
# y values are labels for number 0 to 9, thus we need 10 categories
y_train <- to_categorical(y_train, num_classes = 10)
y_test <- to_categorical(y_test, num_classes = 10)
model <- keras_model_sequential() %>%
layer_conv_2d(
# number of filters for transformation
filters = 16,
# size of the filters
kernel_size = c(3,3),
activation = 'relu',
input_shape = input_shape) %>%
layer_max_pooling_2d(pool_size = c(2, 2)) %>%
layer_dropout(rate = 0.25) %>%
layer_flatten() %>%
layer_dense(units = 10,
activation = 'relu') %>%
layer_dropout(rate = 0.5) %>%
layer_dense(units = 10,
# for categorical prediction
activation = 'softmax')
model %>% summary()
model %>% compile(
loss = loss_categorical_crossentropy,
optimizer = optimizer_adadelta(),
metrics = c('accuracy')
)
model %>% fit(
x_train,
y_train,
batch_size = 128,
epochs = 12,
validation_split = 0.2
)
score <- model %>% evaluate(x_test,
y_test)
score