Package topo :: Package outputfns :: Module homeostatic
[hide private]
[frames] | no frames]

Source Code for Module topo.outputfns.homeostatic

  1  """ 
  2  Homeostatic output functions, which are designed to keep an activity 
  3  value in a desired range over time. 
  4   
  5  Originally implemented by Veldri Kurniawan, 2006. 
  6   
  7  $Id: homeostatic.py 7868 2008-02-07 14:16:01Z jbednar $ 
  8  """ 
  9  __version__='$Revision: 7868 $' 
 10   
 11  # ALERT: These should probably move into basic.py; they don't have any special dependencies. 
 12   
 13  import copy 
 14  import topo 
 15  from numpy import exp,zeros,ones 
 16   
 17  from topo.base.functionfamilies import OutputFn, OutputFnWithState, OutputFnParameter, IdentityOF 
 18  from topo.base.parameterclasses import Number, BooleanParameter, ListParameter, StringParameter 
 19  from topo.base.parameterizedobject import ParameterizedObject 
 20  from topo.base.sheet import activity_type 
 21  from topo.commands.pylabplots import vectorplot 
 22  from topo.misc.filepaths import normalize_path 
 23   
 24   
25 -class HomeostaticMaxEnt(OutputFnWithState):
26 """ 27 Implementation of homeostatic intrinsic plasticity from Jochen Triesch, 28 ICANN 2005, LNCS 3696 pp.65-70. 29 30 A sigmoid activation function is adapted automatically to achieve 31 desired average firing rate and approximately exponential 32 distribution of firing rates (for the maximum possible entropy). 33 34 Note that this OutputFn has state, so the history of calls to it 35 will affect future behavior. The updating parameter can be used 36 to disable changes to the state. 37 38 Also calculates average activity as useful debugging information, 39 for use with ValueTrackingOutoutFn Average activity is calculated as 40 an exponential moving average with a smoothing factor (smoothing). 41 For more information see: 42 NIST/SEMATECH e-Handbook of Statistical Methods, Single Exponential Smoothing 43 http://www.itl.nist.gov/div898/handbook/pmc/section4/pmc431.htm 44 """ 45 46 a_init = Number(default=13,doc="Multiplicative parameter controlling the exponential.") 47 48 b_init = Number(default=-4,doc="Additive parameter controlling the exponential.") 49 50 eta = Number(default=0.0002,doc="Learning rate for homeostatic plasticity.") 51 52 mu = Number(default=0.01,doc="Target average firing rate.") 53 54 smoothing = Number(default=0.9997, doc=""" 55 Weighting of previous activity vs. current activity when calculating the average.""") 56
57 - def __init__(self,**params):
58 super(HomeostaticMaxEnt,self).__init__(**params) 59 self.first_call = True
60
61 - def __call__(self,x):
62 63 if self.first_call: 64 self.first_call = False 65 self.a = ones(x.shape, x.dtype.char) * self.a_init 66 self.b = ones(x.shape, x.dtype.char) * self.b_init 67 self.y_avg = zeros(x.shape, x.dtype.char) 68 69 # Apply sigmoid function to x, resulting in what Triesch calls y 70 x_orig = copy.copy(x) 71 x *= 0.0 72 x += 1.0 / (1.0 + exp(-(self.a*x_orig + self.b))) 73 74 if self.updating: 75 76 self.y_avg = (1.0-self.smoothing)*x + self.smoothing*self.y_avg #Calculate average for use in debugging only 77 78 # Update a and b 79 self.a += self.eta * (1.0/self.a + x_orig - (2.0 + 1.0/self.mu)*x_orig*x + x_orig*x*x/self.mu) 80 self.b += self.eta * (1.0 - (2.0 + 1.0/self.mu)*x + x*x/self.mu)
81 82 83
84 -class ScalingOF(OutputFnWithState):
85 """ 86 Scales input activity based on the current average activity (x_avg). 87 88 The scaling is calculated to bring x_avg for each unit closer to a 89 specified target average. Calculates a scaling factor that is 90 greater than 1 if x_avg is less than the target and less than 1 if 91 x_avg is greater than the target, and multiplies the input 92 activity by this scaling factor. 93 94 The updating parameter allows the updating of the average values 95 to be disabled temporarily, e.g. while presenting test patterns. 96 """ 97 98 target = Number(default=0.01, doc=""" 99 Target average activity for each unit.""") 100 101 step=Number(default=1, doc=""" 102 How often to calculate the average activity and scaling factor.""") 103 104 smoothing = Number(default=0.9997, doc=""" 105 Determines the degree of weighting of previous activity vs. 106 current activity when calculating the average.""") 107 108
109 - def __init__(self,**params):
110 super(ScalingOF,self).__init__(**params) 111 self.n_step = 0 112 self.x_avg=None 113 self.sf=None
114
115 - def __call__(self,x):
116 117 if self.x_avg is None: 118 self.x_avg=self.target*ones(x.shape, activity_type) 119 if self.sf is None: 120 self.sf=ones(x.shape, activity_type) 121 122 # Collect values on each appropriate step 123 if self.updating: 124 self.n_step += 1 125 if self.n_step == self.step: 126 self.n_step = 0 127 self.sf *= 0.0 128 self.sf += self.target/self.x_avg 129 self.x_avg = (1.0-self.smoothing)*x + self.smoothing*self.x_avg 130 131 x *= self.sf
132 133 134 # JABALERT: Surely this should be merged back into HomeostaticMaxEnt as a parameterizable option instead?
135 -class HomeostaticMaxEnt_bonly(OutputFnWithState):
136 """ 137 Implementation of homeostatic intrinsic plasticity from Jochen Triesch, 138 ICANN 2005, LNCS 3696 pp.65-70, modified to change only the b value. 139 140 A sigmoid activation function is adapted automatically to achieve 141 desired average firing rate and approximately exponential 142 distribution of firing rates (for the maximum possible entropy). 143 144 Note that this OutputFn has state, so the history of calls to it 145 will affect future behavior. The updating parameter can be used 146 to disable changes to the state. 147 148 Also calculates average activity as useful debugging information, 149 for use with ValueTrackingOutoutFn Average activity is calculated as 150 an exponential moving average with a smoothing factor (smoothing). 151 For more information see: 152 NIST/SEMATECH e-Handbook of Statistical Methods, Single Exponential Smoothing 153 http://www.itl.nist.gov/div898/handbook/pmc/section4/pmc431.htm 154 """ 155 156 a_init = Number(default=13,doc="Multiplicative parameter controlling the exponential.") 157 158 b_init = Number(default=-4,doc="Additive parameter controlling the exponential.") 159 160 eta = Number(default=0.0002,doc="Learning rate for homeostatic plasticity.") 161 162 mu = Number(default=0.01,doc="Target average firing rate.") 163 164 smoothing = Number(default=0.9997, doc=""" 165 Weighting of previous activity vs. current activity when calculating the average.""") 166 167
168 - def __init__(self,**params):
169 super(HomeostaticMaxEnt_bonly,self).