1 """
2 Learning functions for Projections.
3
4
5 For example, CFProjectionLearningFunctions compute a new set of
6 ConnectionFields when given an input and output pattern and a set of
7 ConnectionField objects.
8
9 $Id: projfn.py 11316 2010-07-27 17:52:53Z ceball $
10 """
11 __version__ = "$Revision: 11316 $"
12
13 from numpy import ones,zeros
14 import numpy.oldnumeric as Numeric
15 from numpy.oldnumeric import Float
16
17 import param
18
19 from topo.base.cf import CFPLearningFn
20 from topo.base.sheet import activity_type
21 from topo.base.functionfamily import Hebbian,LearningFn
22
23 from topo.base.cf import CFPLF_Identity,CFPLF_Plugin
24
25 from basic import BCMFixed
26
27
29 """
30 Hebbian CFProjection learning rule based on Euclidean distance.
31
32 Learning is driven by the distance from the input pattern to the
33 weights, scaled by the current activity. To implement a Kohonen
34 SOM algorithm, the activity should be the neighborhood kernel
35 centered around the winning unit, as implemented by KernelMax.
36 """
37
38 - def __call__(self, iterator, input_activity, output_activity, learning_rate, **params):
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
107 """
108 LearningFn that incorporates a trace of recent activity,
109 not just the current activity.
110
111 Based on P. Foldiak (1991), "Learning Invariance from
112 Transformation Sequences", Neural Computation 3:194-200. Also see
113 Sutton and Barto (1981) and Wallis and Rolls (1997).
114
115 Incorporates a decay term to keep the weight vector bounded, and
116 so it does not normally require any output_fn normalization for
117 stability.
118
119 NOT YET TESTED.
120 """
121
122 trace_strength=param.Number(default=0.5,bounds=(0.0,1.0),
123 doc="How much the learning is dominated by the activity trace, relative to the current value.")
124
125 single_cf_fn = param.ClassSelector(LearningFn,default=Hebbian(),
126 doc="LearningFn that will be applied to each CF individually.")
127
128 - def __call__(self, iterator, input_activity, output_activity, learning_rate, **params):
146
147
148
150 """
151 CFPLearningFunction applying the specified (default is Hebbian)
152 single_cf_fn to each CF, where normalization is done in an outstar-manner.
153
154 Presumably does not need a separate output_fn for normalization.
155
156 NOT YET TESTED.
157 """
158 single_cf_fn = param.ClassSelector(LearningFn,default=Hebbian(),
159 doc="LearningFn that will be applied to each CF individually.")
160
161 outstar_wsum = None
162
163 - def __call__(self, iterator, input_activity, output_activity, learning_rate, **params):
179
180
181
182
184 """
185 Learning function using homeostatic synaptic scaling from
186 Sullivan & de Sa, "Homeostatic Synaptic Scaling in Self-Organizing Maps",
187 Neural Networks (2006), 19(6-7):734-43.
188
189 Does not necessarily require output_fn normalization for stability.
190 """
191 single_cf_fn = param.ClassSelector(LearningFn,default=Hebbian(),
192 doc="LearningFn that will be applied to each CF individually")
193
194 beta_n = param.Number(default=0.01,bounds=(0,None),
195 doc="homeostatic learning rate")
196
197 beta_c = param.Number(default=0.005,bounds=(0,None),
198 doc="time window over which the neuron's firing rate is averaged")
199
200 activity_target = param.Number(default=0.1,bounds=(0,None),
201 doc="Target average activity")
202
203
204
205
206
211
212 - def __call__(self, iterator, input_activity, output_activity, learning_rate, **params):
213 """
214 Update the value of the given weights matrix based on the
215 input_activity matrix (of the same size as the weights matrix)
216 and the response of this unit (the unit_activity), governed by
217 a per-connection learning rate.
218 """
219 if not hasattr(self,'averages'):
220 self.averages = ones(output_activity.shape,Float) * 0.1
221
222
223
224 for cf,i in iterator():
225 current_norm_value = 1.0*Numeric.sum(abs(cf.weights.ravel()))
226 if current_norm_value != 0:
227 factor = (1.0/current_norm_value)
228 cf.weights *= factor
229
230
231 self.averages = self.beta_c * output_activity + (1.0-self.beta_c) * self.averages
232 activity_norm = 1.0 + self.beta_n * \
233 ((self.averages - self.activity_target)/self.activity_target)
234
235 single_connection_learning_rate = self.constant_sum_connection_rate(iterator.proj_n_units,learning_rate)
236
237
238 single_cf_fn = self.single_cf_fn
239 for cf,i in iterator():
240 single_cf_fn(cf.get_input_matrix(input_activity),
241 output_activity.flat[i], cf.weights, single_connection_learning_rate)
242
243
244 cf.weights /= activity_norm.flat[i]
245
246
247 cf.weights *= cf.mask
248
249
250
251 self.ave_hist.append(self.averages.flat[0])
252 self.temp_hist.append (Numeric.sum(abs(iterator.flatcfs[0].weights.ravel())))
253
254
255
256
258 """
259 CFPLearningFunction applying the specified single_cf_fn to each CF.
260 Scales the single-connection learning rate by a scaling factor
261 that is different for each individual unit. Thus each individual
262 connection field uses a different learning rate.
263 """
264
265 single_cf_fn = param.ClassSelector(LearningFn,default=Hebbian(),
266 doc="Accepts a LearningFn that will be applied to each CF individually.")
267
268 learning_rate_scaling_factor = param.Parameter(default=None,
269 doc="Matrix of scaling factors for scaling the learning rate of each CF individually.")
270
271
272 - def __call__(self, iterator, input_activity, output_activity, learning_rate, **params):
287
288
292