-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPCConnection.py
385 lines (314 loc) · 12.7 KB
/
PCConnection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import numpy as np
import torch
import pickle
import PCLayer
from copy import deepcopy
from abc import abstractmethod
dtype = torch.float32
# if torch.cuda.is_available():
# device = torch.device("cuda:5") # Uncomment this to run on GPU
# else:
# device = torch.device("cpu")
global device
#====================================================================================
#====================================================================================
#
# PCConnection class
#
# This is the base class.
#
#====================================================================================
#====================================================================================
class PCConnection():
def __init__(self, v=None, e=None, lower_layer=None, act_text='identity', device=torch.device('cpu')):
'''
con = PCConnection(v=None, e=None, act_text='identity', device=torch.device('cpu'))
Creates a PCConnection object from PCLayers 'v' to 'e'.
Inputs:
v a PCLayer object, or None
e a PCLayer object, or None
lower_layer is either None, or the same as v or e. It indicates which
layer is closer to the input side. If it is None, then the
layer with the lower index is chosen.
act_text one of: 'identity', 'logistic'
'''
self.device = device
if isinstance(v, PCLayer.PCLayer) and isinstance(e, PCLayer.PCLayer):
self.v = v
self.e = e
self.v_idx = v.idx
self.e_idx = e.idx
else:
self.v = []
self.e = []
self.v_idx = -99
self.e_idx = -99
self.v.SetType('value')
self.e.SetType('error')
self.learning_on = False
self.gamma = 0.1 # Learning time constant
# This next part sets M_sign to account for the fact that the
# connections going UP the network are exicitatory, and the
# connections going down are inhibitory. M_sign is the multiplier
# for the M direction.
# M_sign = 1 for M pointing UP the network
if lower_layer==None:
if self.v_idx<self.e_idx: # if (v) --M--> (e)
self.M_sign = torch.tensor(1., dtype=torch.float32, device=self.device)
elif self.e_idx<self.v_idx: # if (e) <--M-- (v)
self.M_sign = torch.tensor(-1., dtype=torch.float32, device=self.device)
else:
self.M_sign = 0.
else:
if lower_layer==self.v_idx: # if (v) --M--> (e)
self.M_sign = torch.tensor(1., dtype=torch.float32, device=self.device)
else: # if (e) <--M-- (v)
self.M_sign = torch.tensor(-1., dtype=torch.float32, device=self.device)
self.SetActivationFunction(act_text)
#====================================================================================
#
# Dynamics
#
#====================================================================================
def RateOfChange(self):
'''
con.RateOfChange(t)
Adds the between-layer connection terms to the derivatives of
v and e.
'''
self.CurrentTo_e()
self.CurrentTo_v()
if True: #self.learning_on:
self.RateOfChange_Weights()
@abstractmethod
def CurrentTo_e(self):
'''
con.CurrentTo_e()
Updates the input current to the connected e layer.
'''
pass
@abstractmethod
def CurrentTo_v(self):
'''
con.CurrentTo_e()
Updates the input current to the connected e layer.
'''
pass
def IncrementToWeights(self):
'''
con.IncrementToWeights()
Updates the derivatives of the weights.
'''
pass
def Step(self, dt=0.001):
pass
#====================================================================================
#
# Setting behaviours
#
#====================================================================================
def Learning(self, learning_on):
pass
def SetGamma(self, gamma):
self.gamma = gamma
def SetActivationFunction(self, act_text):
'''
conn.SetActivationFunction(act_text)
Sets the activation function for the connection.
The activation function is only applied to the adjacent v layer.
'''
self.act_text = act_text
if self.act_text=='logistic':
self.sigma = self.Logistic
self.sigma_p = self.Logistic_p
elif self.act_text=='identity':
self.sigma = self.Identity
self.sigma_p = self.Identity_p
elif self.act_text=='tanh':
self.sigma = self.Tanh
self.sigma_p = self.Tanh_p
#====================================================================================
#
# Activation functions
#
#====================================================================================
def Logistic(self):
'''
conn.Logistic()
Applies the logistic function to the values in layer v.
Returns a tensor the same size as v.x.
'''
return 1. / ( 1. + torch.exp(-self.v.x) )
def Logistic_p(self):
'''
conn.Logistic_p()
Computes the derivative of the logistic function of the values
in layer v.
Returns a tensor the same size as v.x.
'''
h = self.Logistic()
return h * ( 1. - h )
def Tanh(self):
'''
conn.Tanh()
Applies the tanh function to the values in layer v.
Returns a tensor the same size as v.x.
'''
return torch.tanh(self.v.x)
def Tanh_p(self):
'''
conn.Tanh_p()
Computes the derivative of the tanh function of the values
in layer v.
Returns a tensor the same size as v.x.
'''
return 1. - torch.pow(torch.tanh(self.v.x), 2)
def Identity(self):
return self.v.x
def Identity_p(self):
return torch.ones_like(self.v.x, dtype=torch.float32, device=self.device)
#====================================================================================
#====================================================================================
#
# DenseConnection class
#
#====================================================================================
#====================================================================================
class DenseConnection(PCConnection):
def __init__(self, v=None, e=None, lower_layer=None, sym=False, type='general', act_text='identity', device=torch.device('cpu')):
PCConnection.__init__(self, v=v, e=e, lower_layer=lower_layer, act_text=act_text, device=device)
self.type = type
if self.type=='1to1':
self.learning_on = False
else:
self.learning_on = True
self.M_learning_on = self.learning_on
self.W_learning_on = self.learning_on
# Create weight matrices
self.sym = sym
self.M = torch.randn(self.v.n, self.e.n, dtype=torch.float32, device=self.device)
if self.sym:
self.W = deepcopy(self.M.transpose(1,0))
else:
self.W = torch.randn(self.e.n, self.v.n, dtype=torch.float32, device=self.device)
self.dMdt = torch.zeros_like(self.M)
self.dWdt = torch.zeros_like(self.W)
self.M_decay = 0.
self.W_decay = 0.
self.rho = 0. # Coef for repelling weights away from zero
self.ell = 1. # Absolue sum of weights for normalization
self.renormalize = False # True to keep the abs sum of the weights = ell
#====================================================================================
#
# Dynamics
#
#====================================================================================
def CurrentTo_v(self):
self.v.RateOfChange( -self.M_sign * self.e.x@self.W * self.sigma_p() )
#self.v.RateOfChange( -self.M_sign * self.e.x@self.W )
def CurrentTo_e(self):
self.e.RateOfChange( self.M_sign * self.sigma()@self.M )
def RateOfChange_Weights(self):
'''
densecon.RateOfChange_Weights()
Sets the derivative of the weights (w.r.t. time), including decay.
'''
sigmax_times_e = ( self.sigma().transpose(1,0) @ self.e.x ) / self.v.batchsize
self.dMdt = -self.M_sign * sigmax_times_e - self.M_decay*self.M
self.dWdt = -self.M_sign * sigmax_times_e.transpose(1,0) - self.W_decay*self.W
if self.rho>0.:
Mnorm, Wnorm = self.WeightNorms()
# self.dMdt += self.rho/Mnorm * torch.randn_like(self.M, dtype=torch.float32, device=self.device)
# self.dWdt += self.rho/Wnorm * torch.randn_like(self.W, dtype=torch.float32, device=self.device)
self.dMdt += self.rho/Mnorm * self.M
self.dWdt += self.rho/Wnorm * self.W
def Step(self, dt=0.001):
if self.learning_on:
if self.M_learning_on:
self.M += self.dMdt*dt/self.gamma
if self.W_learning_on:
self.W += self.dWdt*dt/self.gamma
self.dMdt.zero_()
self.dWdt.zero_()
if self.renormalize:
#self.NormalizeWeights()
self.ClipWeights()
def NormalizeWeights(self):
weightsum = torch.sum(torch.abs(self.M))
self.M *= self.ell/weightsum
weightsum = torch.sum(torch.abs(self.W))
self.W *= self.ell/weightsum
def ClipWeights(self):
self.W.clamp_(min=0.)
self.M.clamp_(min=0.)
#====================================================================================
#
# Info
#
#====================================================================================
def WeightNorms(self):
Mnorm = torch.norm(self.M) # Frobenius norm, sqrt(sum(M**2))
Wnorm = torch.norm(self.W)
return Mnorm, Wnorm
#====================================================================================
#
# Setting behaviours
#
#====================================================================================
def Learning(self, learning_on):
if self.type=='general':
self.learning_on = learning_on
else:
self.learning_on = False
def SetWeightDecay(self, lam):
self.M_decay = lam
self.W_decay = lam
def SetRepelSmallWeights(self, rho):
'''
con.SetRepelSmallWeights(rho)
Sets the weight for the term that repels weigths away from zero.
If F is the Frobenius norm of the weights, then the DE for the
weights includes a term
rho/F * randomWeights
'''
self.rho = rho
#====================================================================================
#
# Creating Weight matrices
#
#====================================================================================
def SetIdentity(self, mult=1., random=0.):
'''
con.SetIdentity(mult=1.)
Sets the connection weights to mult times the identity matrix.
'''
assert (self.v.n==self.e.n), 'Cannot use identity matrix: Number of nodes do not match'
self.M = mult*torch.eye(self.v.n, dtype=torch.float32, device=self.device) + torch.randn_like(self.M, dtype=torch.float32, device=self.device)*random
if self.sym:
self.W = deepcopy(self.M)
else:
self.W = mult*torch.eye(self.v.n, dtype=torch.float32, device=self.device) + torch.randn_like(self.W, dtype=torch.float32, device=self.device)*random
def SetRandom(self, random=1.):
'''
con.SetRandom(random=1.)
'''
if self.type=='general':
self.M = torch.randn(self.v.n, self.e.n, dtype=torch.float32, device=self.device) * random
if self.sym:
self.W = self.M.transpose(1,0).clone().detach()
else:
self.W = torch.randn(self.e.n, self.v.n, dtype=torch.float32, device=self.device) * random
def SetRandomUniform(self, low=0, high=1):
if self.type=='general':
self.M = torch.rand(self.v.n, self.e.n, dtype=torch.float32, device=self.device)*(high-low) + low
if self.sym:
self.W = self.M.transpose(1,0).clone().detach()
else:
self.W = torch.rand(self.e.n, self.v.n, dtype=torch.float32, device=self.device)*(high-low) + low
def SetWeights(self, M, W=None):
self.M = torch.tensor(deepcopy(M))
if W is None:
self.W = deepcopy(self.M.transpose(1,0))
else:
self.W = torch.tensor(deepcopy(W))
# end