-
Notifications
You must be signed in to change notification settings - Fork 5
/
extreme_learning_machines.py
629 lines (492 loc) · 23.8 KB
/
extreme_learning_machines.py
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
"""Extreme Learning Machines
"""
# Author: Issam H. Laradji <issam.laradji@gmail.com>
# Licence: BSD 3 clause
from abc import ABCMeta, abstractmethod
import numpy as np
from scipy import linalg
from sklearn.base import BaseEstimator, ClassifierMixin, RegressorMixin
from base import logistic, softmax, ACTIVATIONS
from sklearn.externals import six
from sklearn.preprocessing import LabelBinarizer
from sklearn.metrics import mean_squared_error
from sklearn.linear_model.ridge import ridge_regression
from sklearn.utils import gen_batches, check_random_state
from sklearn.utils import check_array, check_X_y, column_or_1d
from sklearn.utils.extmath import safe_sparse_dot
from class_weight import compute_sample_weight
def _multiply_weights(X, sample_weight):
"""Return W*X if sample_weight is not None."""
if sample_weight is None:
return X
else:
return X * sample_weight[:, np.newaxis]
class BaseELM(six.with_metaclass(ABCMeta, BaseEstimator)):
"""Base class for ELM classification and regression.
Warning: This class should not be used directly.
Use derived classes instead.
"""
@abstractmethod
def __init__(self, n_hidden, activation, C, class_weight,
weight_scale, batch_size, verbose, warm_start,
random_state):
self.C = C
self.activation = activation
self.class_weight = class_weight
self.weight_scale = weight_scale
self.batch_size = batch_size
self.n_hidden = n_hidden
self.verbose = verbose
self.warm_start = warm_start
self.random_state = random_state
def _init_weights(self, n_features):
"""Initialize the parameter weights."""
rng = check_random_state(self.random_state)
# Use the initialization method recommended by Glorot et al.
weight_init_bound = np.sqrt(6. / (n_features + self.n_hidden))
self.coef_hidden_ = rng.uniform(-weight_init_bound,
weight_init_bound, (n_features,
self.n_hidden))
self.intercept_hidden_ = rng.uniform(-weight_init_bound,
weight_init_bound,
self.n_hidden)
if self.weight_scale != 1:
self.coef_hidden_ *= self.weight_scale
self.intercept_hidden_ *= self.weight_scale
def _compute_hidden_activations(self, X):
"""Compute the hidden activations."""
hidden_activations = safe_sparse_dot(X, self.coef_hidden_)
hidden_activations += self.intercept_hidden_
# Apply the activation method
activation = ACTIVATIONS[self.activation]
hidden_activations = activation(hidden_activations)
return hidden_activations
def _fit(self, X, y, sample_weight=None, incremental=False):
"""Fit the model to the data X and target y."""
# Validate input params
if self.n_hidden <= 0:
raise ValueError("n_hidden must be > 0, got %s." % self.n_hidden)
if self.C <= 0.0:
raise ValueError("C must be > 0, got %s." % self.C)
if self.activation not in ACTIVATIONS:
raise ValueError("The activation %s is not supported. Supported "
"activation are %s." % (self.activation,
ACTIVATIONS))
# Initialize public attributes
if not hasattr(self, 'classes_'):
self.classes_ = None
if not hasattr(self, 'coef_hidden_'):
self.coef_hidden_ = None
# Initialize private attributes
if not hasattr(self, '_HT_H_accumulated'):
self._HT_H_accumulated = None
X, y = check_X_y(X, y, accept_sparse=['csr', 'csc', 'coo'],
dtype=np.float64, order="C", multi_output=True)
# This outputs a warning when a 1d array is expected
if y.ndim == 2 and y.shape[1] == 1:
y = column_or_1d(y, warn=True)
# Classification
if isinstance(self, ClassifierMixin):
self.label_binarizer_.fit(y)
if self.classes_ is None or not incremental:
self.classes_ = self.label_binarizer_.classes_
if sample_weight is None:
sample_weight = compute_sample_weight(self.class_weight,
self.classes_, y)
else:
classes = self.label_binarizer_.classes_
if not np.all(np.in1d(classes, self.classes_)):
raise ValueError("`y` has classes not in `self.classes_`."
" `self.classes_` has %s. 'y' has %s." %
(self.classes_, classes))
y = self.label_binarizer_.transform(y)
# Ensure y is 2D
if y.ndim == 1:
y = np.reshape(y, (-1, 1))
n_samples, n_features = X.shape
self.n_outputs_ = y.shape[1]
# Step (1/2): Compute the hidden layer coefficients
if (self.coef_hidden_ is None or (not incremental and
not self.warm_start)):
# Randomize and scale the input-to-hidden coefficients
self._init_weights(n_features)
# Step (2/2): Compute hidden-to-output coefficients
if self.batch_size is None:
# Run the least-square algorithm on the whole dataset
batch_size = n_samples
else:
# Run the recursive least-square algorithm on mini-batches
batch_size = self.batch_size
batches = gen_batches(n_samples, batch_size)
# (First time call) Run the least-square algorithm on batch 0
if not incremental or self._HT_H_accumulated is None:
batch_slice = next(batches)
H_batch = self._compute_hidden_activations(X[batch_slice])
# Get sample weights for the batch
if sample_weight is None:
sw = None
else:
sw = sample_weight[batch_slice]
# beta_{0} = inv(H_{0}^T H_{0} + (1. / C) * I) * H_{0}.T y_{0}
self.coef_output_ = ridge_regression(H_batch, y[batch_slice],
1. / self.C,
sample_weight=sw).T
# Initialize K if this is batch based or partial_fit
if self.batch_size is not None or incremental:
# K_{0} = H_{0}^T * W * H_{0}
weighted_H_batch = _multiply_weights(H_batch, sw)
self._HT_H_accumulated = safe_sparse_dot(H_batch.T,
weighted_H_batch)
if self.verbose:
y_scores = self._decision_scores(X[batch_slice])
if self.batch_size is None:
verbose_string = "Training mean squared error ="
else:
verbose_string = "Batch 0, Training mean squared error ="
print("%s %f" % (verbose_string,
mean_squared_error(y[batch_slice], y_scores,
sample_weight=sw)))
# Run the least-square algorithm on batch 1, 2, ..., n
for batch, batch_slice in enumerate(batches):
# Compute hidden activations H_{i} for batch i
H_batch = self._compute_hidden_activations(X[batch_slice])
# Get sample weights (sw) for the batch
if sample_weight is None:
sw = None
else:
sw = sample_weight[batch_slice]
weighted_H_batch = _multiply_weights(H_batch, sw)
# Update K_{i+1} by H_{i}^T * W * H_{i}
self._HT_H_accumulated += safe_sparse_dot(H_batch.T,
weighted_H_batch)
# Update beta_{i+1} by
# K_{i+1}^{-1} * H_{i+1}^T * W * (y_{i+1} - H_{i+1} * beta_{i})
y_batch = y[batch_slice] - safe_sparse_dot(H_batch,
self.coef_output_)
weighted_y_batch = _multiply_weights(y_batch, sw)
Hy_batch = safe_sparse_dot(H_batch.T, weighted_y_batch)
# Update hidden-to-output coefficients
regularized_HT_H = self._HT_H_accumulated.copy()
regularized_HT_H.flat[::self.n_hidden + 1] += 1. / self.C
# It is safe to use linalg.solve (instead of linalg.lstsq
# which is slow) since it is highly unlikely that
# regularized_HT_H is singular due to the random
# projection of the first layer and 'C' regularization being
# not dangerously large.
self.coef_output_ += linalg.solve(regularized_HT_H, Hy_batch,
sym_pos=True, overwrite_a=True,
overwrite_b=True)
if self.verbose:
y_scores = self._decision_scores(X[batch_slice])
print("Batch %d, Training mean squared error = %f" %
(batch + 1, mean_squared_error(y[batch_slice], y_scores,
sample_weight=sw)))
return self
def fit(self, X, y, sample_weight=None):
"""Fit the model to the data X and target y.
Parameters
----------
X : {array-like, sparse matrix}, shape (n_samples, n_features)
The input data.
y : array-like, shape (n_samples,)
Target values.
sample_weight : array-like, shape (n_samples,)
Per-sample weights. Rescale C per sample. Higher weights
force the classifier to put more emphasis on these points.
Returns
-------
self : returns a trained ELM usable for prediction.
"""
return self._fit(X, y, sample_weight=sample_weight, incremental=False)
def partial_fit(self, X, y, sample_weight=None):
"""Fit the model to the data X and target y.
Parameters
----------
X : {array-like, sparse matrix}, shape (n_samples, n_features)
Subset of training data.
y : array-like, shape (n_samples,)
Subset of target values.
sample_weight : array-like, shape (n_samples,)
Per-sample weights. Rescale C per sample. Higher weights
force the classifier to put more emphasis on these points.
Returns
-------
self : returns a trained ELM usable for prediction.
"""
self._fit(X, y, sample_weight=sample_weight, incremental=True)
return self
def _decision_scores(self, X):
"""Predict using the ELM model
Parameters
----------
X : {array-like, sparse matrix}, shape (n_samples, n_features)
The input data.
Returns
-------
y_pred : array-like, shape (n_samples,) or (n_samples, n_outputs)
The predicted values.
"""
X = check_array(X, accept_sparse=['csr', 'csc', 'coo'])
if self.batch_size is None:
hidden_activations = self._compute_hidden_activations(X)
y_pred = safe_sparse_dot(hidden_activations, self.coef_output_)
else:
n_samples = X.shape[0]
batches = gen_batches(n_samples, self.batch_size)
y_pred = np.zeros((n_samples, self.n_outputs_))
for batch in batches:
h_batch = self._compute_hidden_activations(X[batch])
y_pred[batch] = safe_sparse_dot(h_batch, self.coef_output_)
return y_pred
class ELMClassifier(BaseELM, ClassifierMixin):
"""Extreme learning machine classifier.
The algorithm trains a single-hidden layer feedforward network by computing
the hidden layer values using randomized parameters, then solving
for the output weights using least-square solutions. For prediction,
after computing the forward pass, the continuous output values pass
through a gate function converting them to integers that represent classes.
This implementation works with data represented as dense and sparse numpy
arrays of floating point values for the features.
Parameters
----------
C : float, optional, default 100
A regularization term that controls the linearity of the decision
function. Smaller value of C makes the decision boundary more linear.
class_weight : {dict, 'auto', None}, default None
If 'auto', class weights will be given inversely proportional
to the frequency of the class in the data.
If a dictionary is given, keys are the class labels and the
corresponding values are the class weights.
If None is given, then no class weights will be applied.
weight_scale : float, default 1.
Initializes and scales the input-to-hidden weights.
The weight values will range between plus and minus
'sqrt(weight_scale * 6. / (n_features + n_hidden))' based on the
uniform distribution.
n_hidden : int, default 100
The number of units in the hidden layer.
activation : {'logistic', 'tanh', 'relu'}, default 'relu'
Activation function for the hidden layer.
- 'logistic', the logistic sigmoid function,
returns f(x) = 1 / (1 + exp(x)).
- 'tanh', the hyperbolic tan function,
returns f(x) = tanh(x).
- 'relu', the rectified linear unit function,
returns f(x) = max(0, x).
batch_size : int, optional, default None
If None is given, batch_size is set as the number of samples.
Otherwise, it will be set as the given integer.
verbose : bool, optional, default False
Whether to print the training score.
warm_start : bool, optional, default False
When set to True, reuse the solution of the previous
call to fit as initialization, otherwise, just erase the
previous solution.
random_state : int or RandomState, optional, default None
State of or seed for random number generator.
Attributes
----------
`classes_` : array-list, shape (n_classes,)
Class labels for each output.
`n_outputs_` : int
Number of output neurons.
`coef_hidden_` : array-like, shape (n_features, n_hidden)
The input-to-hidden weights.
`intercept_hidden_` : array-like, shape (n_hidden,)
The bias added to the hidden layer neurons.
`coef_output_` : array-like, shape (n_hidden, n_outputs_)
The hidden-to-output weights.
`label_binarizer_` : LabelBinarizer
A LabelBinarizer object trained on the training set.
References
----------
Liang, Nan-Ying, et al.
"A fast and accurate online sequential learning algorithm for
feedforward networks." Neural Networks, IEEE Transactions on
17.6 (2006): 1411-1423.
http://www.ntu.edu.sg/home/egbhuang/pdf/OS-ELM-TNN.pdf
Zong, Weiwei, Guang-Bin Huang, and Yiqiang Chen.
"Weighted extreme learning machine for imbalance learning."
Neurocomputing 101 (2013): 229-242.
Glorot, Xavier, and Yoshua Bengio. "Understanding the difficulty of
training deep feedforward neural networks." International Conference
on Artificial Intelligence and Statistics. 2010.
"""
def __init__(self, n_hidden=100, activation='relu', C=1,
class_weight=None, weight_scale=1.0, batch_size=None,
verbose=False, warm_start=False, random_state=None):
super(ELMClassifier, self).__init__(n_hidden=n_hidden,
activation=activation,
C=C, class_weight=class_weight,
weight_scale=weight_scale,
batch_size=batch_size,
verbose=verbose,
warm_start=warm_start,
random_state=random_state)
self.label_binarizer_ = LabelBinarizer(-1, 1)
def partial_fit(self, X, y, classes=None, sample_weight=None):
"""Fit the model to the data X and target y.
Parameters
----------
X : {array-like, sparse matrix}, shape (n_samples, n_features)
The input data.
y : array-like, shape (n_samples,)
Subset of the target values.
classes : array-like, shape (n_classes,)
List of all the classes that can possibly appear in the y vector.
Must be provided at the first call to partial_fit, can be omitted
in subsequent calls.
sample_weight : array-like, shape (n_samples,)
Per-sample weights. Rescale C per sample. Higher weights
force the classifier to put more emphasis on these points.
Returns
-------
self : returns a trained elm usable for prediction.
"""
self.classes_ = classes
super(ELMClassifier, self).partial_fit(X, y, sample_weight)
return self
def decision_function(self, X):
"""Decision function of the elm model
Parameters
----------
X : {array-like, sparse matrix}, shape (n_samples, n_features)
The input data.
Returns
-------
y : array-like, shape (n_samples,) or (n_samples, n_classes)
The predicted values.
"""
y_scores = self._decision_scores(X)
if self.n_outputs_ == 1:
return y_scores.ravel()
else:
return y_scores
def predict(self, X):
"""Predict using the ELM model
Parameters
----------
X : {array-like, sparse matrix}, shape (n_samples, n_features)
The input data.
Returns
-------
y : array-like, shape (n_samples,) or (n_samples, n_classes)
The predicted classes, or the predicted values.
"""
y_scores = self._decision_scores(X)
return self.label_binarizer_.inverse_transform(y_scores)
def predict_proba(self, X):
"""Probability estimates.
Warning: the estimates aren't callibrated since the model optimizes a
penalized least squares objective function based on the One Vs Rest
binary encoding of the class membership.
Parameters
----------
X : {array-like, sparse matrix}, shape (n_samples, n_features)
The input data.
Returns
-------
y_prob : array-like, shape (n_samples, n_classes)
The predicted probability of the sample for each class in the
model, where classes are ordered as they are in
`self.classes_`.
"""
y_scores = self._decision_scores(X)
if len(self.classes_) == 2:
y_scores = logistic(y_scores)
return np.hstack([1 - y_scores, y_scores])
else:
return softmax(y_scores)
class ELMRegressor(BaseELM, RegressorMixin):
"""Extreme learning machine regressor.
The algorithm trains a single-hidden layer feedforward network by computing
the hidden layer values using randomized parameters, then solving
for the output weights using least-square solutions. For prediction,
ELMRegressor computes the forward pass resulting in continuous output
values.
This implementation works with data represented as dense and sparse numpy
arrays of floating point values for the features.
Parameters
----------
C : float, optional, default 100
A regularization term that controls the linearity of the decision
function. Smaller value of C makes the decision boundary more linear.
weight_scale : float, default 1.
Initializes and scales the input-to-hidden weights.
The weight values will range between plus and minus
'sqrt(weight_scale * 6. / (n_features + n_hidden))' based on the
uniform distribution.
n_hidden : int, default 100
The number of units in the hidden layer.
activation : {'logistic', 'tanh', 'relu'}, default 'relu'
Activation function for the hidden layer.
- 'logistic', the logistic sigmoid function,
returns f(x) = 1 / (1 + exp(x)).
- 'tanh', the hyperbolic tan function,
returns f(x) = tanh(x).
- 'relu', the rectified linear unit function,
returns f(x) = max(0, x).
batch_size : int, optional, default None
If None is given, batch_size is set as the number of samples.
Otherwise, it will be set as the given integer.
verbose : bool, optional, default False
Whether to print the training score.
warm_start : bool, optional, default False
When set to True, reuse the solution of the previous
call to fit as initialization, otherwise, just erase the
previous solution.
random_state : int or RandomState, optional, default None
State of or seed for random number generator.
Attributes
----------
`classes_` : array-list, shape (n_classes,)
Class labels for each output.
`n_outputs_` : int
Number of output neurons.
`coef_hidden_` : array-like, shape (n_features, n_hidden)
The input-to-hidden weights.
`intercept_hidden_` : array-like, shape (n_hidden,)
The bias added to the hidden layer neurons.
`coef_output_` : array-like, shape (n_hidden, n_outputs_)
The hidden-to-output weights.
References
----------
Liang, Nan-Ying, et al.
"A fast and accurate online sequential learning algorithm for
feedforward networks." Neural Networks, IEEE Transactions on
17.6 (2006): 1411-1423.
http://www.ntu.edu.sg/home/egbhuang/pdf/OS-ELM-TNN.pdf
Zong, Weiwei, Guang-Bin Huang, and Yiqiang Chen.
"Weighted extreme learning machine for imbalance learning."
Neurocomputing 101 (2013): 229-242.
Glorot, Xavier, and Yoshua Bengio. "Understanding the difficulty of
training deep feedforward neural networks." International Conference
on Artificial Intelligence and Statistics. 2010.
"""
def __init__(self, n_hidden=100, activation='relu', weight_scale=1.0,
batch_size=None, C=1, verbose=False, warm_start=False,
random_state=None):
super(ELMRegressor, self).__init__(n_hidden=n_hidden,
activation=activation,
C=C, class_weight=None,
weight_scale=weight_scale,
batch_size=batch_size,
verbose=verbose,
warm_start=warm_start,
random_state=random_state)
def predict(self, X):
"""Predict using the ELM model.
Parameters
----------
X : {array-like, sparse matrix}, shape (n_samples, n_features)
The input data.
Returns
-------
y : array-like, shape (n_samples,) or (n_samples, n_outputs)
The predicted values.
"""
y_pred = self._decision_scores(X)
if self.n_outputs_ == 1:
return y_pred.ravel()
else:
return y_pred