1- # -*- coding: utf8
1+ #-*- coding: utf8
22# Author: David C. Lambert [dcl -at- panix -dot- com]
33# Copyright(c) 2013
44# License: Simple BSD
3535 ]
3636
3737
38- # Abstract Base Class for random hidden layers
3938class BaseRandomLayer (BaseEstimator , TransformerMixin ):
39+ """Abstract Base Class for random layers"""
4040 __metaclass__ = ABCMeta
4141
4242 _internal_activation_funcs = dict ()
4343
4444 @classmethod
4545 def activation_func_names (cls ):
46+ """Get list of internal activation function names"""
4647 return cls ._internal_activation_funcs .keys ()
4748
4849 # take n_hidden and random_state, init components_ and
@@ -248,7 +249,6 @@ class RandomLayer(BaseRandomLayer):
248249 'sigmoid' : _sigmoid ,
249250 'softlim' : _softlim ,
250251 'hardlim' : _hardlim ,
251- 'inv_tribas' : _inv_tribas ,
252252 'gaussian' : _gaussian ,
253253 'multiquadric' : _multiquadric ,
254254 'inv_multiquadric' : _inv_multiquadric ,
@@ -277,13 +277,15 @@ def __init__(self, n_hidden=20, alpha=0.5, random_state=None,
277277 self ._use_rbf_input = (self .alpha != 1.0 )
278278
279279 def _get_user_components (self , key ):
280+ """Look for given user component"""
280281 try :
281282 return self .user_components [key ]
282283 except (TypeError , KeyError ):
283284 return None
284285
285- # compute radii
286- def _compute_radii (self , X ):
286+ def _compute_radii (self ):
287+ """Generate RBF radii"""
288+
287289 # use supplied radii if present
288290 radii = self ._get_user_components ('radii' )
289291
@@ -297,15 +299,16 @@ def _compute_radii(self, X):
297299
298300 self .components_ ['radii' ] = radii
299301
300- # compute centers
301302 def _compute_centers (self , X , sparse , rs ):
303+ """Generate RBF centers"""
304+
302305 # use supplied centers if present
303306 centers = self ._get_user_components ('centers' )
304307
305308 # use points taken uniformly from the bounding
306309 # hyperrectangle
307310 if (centers is None ):
308- n_samples , n_features = X .shape
311+ n_features = X .shape [ 1 ]
309312
310313 if (sparse ):
311314 fxr = xrange (n_features )
@@ -328,7 +331,9 @@ def _compute_centers(self, X, sparse, rs):
328331
329332 self .components_ ['centers' ] = centers
330333
331- def _compute_biases (self , X , rs ):
334+ def _compute_biases (self , rs ):
335+ """Generate MLP biases"""
336+
332337 # use supplied biases if present
333338 biases = self ._get_user_components ('biases' )
334339 if (biases is None ):
@@ -338,6 +343,8 @@ def _compute_biases(self, X, rs):
338343 self .components_ ['biases' ] = biases
339344
340345 def _compute_weights (self , X , rs ):
346+ """Generate MLP weights"""
347+
341348 # use supplied weights if present
342349 weights = self ._get_user_components ('weights' )
343350 if (weights is None ):
@@ -352,12 +359,12 @@ def _generate_components(self, X):
352359
353360 rs = check_random_state (self .random_state )
354361 if (self ._use_mlp_input ):
355- self ._compute_biases (X , rs )
362+ self ._compute_biases (rs )
356363 self ._compute_weights (X , rs )
357364
358365 if (self ._use_rbf_input ):
359366 self ._compute_centers (X , sp .issparse (X ), rs )
360- self ._compute_radii (X )
367+ self ._compute_radii ()
361368
362369 def _compute_input_activations (self , X ):
363370 """Compute input activations given X"""
@@ -377,11 +384,13 @@ def _compute_input_activations(self, X):
377384 scale = self .rbf_width * (1.0 - self .alpha )
378385 rbf_acts = scale * cdist (X , centers )/ radii
379386
380- #print rbf_acts.shape, mlp_acts.shape, self.alpha
381387 self .input_activations_ = mlp_acts + rbf_acts
382388
383389
384390class MLPRandomLayer (RandomLayer ):
391+ """Wrapper for RandomLayer with alpha (mixing coefficient) set
392+ to 1.0 for MLP activations only"""
393+
385394 def __init__ (self , n_hidden = 20 , random_state = None ,
386395 activation_func = 'tanh' , activation_args = None ,
387396 weights = None , biases = None ):
@@ -396,6 +405,9 @@ def __init__(self, n_hidden=20, random_state=None,
396405
397406
398407class RBFRandomLayer (RandomLayer ):
408+ """Wrapper for RandomLayer with alpha (mixing coefficient) set
409+ to 0.0 for RBF activations only"""
410+
399411 def __init__ (self , n_hidden = 20 , random_state = None ,
400412 activation_func = 'gaussian' , activation_args = None ,
401413 centers = None , radii = None , rbf_width = 1.0 ):
@@ -467,8 +479,12 @@ class GRBFRandomLayer(RBFRandomLayer):
467479 neural networks", Neurocomputing 74 (2011), 2502-2510
468480
469481 """
470- def _grbf (acts , taus ):
471- return np .exp (np .exp (- pow (acts , taus )))
482+ # def _grbf(acts, taus):
483+ # """GRBF activation function"""
484+
485+ # return np.exp(np.exp(-pow(acts, taus)))
486+
487+ _grbf = (lambda acts , taus : np .exp (np .exp (- pow (acts , taus ))))
472488
473489 _internal_activation_funcs = {'grbf' : _grbf }
474490
@@ -488,6 +504,8 @@ def __init__(self, n_hidden=20, grbf_lambda=0.001,
488504 # get centers from superclass, then calculate tau_vals
489505 # according to ref [1]
490506 def _compute_centers (self , X , sparse , rs ):
507+ """Generate centers, then compute tau, dF and dN vals"""
508+
491509 super (GRBFRandomLayer , self )._compute_centers (X , sparse , rs )
492510
493511 centers = self .components_ ['centers' ]
@@ -506,6 +524,8 @@ def _compute_centers(self, X, sparse, rs):
506524 self ._extra_args ['taus' ] = self .tau_vals
507525
508526 # get radii according to ref [1]
509- def _compute_radii (self , X ):
527+ def _compute_radii (self ):
528+ """Generate radii"""
529+
510530 denom = pow (- np .log (self .grbf_lambda ), 1.0 / self .tau_vals )
511531 self .components_ ['radii' ] = self .dF_vals / denom
0 commit comments