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
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
15 from topo.base.boundingregion import BoundingBox
16 from topo.base.sheet import activity_type
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
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
43
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
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
76
87
88
90 """
91 Compute norm_total for each CF in each projection from a group to
92 be normalized jointly.
93 """
94
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
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
140
143
144
145
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
168 """
169 Call the learn() method on every Projection to the Sheet, and
170 call the output functions (jointly if necessary).
171 """
172
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
180 self._normalize_weights()
181
182
183
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 """
199
200
201
202 __all__ = list(set([k for k,v in locals().items() if isinstance(v,type) and issubclass(v,Sheet)]))
203