Package topo :: Package base :: Module functionfamily
[hide private]
[frames] | no frames]

Source Code for Module topo.base.functionfamily

  1  """ 
  2  Basic function objects. 
  3   
  4  TransferFns: accept and modify a 2d array 
  5  ResponseFns: accept two 2d arrays and return a scalar 
  6  LearningFns: accept two 2d arrays and a scalar, return one of the arrays modified 
  7   
  8  $Id: functionfamily.py 11088 2010-06-17 17:23:59Z ceball $ 
  9  """ 
 10  __version__='$Revision: 11088 $' 
 11   
 12  # CEBALERT: Documentation is just draft. 
 13   
 14  import numpy 
 15   
 16  import param 
 17   
 18   
19 -class TransferFn(param.Parameterized):
20 """ 21 Function object to modify a matrix in place, e.g. for normalization. 22 23 Used for transforming an array of intermediate results into a 24 final version, by cropping it, normalizing it, squaring it, etc. 25 26 Objects in this class must support being called as a function with 27 one matrix argument, and are expected to change that matrix in place. 28 """ 29 __abstract = True 30 31 # CEBALERT: can we have this here - is there a more appropriate 32 # term for it, general to output functions? JAB: Please do rename it! 33 norm_value = param.Parameter(default=None) 34 35
36 - def __call__(self,x):
37 raise NotImplementedError
38 39 # Trivial example of a TransferFn, provided for when a default 40 # is needed. The other concrete OutputFunction classes are stored 41 # in transferfn/, to be imported as needed.
42 -class IdentityTF(TransferFn):
43 """ 44 Identity function, returning its argument as-is. 45 46 For speed, calling this function object is sometimes optimized 47 away entirely. To make this feasible, it is not allowable to 48 derive other classes from this object, modify it to have different 49 behavior, add side effects, or anything of that nature. 50 """ 51
52 - def __call__(self,x,sum=None):
53 pass
54 55 56 57
58 -class LearningFn(param.Parameterized):
59 """ 60 Abstract base class for learning functions that plug into 61 CFPLF_Plugin. 62 """ 63 64 __abstract = True 65 66 # JABALERT: Shouldn't the single_connection_learning_rate be 67 # omitted from the call and instead made into a class parameter?
68 - def __call__(self,input_activity, unit_activity, weights, single_connection_learning_rate):
69 """ 70 Apply this learning function given the input and output 71 activities and current weights. 72 73 Must be implemented by subclasses. 74 """ 75 raise NotImplementedError
76 77
78 -class Hebbian(LearningFn):
79 """ 80 Basic Hebbian rule; Dayan and Abbott, 2001, equation 8.3. 81 82 Increases each weight in proportion to the product of this 83 neuron's activity and the input activity. 84 85 Requires some form of output_fn normalization for stability. 86 """ 87
88 - def __call__(self,input_activity, unit_activity, weights, single_connection_learning_rate):
89 weights += single_connection_learning_rate * unit_activity * input_activity
90 91
92 -class IdentityLF(LearningFn):
93 """ 94 Identity function; does not modify the weights. 95 96 For speed, calling this function object is sometimes optimized 97 away entirely. To make this feasible, it is not allowable to 98 derive other classes from this object, modify it to have different 99 behavior, add side effects, or anything of that nature. 100 """ 101
102 - def __call__(self,input_activity, unit_activity, weights, single_connection_learning_rate):
103 pass
104 105 106
107 -class ResponseFn(param.Parameterized):
108 """Abstract base class for response functions that plug into CFPRF_Plugin.""" 109 110 __abstract = True 111
112 - def __call__(self,m1,m2):
113 """ 114 Apply the response function; must be implemented by subclasses. 115 """ 116 raise NotImplementedError
117 118 119
120 -class DotProduct(ResponseFn):
121 """ 122 Return the sum of the element-by-element product of two 2D 123 arrays. 124 """
125 - def __call__(self,m1,m2):
126 # CBENHANCEMENT: numpy.vdot(m1,m2)? 127 # Early tests indicated that 128 # vdot(a,b)=dot(a.ravel(),b.ravel()) but was slower. Should 129 # check that. 130 return numpy.dot(m1.ravel(),m2.ravel())
131 132 133
134 -class CoordinateMapperFn(param.Parameterized):
135 """Abstract base class for functions mapping from a 2D coordinate into another one.""" 136 137 __abstract = True 138
139 - def __call__(self,x,y):
140 """ 141 Apply the coordinate mapping function; must be implemented by subclasses. 142 """ 143 raise NotImplementedError
144 145
146 -class IdentityMF(CoordinateMapperFn):
147 """Return the x coordinate of the given coordinate."""
148 - def __call__(self,x,y):
149 return x,y
150 151
152 -class PatternDrivenAnalysis(param.Parameterized):
153 """ 154 Abstract base class for various stimulus-response types of analysis. 155 156 This type of analysis consists of presenting a set of input 157 patterns and collecting the responses to each one, which one will 158 often want to do in a way that does not affect the current state 159 of the network. 160 161 To achieve this, the class defines several types of hooks where 162 arbitrary function objects (i.e., callables) can be registered. 163 These hooks are generally used to ensure that unrelated previous 164 activity is eliminated, that subsequent patterns do not interact, 165 and that the initial state is restored after analysis. 166 167 Any subclasses must ensure that these hook lists are run at the 168 appropriate stage in their processing, using e.g. 169 "for f in some_hook_list: f()". 170 """ 171 172 __abstract = True 173 174 pre_analysis_session_hooks = param.HookList(default=[],instantiate=False,doc=""" 175 List of callable objects to be run before an analysis session begins.""") 176 177 pre_presentation_hooks = param.HookList(default=[],instantiate=False,doc=""" 178 List of callable objects to be run before each pattern is presented.""") 179 180 post_presentation_hooks = param.HookList(default=[],instantiate=False,doc=""" 181 List of callable objects to be run after each pattern is presented.""") 182 183 post_analysis_session_hooks = param.HookList(default=[],instantiate=False,doc=""" 184 List of callable objects to be run after an analysis session ends.""")
185