-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPCNetwork.py
428 lines (351 loc) · 13 KB
/
PCNetwork.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import numpy as np
import torch
import pickle
import matplotlib.pyplot as plt
import PCLayer
import PCConnection
from tqdm import tqdm
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
class PCNetwork():
def __init__(self, device=torch.device('cpu')):
self.device = device
self.lyr = [] # list of layers
self.n_layers = 0 # number of layers
self.con = [] # list of connections
self.batchsize = 0 # size of batch
self.t_history = [] # for recording (probes)
self.t = 0. # current simulation time
self.probe_on = False # becomes True if at least one layer has a probe on
self.blackout = 0. # How long to wait during a hold before
# turning learning on.
self.learning_on = False # Used to control the blackout period
'''
Weight decay
The weight decay rate can change as training progresses. It is
implemented using 2 numbers:
- init_wd: initial weight decay rate, and
- drop_wd: fractional drop per epoch
Then, if e is the number of epochs, the weight decay is
init_wd * np.exp( np.log(1-drop_wd)*e )
'''
self.init_wd = 0.
self.drop_wd = 0.
#self.WeightDecay = (lambda e: init_wd*np.exp(np.log(1.-drop_wd)*e))
#=======
# Save and Load
def Save(self, fname):
with open(fname, 'wb') as fp:
pickle.dump(self, fp)
@classmethod
def Load(cls, fname):
with open(fname, 'rb') as fp:
net = pickle.load(fp)
return net
#=======
# Dynamics
def Learn(self, data, T, dt=0.001, epochs=5):
'''
net.Learn(data, T, dt=0.001, epochs=5)
Perform learning on the network.
Inputs:
data a DataLoader object, or a list containing two tensors,
where data[0] are inputs, and data[1] are targets
T how long to hold each sample (in seconds)
dt step size (in seconds)
epochs number of epochs
blackout determines how long to wait during a hold before
turning learning on.
'''
#self.Learning(True)
self.lyr[0].Clamped(True)
self.lyr[-1].Clamped(True)
self.Learning(True)
for k in tqdm(range(epochs)):
#batches = MakeBatches(x, t, batchsize=batchsize, shuffle=True)
if type(data) in (list, ):
data = [data]
n_batches = len(data)
#print('Epoch: '+str(k)+' weight decay = '+str(self.CurrentWeightDecay(k)))
for batch_idx,b in enumerate(data):
epoch_time = k + batch_idx/n_batches
#print(epoch_time, self.WeightDecay(epoch_time))
self.SetWeightDecay(self.CurrentWeightDecay(epoch_time))
self.ResetState(random=0.5)
self.SetInput(b[0])
self.SetOutput(b[1])
self.Run(T, dt=0.001)
def Predict(self, x, T, dt=0.001):
self.Learning(False)
self.lyr[0].Clamped(True)
self.lyr[-1].Clamped(False)
self.SetInput(x)
self.Run(T, dt=dt)
return self.lyr[-1].x
def Generate(self, t, T, dt=0.001):
self.Learning(False)
self.lyr[0].Clamped(False)
self.lyr[-1].Clamped(True)
self.SetOutput(t)
self.Run(T, dt=dt)
return self.lyr[0].x
def Run(self, T, dt=0.001):
'''
net.Run(T, dt=0.001)
Simulates the network for T seconds using a time step of dt.
The network state is continued from the previous run, if one
exists. Calling net.Reset() removes the previous run.
'''
self.probe_on = False
for l in self.lyr:
self.probe_on = self.probe_on or l.probe_on
self.learning_on = False
t_start = self.t
while self.t < t_start + T:
# Learning blackout
if self.t-t_start > self.blackout:
self.learning_on = True
self.RateOfChange()
self.Step(dt=dt)
self.t += dt
if self.probe_on:
self.t_history.append(self.t)
def RateOfChange(self):
'''
net.RateOfChange()
Updates the input currents to all nodes in the network
'''
#for l in self.lyr:
# l.dxdt.zero_()
# Delivers current across connections
# and overwrites dMdt and dWdt if a connection's learning is on.
for c in self.con:
c.RateOfChange()
# Apply activity decay (where appropriate)
for l in self.lyr:
l.Decay(self.t)
def Step(self, dt=0.001):
# Increment the state of each layer
for l in self.lyr:
l.Step(dt=dt)
# Increment the connection weights (potentially)
if self.learning_on:
for c in self.con:
c.Step(dt=dt)
def dEdInput(self, x, t):
'''
dEdx = net.dEdInput(x, t)
Computes dEdx given input x and target t.
The cost function is specified by the network.
Inputs:
x is the input that you want to perturb
t is the corresponding true target vector (one-hot)
Output:
dEdx is a vector the same shape as x
'''
self.FeedForward([x])
t = np.array(t) # convert t to an array, in case it's not
# Error gradient for top layer
dEdz = net.TopGradient(t)
# Loop down through the layers
for i in range(net.n_layers-2, -1, -1):
pre = net.lyr[i]
if i>0:
dEdz = ( dEdz @ net.W[i].T ) * pre.sigma_p()
# Calculate gradient w.r.t. input (x)
dEdx = np.squeeze( np.dot(net.W[0], dEdz.T) )
return dEdx
def FastPredict(self, x):
'''
y = net.FastPredict(x)
Runs the network as a feedforward network, starting with x as input,
skipping the error nodes, and overwriting the value nodes
with the equilibrium solution.
Returns the activity of the output layer.
NOTE: This method assumes that the connections are ordered from
bottom to top.
'''
#x = np.array(x) # Convert input to array, in case it's not
self.SetInput(x) # Allocate, and set layer 0
# Reset all derivatives to zero
for l in self.lyr:
l.dxdt.zero_()
# Loop over connections...
for c in self.con:
if c.type=='general': # c.M_sign = 1
# (v) --M--> (e)
c.CurrentTo_e()
c.e.x = c.e.dxdt.detach().clone() # Overwrite state with input current
else: # c.type=="1to1", c.M_sign = -1
# (e) --W--> (v)
c.CurrentTo_v()
c.v.x = c.v.dxdt.detach().clone()
# Return activity of output layer
return self.lyr[-1].x
#=======
# Setting behaviours
def Learning(self, learning_on):
'''
net.Learning(learning_on)
Turn learning on (True) or off (False) for all 'general' connections.
'''
self.learning_on = learning_on
for c in self.con:
c.Learning(learning_on)
for l in self.lyr:
l.Learning(learning_on)
def SetInput(self, x):
self.Allocate(x)
self.lyr[0].SetState(x)
def SetOutput(self, t):
self.Allocate(t)
self.lyr[-1].SetState(t)
def SetTau(self, tau):
for l in self.lyr:
l.SetTau(tau)
def SetGamma(self, gamma):
for c in self.con:
c.SetGamma(gamma)
for l in self.lyr:
l.SetGamma(gamma)
def SetActivityDecay(self, lam):
for l in self.lyr:
l.SetActivityDecay(lam) # does nothing on error layers
def SetDynamicWeightDecay(self, init_wd, drop_wd):
'''
net.SetDynamicWeightDecay(init_wd, drop_wd)
Sets the weight decay parameters.
The weight decay rate can change as training progresses. It is
implemented using 2 numbers:
- init_wd: initial weight decay rate, and
- drop_wd: fractional drop per epoch
Then, if e is the number of epochs, the weight decay is
init_wd * np.exp( np.log(1-drop_wd)*e )
'''
self.init_wd = init_wd
self.drop_wd = drop_wd
#self.WeightDecay = (lambda e: init_wd*np.exp(np.log(1.-drop_wd)*e))
def CurrentWeightDecay(self, fe):
return self.init_wd*np.exp(np.log(1.-self.drop_wd)*fe)
def SetWeightDecay(self, lam):
for c in self.con:
c.SetWeightDecay(lam)
def SetRepelSmallWeights(self, rho):
for c in self.con:
c.SetRepelSmallWeights(rho)
def SetBlackout(self, t):
'''
net.SetBlackout(t)
Sets the blackout period for learning. The blackout period is
how long to wait during a hold before turning learning on.
'''
self.blackout = t
#=======
# Building utilities
def AddLayer(self, l):
'''
net.AddLayer(l)
Adds a layer to the network 'net', where l is a network object.
'''
self.lyr.append(l)
self.n_layers = len(self.lyr)
l.idx = self.n_layers-1
def Connect(self, v_idx=None, e_idx=None, lower_layer=None, type='general', sym=False, act_text='logistic'):
'''
net.Connect(v=None, e=None, type='general')
Creates a connection between the value node v and the error
node e.
Inputs:
v_idx, e_idx are indices to the network's lyr list
lower_layer is either v_idx or e_idx, or None (default to min of x_idx and e_idx)
type is either 'general', or '1to1'
act_text is one of 'logistic', or 'identity'
'''
if type=='general':
c = PCConnection.DenseConnection(v=self.lyr[v_idx], e=self.lyr[e_idx], lower_layer=lower_layer, sym=sym, act_text=act_text, device=self.device)
elif type=='1to1':
c = PCConnection.DenseConnection(v=self.lyr[v_idx], e=self.lyr[e_idx], lower_layer=lower_layer, type=type, act_text='identity', device=self.device)
c.SetIdentity()
#self.lyr[e_idx].SetBias(random=1.)
self.lyr[e_idx].SetDecay(1.) # Set decay of error layer
self.lyr[e_idx].type = 'error'
self.con.append(c)
def Allocate(self, x):
'''
net.Allocate(x)
Allocates vectors for the nodes in all the layers.
This method makes no guarantees about the values in the nodes.
Input:
x can either be the number of samples in a batch, or it can be
a batch.
'''
proposed_batchsize = 1
if type(x) in (int, float, ):
proposed_batchsize = x
else:
proposed_batchsize = len(x)
if proposed_batchsize!=self.batchsize:
self.batchsize = proposed_batchsize
del self.t_history
self.t_history = []
self.t = 0.
for l in self.lyr:
l.Allocate(batchsize=proposed_batchsize)
def Reset(self, random=0.):
'''
net.Reset(random=0.)
Resets the simulation of the network, and erases its history.
It also resets the values in the nodes to random numbers using
a Gaussian distribution with mean 0 and std specified by the
input parameter 'random'.
'''
self.ClearHistory()
self.ResetState(random=random)
def ClearHistory(self):
del self.t_history
self.t_history = []
self.t = 0.
for l in self.lyr:
l.ClearHistory()
def ResetState(self, random=0.):
for l in self.lyr:
l.ResetState(random=random)
#=======
# Utilities
def Probe(self, probe_on=True):
'''
net.Probe(probe_on=True)
Turns on (True) or off (False) data history recording for
all the layers in the network.
'''
for l in self.lyr:
l.Probe(probe_on)
def Plot(self, idx=0):
'''
net.Plot(idx=0)
Plots the time evolution of sample idx in the batch.
It displays an array of plots, one plot for each layer.
The plots are in 2 rows, with odd-index layers in the top row,
and even-index layers in the bottom row.
'''
fig = plt.figure(figsize=(10,4), constrained_layout=True)
n_valnodes = np.ceil(self.n_layers/2.)
gs = plt.GridSpec(2, int(2*n_valnodes), figure=fig)
r,c = 0,0
for l in self.lyr:
fig.add_subplot(gs[r,c:c+2])
l.Plot(self.t_history, idx=idx)
if r==0 and c==0:
plt.ylabel('v')
elif r==1 and c==1:
plt.ylabel('e')
if r==1:
plt.xlabel('Time (s)')
r = (r+1)%2
c += 1
return fig
# end