Skip to content
This repository was archived by the owner on Mar 6, 2021. It is now read-only.

Commit 997ea67

Browse files
committedMar 7, 2013
changed xfer_* to activation_*, modified docs and demo to match;
made internal regression use LinearRegression object
1 parent ed2209d commit 997ea67

File tree

4 files changed

+124
-98
lines changed

4 files changed

+124
-98
lines changed
 

‎elm.py

+47-36
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
ELMRegressor, SimpleELMRegressor, SimpleELMClassifier).
1010
1111
An Extreme Learning Machine (ELM) is a single layer feedforward
12-
network with a random hidden layer components and least-squares fitting
13-
of the hidden->output weights by default. [1][2]
12+
network with a random hidden layer components and ordinary linear
13+
least squares fitting of the hidden->output weights by default.
14+
[1][2]
1415
1516
References
1617
----------
@@ -23,12 +24,11 @@
2324
from abc import ABCMeta, abstractmethod
2425

2526
import numpy as np
26-
from scipy.linalg import pinv2
2727

2828
from sklearn.utils import as_float_array
29-
from sklearn.utils.extmath import safe_sparse_dot
3029
from sklearn.base import BaseEstimator, ClassifierMixin, RegressorMixin
3130
from sklearn.preprocessing import LabelBinarizer
31+
from sklearn.linear_model import LinearRegression
3232

3333
from random_hidden_layer import SimpleRandomHiddenLayer
3434

@@ -96,18 +96,19 @@ class ELMRegressor(BaseELM, RegressorMixin):
9696
ELMRegressor is a regressor based on the Extreme Learning Machine.
9797
9898
An Extreme Learning Machine (ELM) is a single layer feedforward
99-
network with a random hidden layer components and least-squares fitting
100-
of the hidden->output weights by default. [1][2]
99+
network with a random hidden layer components and ordinary linear
100+
least squares fitting of the hidden->output weights by default.
101+
[1][2]
101102
102103
Parameters
103104
----------
104105
`hidden_layer` : random_hidden_layer instance, optional
105106
(default=SimpleRandomHiddenLayer(random_state=0))
106107
107-
`regressor` : linear_model instance, optional (default=None)
108+
`regressor` : regressor instance, optional (default=None)
108109
If provided, this object is used to perform the regression from hidden
109110
unit activations to the outputs and subsequent predictions. If not
110-
present, a simple least squares fit is performed internally.
111+
present, an ordinary linear least squares fit is performed
111112
112113
Attributes
113114
----------
@@ -139,14 +140,19 @@ def __init__(self,
139140

140141
super(ELMRegressor, self).__init__(hidden_layer, regressor)
141142

142-
self.coefs_ = None
143143
self.fitted_ = False
144144
self.hidden_activations_ = None
145+
self._lin_reg = LinearRegression(copy_X=False,
146+
normalize=False,
147+
fit_intercept=False)
145148

146149
def _fit_regression(self, y):
147-
"""fit regression using internal least squares/supplied regressor"""
150+
"""
151+
fit regression using internal linear regression
152+
or supplied regressor
153+
"""
148154
if (self.regressor is None):
149-
self.coefs_ = safe_sparse_dot(pinv2(self.hidden_activations_), y)
155+
self._lin_reg.fit(self.hidden_activations_, y)
150156
else:
151157
self.regressor.fit(self.hidden_activations_, y)
152158

@@ -183,7 +189,7 @@ def fit(self, X, y):
183189
def _get_predictions(self, X):
184190
"""get predictions using internal least squares/supplied regressor"""
185191
if (self.regressor is None):
186-
preds = safe_sparse_dot(self.hidden_activations_, self.coefs_)
192+
preds = self._lin_reg.predict(self.hidden_activations_)
187193
else:
188194
preds = self.regressor.predict(self.hidden_activations_)
189195

@@ -219,18 +225,19 @@ class ELMClassifier(BaseELM, ClassifierMixin):
219225
ELMClassifier is a classifier based on the Extreme Learning Machine.
220226
221227
An Extreme Learning Machine (ELM) is a single layer feedforward
222-
network with a random hidden layer components and least-squares fitting
223-
of the hidden->output weights by default. [1][2]
228+
network with a random hidden layer components and ordinary linear
229+
least squares fitting of the hidden->output weights by default.
230+
[1][2]
224231
225232
Parameters
226233
----------
227234
`hidden_layer` : random_hidden_layer instance, optional
228235
(default=SimpleRandomHiddenLayer(random_state=0))
229236
230-
`regressor` : linear_model instance, optional (default=None)
237+
`regressor` : regressor instance, optional (default=None)
231238
If provided, this object is used to perform the regression from hidden
232239
unit activations to the outputs and subsequent predictions. If not
233-
present, a simple least squares fit is performed internally.
240+
present, an ordinary linear least squares fit is performed
234241
235242
Attributes
236243
----------
@@ -333,8 +340,9 @@ class SimpleELMRegressor(BaseEstimator, RegressorMixin):
333340
SimpleELMRegressor is a regressor based on the Extreme Learning Machine.
334341
335342
An Extreme Learning Machine (ELM) is a single layer feedforward
336-
network with a random hidden layer components and least-squares fitting
337-
of the hidden->output weights by default. [1][2]
343+
network with a random hidden layer components and ordinary linear
344+
least squares fitting of the hidden->output weights by default.
345+
[1][2]
338346
339347
SimpleELMRegressor is a wrapper for an ELMRegressor that uses a
340348
SimpleRandomHiddenLayer and passes the __init__ parameters through
@@ -345,14 +353,14 @@ class SimpleELMRegressor(BaseEstimator, RegressorMixin):
345353
`n_hidden` : int, optional (default=20)
346354
Number of units to generate in the SimpleRandomHiddenLayer
347355
348-
`xfer_func` : {callable, string} optional (default='tanh')
356+
`activation_func` : {callable, string} optional (default='tanh')
349357
Function used to transform input activation
350358
It must be one of 'tanh', 'sine', 'tribas', 'sigmoid', 'hardlim' or
351359
a callable. If none is given, 'tanh' will be used. If a callable
352360
is given, it will be used to compute the hidden unit activations.
353361
354-
`xfer_args` : dictionary, optional (default=None)
355-
Supplies keyword arguments for a callable xfer_func
362+
`activation_args` : dictionary, optional (default=None)
363+
Supplies keyword arguments for a callable activation_func
356364
357365
`random_state` : int, RandomState instance or None (default=None)
358366
Control the pseudo random number generator used to generate the
@@ -376,12 +384,13 @@ class SimpleELMRegressor(BaseEstimator, RegressorMixin):
376384
2006.
377385
"""
378386

379-
def __init__(self, n_hidden=20, xfer_func='tanh', xfer_args=None,
387+
def __init__(self, n_hidden=20,
388+
activation_func='tanh', activation_args=None,
380389
random_state=None):
381390

382391
self.n_hidden = n_hidden
383-
self.xfer_func = xfer_func
384-
self.xfer_args = xfer_args
392+
self.activation_func = activation_func
393+
self.activation_args = activation_args
385394
self.random_state = random_state
386395

387396
self.elm_regressor_ = None
@@ -407,8 +416,8 @@ def fit(self, X, y):
407416
Returns an instance of self.
408417
"""
409418
rhl = SimpleRandomHiddenLayer(n_hidden=self.n_hidden,
410-
xfer_func=self.xfer_func,
411-
xfer_args=self.xfer_args,
419+
activation_func=self.activation_func,
420+
activation_args=self.activation_args,
412421
random_state=self.random_state)
413422

414423
self.elm_regressor_ = ELMRegressor(hidden_layer=rhl)
@@ -440,8 +449,9 @@ class SimpleELMClassifier(BaseEstimator, ClassifierMixin):
440449
SimpleELMClassifier is a classifier based on the Extreme Learning Machine.
441450
442451
An Extreme Learning Machine (ELM) is a single layer feedforward
443-
network with a random hidden layer components and least-squares fitting
444-
of the hidden->output weights by default. [1][2]
452+
network with a random hidden layer components and ordinary linear
453+
least squares fitting of the hidden->output weights by default.
454+
[1][2]
445455
446456
SimpleELMClassifier is a wrapper for an ELMClassifier that uses a
447457
SimpleRandomHiddenLayer and passes the __init__ parameters through
@@ -452,14 +462,14 @@ class SimpleELMClassifier(BaseEstimator, ClassifierMixin):
452462
`n_hidden` : int, optional (default=20)
453463
Number of units to generate in the SimpleRandomHiddenLayer
454464
455-
`xfer_func` : {callable, string} optional (default='tanh')
465+
`activation_func` : {callable, string} optional (default='tanh')
456466
Function used to transform input activation
457467
It must be one of 'tanh', 'sine', 'tribas', 'sigmoid', 'hardlim' or
458468
a callable. If none is given, 'tanh' will be used. If a callable
459469
is given, it will be used to compute the hidden unit activations.
460470
461-
`xfer_args` : dictionary, optional (default=None)
462-
Supplies keyword arguments for a callable xfer_func
471+
`activation_args` : dictionary, optional (default=None)
472+
Supplies keyword arguments for a callable activation_func
463473
464474
`random_state` : int, RandomState instance or None (default=None)
465475
Control the pseudo random number generator used to generate the
@@ -486,12 +496,13 @@ class SimpleELMClassifier(BaseEstimator, ClassifierMixin):
486496
2006.
487497
"""
488498

489-
def __init__(self, n_hidden=20, xfer_func='tanh', xfer_args=None,
499+
def __init__(self, n_hidden=20,
500+
activation_func='tanh', activation_args=None,
490501
random_state=None):
491502

492503
self.n_hidden = n_hidden
493-
self.xfer_func = xfer_func
494-
self.xfer_args = xfer_args
504+
self.activation_func = activation_func
505+
self.activation_args = activation_args
495506
self.random_state = random_state
496507

497508
self.elm_classifier_ = None
@@ -538,8 +549,8 @@ def fit(self, X, y):
538549
Returns an instance of self.
539550
"""
540551
rhl = SimpleRandomHiddenLayer(n_hidden=self.n_hidden,
541-
xfer_func=self.xfer_func,
542-
xfer_args=self.xfer_args,
552+
activation_func=self.activation_func,
553+
activation_args=self.activation_args,
543554
random_state=self.random_state)
544555

545556
self.elm_classifier_ = ELMClassifier(hidden_layer=rhl)

‎elm_notebook.py

+19-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
# Demo python notebook for sklearn elm and random_hidden_layer modules
77
#
8-
# Author: David C. Lambert
8+
# Author: David C. Lambert [dcl -at- panix -dot- com]
9+
# Copyright(c) 2013
910
# License: Simple BSD
1011

1112
# <codecell>
@@ -78,36 +79,36 @@ def res_dist(x, y, e, n_runs=100, random_state=None):
7879
# <codecell>
7980

8081
# RBF tests
81-
elmc = ELMClassifier(RBFRandomHiddenLayer(xfer_func='gaussian'))
82+
elmc = ELMClassifier(RBFRandomHiddenLayer(activation_func='gaussian'))
8283
tr,ts = res_dist(irx, iry, elmc, n_runs=100, random_state=0)
8384

84-
elmc = ELMClassifier(RBFRandomHiddenLayer(xfer_func='poly_spline', gamma=2))
85+
elmc = ELMClassifier(RBFRandomHiddenLayer(activation_func='poly_spline', gamma=2))
8586
tr,ts = res_dist(irx, iry, elmc, n_runs=100, random_state=0)
8687

87-
elmc = ELMClassifier(RBFRandomHiddenLayer(xfer_func='multiquadric'))
88+
elmc = ELMClassifier(RBFRandomHiddenLayer(activation_func='multiquadric'))
8889
tr,ts = res_dist(irx, iry, elmc, n_runs=100, random_state=0)
8990

9091
# Simple tests
91-
elmc = ELMClassifier(SimpleRandomHiddenLayer(xfer_func='sine'))
92+
elmc = ELMClassifier(SimpleRandomHiddenLayer(activation_func='sine'))
9293
tr,ts = res_dist(irx, iry, elmc, n_runs=100, random_state=0)
9394

94-
elmc = ELMClassifier(SimpleRandomHiddenLayer(xfer_func='tanh'))
95+
elmc = ELMClassifier(SimpleRandomHiddenLayer(activation_func='tanh'))
9596
tr,ts = res_dist(irx, iry, elmc, n_runs=100, random_state=0)
9697

97-
elmc = ELMClassifier(SimpleRandomHiddenLayer(xfer_func='tribas'))
98+
elmc = ELMClassifier(SimpleRandomHiddenLayer(activation_func='tribas'))
9899
tr,ts = res_dist(irx, iry, elmc, n_runs=100, random_state=0)
99100

100-
elmc = ELMClassifier(SimpleRandomHiddenLayer(xfer_func='sigmoid'))
101+
elmc = ELMClassifier(SimpleRandomHiddenLayer(activation_func='sigmoid'))
101102
tr,ts = res_dist(irx, iry, elmc, n_runs=100, random_state=0)
102103

103-
elmc = ELMClassifier(SimpleRandomHiddenLayer(xfer_func='hardlim'))
104+
elmc = ELMClassifier(SimpleRandomHiddenLayer(activation_func='hardlim'))
104105
tr,ts = res_dist(irx, iry, elmc, n_runs=100, random_state=0)
105106

106107
# <codecell>
107108

108109
hardlim = (lambda a: np.array(a > 0.0, dtype=float))
109110
tribas = (lambda a: np.clip(1.0 - np.fabs(a), 0.0, 1.0))
110-
elmr = ELMRegressor(SimpleRandomHiddenLayer(random_state=0, xfer_func=tribas))
111+
elmr = ELMRegressor(SimpleRandomHiddenLayer(random_state=0, activation_func=tribas))
111112
elmr.fit(xtoy_train, ytoy_train)
112113
print elmr.score(xtoy_train, ytoy_train), elmr.score(xtoy_test, ytoy_test)
113114
plot(xtoy, ytoy, xtoy, elmr.predict(xtoy))
@@ -131,8 +132,8 @@ def res_dist(x, y, e, n_runs=100, random_state=None):
131132
nh = 10
132133
(ctrs, _, _) = k_means(xtoy_train, nh)
133134
unit_rs = np.ones(nh)
134-
rhl = RBFRandomHiddenLayer(n_hidden=nh, xfer_func='poly_spline', gamma=5)
135-
#rhl = RBFRandomHiddenLayer(n_hidden=nh, xfer_func='multiquadric', gamma=1)
135+
rhl = RBFRandomHiddenLayer(n_hidden=nh, activation_func='poly_spline', gamma=3)
136+
#rhl = RBFRandomHiddenLayer(n_hidden=nh, activation_func='multiquadric', gamma=1)
136137
#rhl = RBFRandomHiddenLayer(n_hidden=nh, centers=ctrs, radii=unit_rs, gamma=4)
137138
elmr = ELMRegressor(hidden_layer=rhl)
138139
elmr.fit(xtoy_train, ytoy_train)
@@ -150,7 +151,7 @@ def powtanh_xfer(activations, power=1.0):
150151
return pow(np.tanh(activations), power)
151152

152153
#tanh_rhl = SimpleRandomHiddenLayer(n_hidden=5000, random_state=0)
153-
tanh_rhl = SimpleRandomHiddenLayer(n_hidden=5000, xfer_func=powtanh_xfer, xfer_args={'power':2.0})
154+
tanh_rhl = SimpleRandomHiddenLayer(n_hidden=5000, activation_func=powtanh_xfer, activation_args={'power':2.0})
154155
elmc_tanh = ELMClassifier(hidden_layer=tanh_rhl)
155156
elmc_tanh.fit(dgx_train, dgy_train)
156157
print elmc_tanh.score(dgx_train, dgy_train), elmc_tanh.score(dgx_test, dgy_test)
@@ -191,7 +192,7 @@ def powtanh_xfer(activations, power=1.0):
191192

192193
# <codecell>
193194

194-
elmc = SimpleELMClassifier(n_hidden=500, xfer_func='hardlim')
195+
elmc = SimpleELMClassifier(n_hidden=500, activation_func='hardlim')
195196
elmc.fit(dgx_train, dgy_train)
196197
print elmc.score(dgx_train, dgy_train), elmc.score(dgx_test, dgy_test)
197198

@@ -204,8 +205,11 @@ def powtanh_xfer(activations, power=1.0):
204205

205206
# <codecell>
206207

207-
elmr = SimpleELMRegressor(xfer_func='tribas')
208+
elmr = SimpleELMRegressor(activation_func='tribas')
208209
elmr.fit(xtoy_train, ytoy_train)
209210
print elmr.score(xtoy_train, ytoy_train), elmr.score(xtoy_test, ytoy_test)
210211
plot(xtoy, ytoy, xtoy, elmr.predict(xtoy))
211212

213+
# <codecell>
214+
215+

‎plot_elm_comparison.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,21 @@ def make_classifiers():
137137

138138
# pass user defined transfer func
139139
sinsq = (lambda x: np.power(np.sin(x), 2.0))
140-
srhl_sinsq = SimpleRandomHiddenLayer(n_hidden=nh, xfer_func=sinsq,
140+
srhl_sinsq = SimpleRandomHiddenLayer(n_hidden=nh,
141+
activation_func=sinsq,
141142
random_state=0)
142143

143144
# use internal transfer funcs
144-
srhl_tanh = SimpleRandomHiddenLayer(n_hidden=nh, xfer_func='tanh',
145+
srhl_tanh = SimpleRandomHiddenLayer(n_hidden=nh,
146+
activation_func='tanh',
145147
random_state=0)
146148

147-
srhl_tribas = SimpleRandomHiddenLayer(n_hidden=nh, xfer_func='tribas',
149+
srhl_tribas = SimpleRandomHiddenLayer(n_hidden=nh,
150+
activation_func='tribas',
148151
random_state=0)
149152

150-
srhl_hardlim = SimpleRandomHiddenLayer(n_hidden=nh, xfer_func='hardlim',
153+
srhl_hardlim = SimpleRandomHiddenLayer(n_hidden=nh,
154+
activation_func='hardlim',
151155
random_state=0)
152156

153157
# use gaussian RBF

0 commit comments

Comments
 (0)