-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
extreme_learning_machine.Rmd
305 lines (159 loc) · 9.08 KB
/
extreme_learning_machine.Rmd
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
---
title: "Extreme Learning Machine"
author: "Lampros Mouselimis"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Extreme Learning Machine}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
As of 2018-06-17 the [elmNN](https://CRAN.R-project.org/package=elmNN) package was archived and due to the fact that it was one of the machine learning functions that I used when I started learning R (it returns the output results pretty fast too) plus that I had to utilize the package last week for a personal task I decided to reimplement the R code in Rcpp. It didn't take long because the R package was written, initially by the author, in a clear way. In the next lines I'll explain the differences and the functionality just for reference.
<br>
### Differences between the elmNN (R package) and the elmNNRcpp (Rcpp Package)
* The reimplementation assumes that both the predictors ( *x* ) and the response variable ( *y* ) are in the form of a matrix. This means that *character*, *factor* or *boolean* columns have to be transformed (onehot encoded would be an option) before using either the *elm_train* or the *elm_predict* function.
* The output predictions are in the form of a matrix. In case of regression the matrix has one column whereas in case of classification the number of columns equals the number of unique labels
* In case of classification the unique labels should begin from 0 and the difference between the unique labels should not be greater than 1. For instance, *unique_labels = c(0, 1, 2, 3)* are acceptable whereas the following case will raise an error : *unique_labels = c(0, 2, 3, 4)*
* I renamed the *poslin* activation to *relu* as it's easier to remember ( both share the same properties ). Moreover I added the *leaky_relu_alpha* parameter so that if the value is greater than 0.0 a leaky-relu-activation for the single-hidden-layer can be used.
* The initilization weights in the *elmNN* were set by default to uniform in the range [-1,1] *( 'uniform_negative' )* . I added two more options : *'normal_gaussian' ( in the range [0,1] )* and *'uniform_positive' ( in the range [0,1] )* too
* The user has the option to include or exclude *bias* of the one-layer feed-forward neural network
<br>
### The elmNNRcpp functions
The functions included in the *elmNNRcpp* package are the following and details for each parameter can be found in the package documentation,
<br>
| elmNNRcpp |
| :------------------: |
| **elm_train**(x, y, nhid, actfun, init_weights = "normal_gaussian", bias = FALSE, ...) |
| **elm_predict**(elm_train_object, newdata, normalize = FALSE) |
| **onehot_encode**(y) |
<br>
### elmNNRcpp in case of Regression
The following code chunk gives some details on how to use the *elm_train* in case of regression and compares the results with the *lm ( linear model )* base function,
<br>
```{r, eval=T}
# load the data and split it in two parts
#----------------------------------------
data(Boston, package = 'KernelKnn')
library(elmNNRcpp)
Boston = as.matrix(Boston)
dimnames(Boston) = NULL
X = Boston[, -dim(Boston)[2]]
xtr = X[1:350, ]
xte = X[351:nrow(X), ]
# prepare / convert the train-data-response to a one-column matrix
#-----------------------------------------------------------------
ytr = matrix(Boston[1:350, dim(Boston)[2]], nrow = length(Boston[1:350, dim(Boston)[2]]),
ncol = 1)
# perform a fit and predict [ elmNNRcpp ]
#----------------------------------------
fit_elm = elm_train(xtr, ytr, nhid = 1000, actfun = 'purelin',
init_weights = "uniform_negative", bias = TRUE, verbose = T)
pr_te_elm = elm_predict(fit_elm, xte)
# perform a fit and predict [ lm ]
#----------------------------------------
data(Boston, package = 'KernelKnn')
fit_lm = lm(medv~., data = Boston[1:350, ])
pr_te_lm = predict(fit_lm, newdata = Boston[351:nrow(X), ])
# evaluation metric
#------------------
rmse = function (y_true, y_pred) {
out = sqrt(mean((y_true - y_pred)^2))
out
}
# test data response variable
#----------------------------
yte = Boston[351:nrow(X), dim(Boston)[2]]
# mean-squared-error for 'elm' and 'lm'
#--------------------------------------
cat('the rmse error for extreme-learning-machine is :', rmse(yte, pr_te_elm[, 1]), '\n')
cat('the rmse error for liner-model is :', rmse(yte, pr_te_lm), '\n')
```
<br>
### elmNNRcpp in case of Classification
The following code script illustrates how *elm_train* can be used in classification and compares the results with the *glm ( Generalized Linear Models )* base function,
<br>
```{r, eval=T}
# load the data
#--------------
data(ionosphere, package = 'KernelKnn')
y_class = ionosphere[, ncol(ionosphere)]
x_class = ionosphere[, -c(2, ncol(ionosphere))] # second column has 1 unique value
x_class = scale(x_class[, -ncol(x_class)])
x_class = as.matrix(x_class) # convert to matrix
dimnames(x_class) = NULL
# split data in train-test
#-------------------------
xtr_class = x_class[1:200, ]
xte_class = x_class[201:nrow(ionosphere), ]
ytr_class = as.numeric(y_class[1:200])
yte_class = as.numeric(y_class[201:nrow(ionosphere)])
ytr_class = onehot_encode(ytr_class - 1) # class labels should begin from 0 (subtract 1)
# perform a fit and predict [ elmNNRcpp ]
#----------------------------------------
fit_elm_class = elm_train(xtr_class, ytr_class, nhid = 1000, actfun = 'relu',
init_weights = "uniform_negative", bias = TRUE, verbose = TRUE)
pr_elm_class = elm_predict(fit_elm_class, xte_class, normalize = FALSE)
pr_elm_class = max.col(pr_elm_class, ties.method = "random")
# perform a fit and predict [ glm ]
#----------------------------------------
data(ionosphere, package = 'KernelKnn')
fit_glm = glm(class~., data = ionosphere[1:200, -2], family = binomial(link = 'logit'))
pr_glm = predict(fit_glm, newdata = ionosphere[201:nrow(ionosphere), -2], type = 'response')
pr_glm = as.vector(ifelse(pr_glm < 0.5, 1, 2))
# accuracy for 'elm' and 'glm'
#-----------------------------
cat('the accuracy for extreme-learning-machine is :', mean(yte_class == pr_elm_class), '\n')
cat('the accuracy for glm is :', mean(yte_class == pr_glm), '\n')
```
<br>
### Classify MNIST digits using elmNNRcpp
I found an interesting [Python implementation / Code on the web](https://www.kaggle.com/robertbm/extreme-learning-machine-example) and I thought I give it a try to reproduce the results. I downloaded the MNIST data from my [Github repository](https://github.com/mlampros/DataSets) and I used the following parameter setting,
```{r, eval = F, echo = T}
# using system('wget..') on a linux OS
#-------------------------------------
system("wget https://raw.githubusercontent.com/mlampros/DataSets/master/mnist.zip")
mnist <- read.table(unz("mnist.zip", "mnist.csv"), nrows = 70000, header = T,
quote = "\"", sep = ",")
x = mnist[, -ncol(mnist)]
y = mnist[, ncol(mnist)]
# using system('wget..') on a linux OS
#-------------------------------------
system("wget https://raw.githubusercontent.com/mlampros/DataSets/master/mnist.zip")
mnist <- read.table(unz("mnist.zip", "mnist.csv"), nrows = 70000, header = T,
quote = "\"", sep = ",")
x = mnist[, -ncol(mnist)]
y = mnist[, ncol(mnist)] + 1
# use the hog-features as input data
#-----------------------------------
hog = OpenImageR::HOG_apply(x, cells = 6, orientations = 9, rows = 28, columns = 28, threads = 6)
y_expand = elmNNRcpp::onehot_encode(y - 1)
# 4-fold cross-validation
#------------------------
folds = KernelKnn:::class_folds(folds = 4, as.factor(y))
str(folds)
START = Sys.time()
fit = lapply(1:length(folds), function(x) {
cat('\n'); cat('fold', x, 'starts ....', '\n')
tmp_fit = elmNNRcpp::elm_train(as.matrix(hog[unlist(folds[-x]), ]), y_expand[unlist(folds[-x]), ],
nhid = 2500, actfun = 'relu', init_weights = 'uniform_negative',
bias = TRUE, verbose = TRUE)
cat('******************************************', '\n')
tmp_fit
})
END = Sys.time()
END - START
# Time difference of 5.698552 mins
str(fit)
# predictions for 4-fold cross validation
#----------------------------------------
test_acc = unlist(lapply(1:length(fit), function(x) {
pr_te = elmNNRcpp::elm_predict(fit[[x]], newdata = as.matrix(hog[folds[[x]], ]))
pr_max_col = max.col(pr_te, ties.method = "random")
y_true = max.col(y_expand[folds[[x]], ])
mean(pr_max_col == y_true)
}))
test_acc
# [1] 0.9825143 0.9848571 0.9824571 0.9822857
cat('Accuracy ( Mnist data ) :', round(mean(test_acc) * 100, 2), '\n')
# Accuracy ( Mnist data ) : 98.3
```