Package topo :: Package sheet :: Module basic
[hide private]
[frames] | no frames]

Source Code for Module topo.sheet.basic

  1  """ 
  2  Neural sheet objects. 
  3   
  4  $Id: basic.py 11315 2010-07-27 17:43:01Z ceball $ 
  5  """ 
  6  __version__='$Revision: 8986 $' 
  7   
  8  # Imported here so that all Sheets will be in the same package 
  9  from topo.base.sheet import Sheet 
 10  from topo.base.projection import ProjectionSheet 
 11  from topo.base.cf import CFSheet 
 12  from topo.misc.generatorsheet import GeneratorSheet 
 13   
 14  # Imported here for ease of access by users 
 15  from topo.base.boundingregion import BoundingBox 
 16  from topo.base.sheet import activity_type # CEBALERT: ...is it? 
 17   
 18  import numpy 
 19   
 20  import topo 
 21   
 22  import param 
 23   
 24  from topo.base.cf import MaskedCFIter 
 25  from topo.base.projection import Projection 
 26  from topo.base.simulation import FunctionEvent, PeriodicEventSequence 
 27   
 28   
29 -class ActivityCopy(Sheet):
30 """ 31 Copies incoming Activity patterns to its activity matrix and output port. 32 33 Trivial Sheet class that is useful primarily as a placeholder for 34 data that is computed elsewhere but that you want to appear as a 35 Sheet, e.g. when wrapping an external simulation. 36 """ 37 38 dest_ports=['Activity'] 39 src_ports=['Activity'] 40
41 - def input_event(self,conn,data):
42 self.input_data=data
43
44 - def process_current_time(self):
45 if hasattr(self, 'input_data'): 46 self.activity*=0 47 self.activity+=self.input_data 48 self.send_output(src_port='Activity',data=self.activity) 49 del self.input_data
50 51 52 53
54 -class SequenceGeneratorSheet(GeneratorSheet):
55 """ 56 Sheet that generates a timed sequence of patterns. 57 58 This sheet will repeatedly generate the input_sequence, with the 59 given onsets. The sequence is repeated every self.period time 60 units. If the total length of the sequence is longer than 61 self.period, a warning is issued and the sequence repeats 62 immediately after completion. 63 """ 64 65 input_sequence = param.List(default=[], 66 doc="""The sequence of patterns to generate. Must be a list of 67 (onset,generator) tuples. An empty list defaults to the 68 single tuple: (0,self.input_generator), resulting in 69 identical behavior to an ordinary GeneratorSheet.""") 70 71
72 - def __init__(self,**params):
73 super(SequenceGeneratorSheet,self).__init__(**params) 74 if not self.input_sequence: 75 self.input_sequence = [(0,self.input_generator)]
76
77 - def start(self):
78 assert self.simulation 79 80 event_seq = [] 81 for delay,gen in self.input_sequence: 82 event_seq.append(FunctionEvent(delay,self.set_input_generator,gen)) 83 event_seq.append(FunctionEvent(0,self.generate)) 84 now = self.simulation.time() 85 self.event = PeriodicEventSequence(now+self.phase,self.period,event_seq) 86 self.simulation.enqueue_event(self.event)
87 88
89 -def compute_joint_norm_totals(projlist,active_units_mask=True):
90 """ 91 Compute norm_total for each CF in each projection from a group to 92 be normalized jointly. 93 """ 94 # Assumes that all Projections in the list have the same r,c size 95 assert len(projlist)>=1 96 iterator = MaskedCFIter(projlist[0],active_units_mask=active_units_mask) 97 98 for junk,i in iterator(): 99 sums = [p.flatcfs[i].norm_total for p in projlist] 100 joint_sum = numpy.add.reduce(sums) 101 for p in projlist: 102 p.flatcfs[i].norm_total=joint_sum
103 104
105 -class JointNormalizingCFSheet(CFSheet):
106 """ 107 A type of CFSheet extended to support joint sum-based normalization. 108 109 For L1 normalization, joint normalization means normalizing the 110 sum of (the absolute values of) all weights in a set of 111 corresponding CFs in different Projections, rather than only 112 considering weights in the same CF. 113 114 This class makes it possible for a model to use joint 115 normalization, by providing a mechanism for grouping Projections 116 (see _port_match), plus a learn() function that computes the joint 117 sums. Joint normalization also requires having ConnectionField 118 store and return a norm_total for each neuron, and having an 119 TransferFn that will respect this norm_total rather than the strict 120 total of the ConnectionField's weights. At present, 121 CFPOF_DivisiveNormalizeL1 and CFPOF_DivisiveNormalizeL1_opt do use 122 norm_total; others can be extended to do something similar if 123 necessary. 124 125 To enable joint normalization, you can declare that all the 126 incoming connections that should be normalized together each 127 have a dest_port of: 128 129 dest_port=('Activity','JointNormalize', 'AfferentGroup1'), 130 131 Then all those that have this dest_port will be normalized 132 together, as long as an appropriate TransferFn is being used. 133 """ 134 135 joint_norm_fn = param.Callable(default=compute_joint_norm_totals,doc=""" 136 Function to use to compute the norm_total for each CF in each 137 projection from a group to be normalized jointly.""") 138 139 # JABALERT: Should check that whenever a connection is added to a 140 # group, it has the same no of cfs as the existing connections.
141 - def start(self):
142 self._normalize_weights(active_units_mask=False)
143 144 145 # CEBALERT: rename active_units_mask and default to False
146 - def _normalize_weights(self,active_units_mask=True):
147 """ 148 Apply the weights_output_fns for every group of Projections. 149 150 If active_units_mask is True, only active units will have 151 their weights normalized. 152 """ 153 for key,projlist in self._grouped_in_projections('JointNormalize'): 154 if key == None: 155 normtype='Individually' 156 else: 157 normtype='Jointly' 158 self.joint_norm_fn(projlist,active_units_mask) 159 160 self.debug(normtype + " normalizing:") 161 162 for p in projlist: 163 p.apply_learn_output_fns(active_units_mask=active_units_mask) 164 self.debug(' ',p.name)
165 166
167 - def learn(self):
168 """ 169 Call the learn() method on every Projection to the Sheet, and 170 call the output functions (jointly if necessary). 171 """ 172 # Ask all projections to learn independently 173 for proj in self.in_connections: 174 if not isinstance(proj,Projection): 175 self.debug("Skipping non-Projection "+proj.name) 176 else: 177 proj.learn() 178 179 # Apply output function in groups determined by dest_port 180 self._normalize_weights()
181 182 183
184 -class JointNormalizingCFSheet_Continuous(JointNormalizingCFSheet):
185 """ 186 CFSheet that runs continuously, with no 'resting' periods between pattern presentations. 187 188 Note that learning occurs only when the time is a whole number. 189 """
190 - def process_current_time(self):
191 if self.new_input: 192 self.new_input = False 193 if(float(topo.sim.time()) % 1.0 == 0.0): 194 #self.activate() 195 if (self.plastic): 196 self.learn() 197 #else: 198 self.activate()
199 200 201 202 __all__ = list(set([k for k,v in locals().items() if isinstance(v,type) and issubclass(v,Sheet)])) 203