-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathneuralNetwork.py
470 lines (421 loc) · 13.6 KB
/
neuralNetwork.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import theano
import theano.tensor as T
import numpy as np
import time
import h5py
import cPickle as pickle
from utilityFunctions import (preds, printStats, geoMean, floatToString,
getWithName, intCounter)
from objectives import objecD
from optimisers import optD, Optimiser
# Wrapper object for the theano tensors and associated information that
# get passed though the network when it's constructed.
class Representation(object): # {{{
def __init__(self, shape, trainTensor=None, testTensor=None, layers=None):
if layers is None:
layers = []
self.shape = shape
self.layers = layers
nd = len(self.shape)+1
if trainTensor is None:
self.trainTensor = T.TensorType(
dtype=theano.config.floatX, broadcastable=(False,)*nd
)('X_tr')
else:
self.trainTensor = trainTensor
if testTensor is None:
self.testTensor = T.TensorType(
dtype=theano.config.floatX, broadcastable=(False,)*nd
)('X_te')
else:
self.testTensor = testTensor
# Given an archiver object, reports the layers the rep has passed through.
def show(self, arc):
arc.log("")
arc.log("A representation having moved through:")
for layer in self.layers:
layer.show(arc) # }}}
# Actual Neural Network object.
class NN(object): # {{{
def __init__(self, inRep, outRep, archiver=None, # {{{
objective=None, optimiser=None, postBatchFunctions=None,
postEpochFunctions=None, classify=True):
if postBatchFunctions is None:
postBatchFunctions = []
if postEpochFunctions is None:
postEpochFunctions = []
if archiver is None:
archiver = Archiver()
self.testInTensor = inRep.testTensor
self.trainInTensor = inRep.trainTensor
self.testOutTensor = outRep.testTensor
self.trainOutTensor = outRep.trainTensor
self.classify = classify
self.labelTensor = T.matrix('y')
self.set(objective, optimiser)
self.trainCompiled = False
self.evaluateCompiled = False
self.observeCompiled = False
self.layers = outRep.layers
self.trainableWeights = []
self.depth = 0
self.numWeights = 0
ndict = {}
for layer in self.layers:
self.trainableWeights += layer.trainableWeights
self.depth += layer.depth
self.numWeights += layer.numWeights
if layer.name not in ndict:
ndict[layer.name] = 0
ndict[layer.name] += 1
layer.name += "_"+str(ndict[layer.name])
self.pbfs = postBatchFunctions
self.pefs = postEpochFunctions
self.LSUVFlag = False
self.LSUVdata = None
self.restarts = 0
self.observationLayers = []
self.observationFs = []
self.arc = archiver # }}}
def set(self, objective, optimiser): # {{{
if objective is not None:
self.objective = getWithName(objecD, objective)
if type(optimiser) is str:
self.optimiser = getWithName(optD, optimiser)() # passed the name
elif type(optimiser) is type and issubclass(optimiser, Optimiser):
self.optimiser = optimiser() # passed the class
elif optimiser is not None:
self.optimiser = optimiser # assumed to have passed an instance }}}
def compileTrain(self, objective=None, optimiser=None): # {{{
self.set(objective, optimiser)
self.trainLoss = self.objective(self.trainOutTensor, self.labelTensor)
for layer in self.layers:
for W in layer.regWeights:
self.trainLoss += 0.5 * layer.reg * layer.regFunction(W)
self.optimiser.compile(self.trainLoss)
self.updates = []
self.weightList = []
for layer in self.layers:
self.weightList += layer.trainableWeights
self.updates += layer.manualUpdates
self.updates += self.optimiser(self.weightList)
out = self.trainLoss
if self.classify:
_, accuracy = preds(self.trainOutTensor, self.labelTensor)
out = [out, accuracy]
self._trainOnBatch = theano.function(
[self.trainInTensor, self.labelTensor]
, out
, updates=self.updates
)
self.trainCompiled = True
def trainOnBatch(self, x, y):
if not self.trainCompiled:
self.compileTrain()
return self._trainOnBatch(x, y) # }}}
def compileEvaluate(self): # {{{
self.testLoss = self.objective(self.testOutTensor, self.labelTensor)
out = [self.testLoss]
if self.classify:
predictions, accuracy = preds(self.testOutTensor, self.labelTensor)
out += [predictions, accuracy]
else:
out += [self.testOutTensor]
self._eval = theano.function(
[self.testInTensor, self.labelTensor]
, out
)
self.evaluateCompiled = True
def evaluate(self, x, y, batchSize=64):
if not self.evaluateCompiled:
self.compileEvaluate()
numBatches = x.shape[0]/batchSize
batches = [
(x[i*batchSize:(i + 1)*batchSize], y[i*batchSize:(i + 1)*batchSize])
for i in range(numBatches)
]
if x.shape[0] % batchSize != 0:
batches.append((x[numBatches*batchSize:], y[numBatches*batchSize:]))
lS = []
aS = []
predS = []
for xBatch, yBatch in batches:
if self.classify:
l, pred, a = self._eval(xBatch, yBatch)
aS.append(xBatch.shape[0]*a)
else:
l, pred = self._eval(xBatch, yBatch)
lS.append(xBatch.shape[0]*l)
predS.append(pred)
l = sum(lS)/x.shape[0]
pred = np.concatenate(predS, axis=0)
if self.classify:
a = sum(aS)/x.shape[0]
return l, pred, a
else:
return l, pred # }}}
def trainForEpoch(self, x, y, batchSize, # {{{
validationData=None, verbosity=1, plotting=False, r=6):
# x, y can be numpy arrays wth data and labels respectively,
# or x can be a callable returning a batch: data, labels
# in which case y must specify the number of batches to perform,
# and x must accept the batchSize keyword argument.
if type(y) is int:
generating = True
batchIndices = range(y)
epochLen = y * batchSize
else:
generating = False
epochLen = x.shape[0]
inds = np.arange(epochLen)
np.random.shuffle(inds)
batchIndices = [inds[batchSize*i:batchSize*(i + 1)]
for i in range(epochLen / batchSize)]
if epochLen % batchSize != 0:
batchIndices += [inds[batchSize*(epochLen / batchSize):]]
loss = 0.
acc = 0.
a = 0.
if plotting:
losses = []
accs = []
t0 = time.time()
for i, binds in enumerate(batchIndices):
if generating:
xBatch, yBatch = x(batchSize=batchSize)
else:
xBatch = x[binds]
yBatch = y[binds]
if self.classify:
l, a = self.trainOnBatch(xBatch, yBatch)
else:
l = self.trainOnBatch(xBatch, yBatch)
if plotting:
losses.append(l)
accs.append(a)
loss = (i*loss + l) / (i + 1)
acc = (i*acc + a) / (i + 1)
tcur = time.time() - t0
if i == 0:
tproj = (epochLen / batchSize) * tcur
t0 = time.time()
else:
tproj = (epochLen / batchSize) * (tcur / i)
if verbosity == 1:
self.arc.log(
("(%d/%d) - Loss: " + floatToString(loss, r) + " - Acc: "
+ floatToString(acc, r) + " - T: %d/%ds "
) % (
min(batchSize*(i + 1), epochLen)
, epochLen
, int(tcur)
, int(tproj)
)
, eol='\r'
)
for f in self.pbfs:
f(model=self, l=l, a=a, avgLoss=loss, avgAcc=acc, t=tcur)
if np.isnan(l):
self.arc.log("")
self.arc.log("Loss is NaN; cancelling epoch.")
return None, None, [[], [], None, None]
tf = time.time()
report = "Loss: " + floatToString(loss, r)
report += " - Acc: " + floatToString(acc, r)
if validationData is not None:
vloss, _, vacc = self.evaluate(validationData[0], validationData[1])
report += " - V loss: " + floatToString(vloss, r)
report += " - V acc: " + floatToString(vacc, r)
else:
eplToken = "(%d/%d) - " % (epochLen, epochLen)
report = eplToken + report
tLen = max(4, 2*len(str(int(tf-t0))) + 1)
report += " - T: " + floatToString(tf-t0, tLen) + "s"
self.arc.log(report)
if plotting:
if validationData is None:
return loss, acc, [losses, accs]
else:
return loss, acc, [losses, accs, vloss, vacc]
else:
return loss, acc, [] # }}}
def schedule(self, x, y, scheduler, batchSize=64, # {{{
validationData=None, verbosity=1, plotting=False,
saveWeights=True, name='1', restartDecay=1.):
scheduler.set(optimiser=self.optimiser, archiver=self.arc)
if plotting:
ds = ['plots', name]
cuunter = 0
losses = []
accs = []
allEpochLs = []
allEpochAs = []
if validationData is not None:
vlosses = []
vaccs = []
if saveWeights:
self.arc.saveWeights(self, name + " - epoch 0 (init).h5")
loss = None
acc = None
epochLs = None
epochAs = None
vloss = None
vacc = None
while scheduler.epoch(loss=loss, acc=acc):
self.arc.log("")
self.arc.log("Epoch #%d, with learning rate %f and batch size %d"
% (scheduler.currentEpoch, scheduler.lr, batchSize))
loss, acc, l = self.trainForEpoch(x, y, batchSize,
validationData=validationData,
verbosity=verbosity, plotting=plotting)
if plotting:
losses.append(loss)
cuunter += 1
accs.append(acc)
epochLs = l[0]
epochAs = l[1]
allEpochLs.extend(epochLs)
allEpochAs.extend(epochAs)
pe_ds=['epochal data']
self.arc.plot([], [epochLs], name='Loss - Epoch %d'
% scheduler.currentEpoch, directoryStructure=ds+pe_ds)
self.arc.plot([], [epochAs], name='Accuracy - Epoch %d'
% scheduler.currentEpoch, directoryStructure=ds+pe_ds)
lossesL = [losses]
accsL = [accs]
if validationData is not None:
vloss = l[2]
vacc = l[3]
vlosses.append(vloss)
vaccs.append(vacc)
lossesL.append(vlosses)
accsL.append(vaccs)
self.arc.plot([], lossesL, name='Per Epoch Loss',
directoryStructure=ds)
self.arc.plot([], accsL, name='Per Epoch Accuracy',
directoryStructure=ds)
self.arc.plot([], [allEpochLs], name='Loss',
directoryStructure=ds)
self.arc.plot([], [allEpochAs], name='Accuracy',
directoryStructure=ds)
self.arc.pickle(
{'loss':loss, 'acc':acc, 'l':l}
, "epoch %d.b" % scheduler.currentEpoch
, directoryStructure=ds+pe_ds
)
for f in self.pefs:
restartFlag = f(model=self, epoch=scheduler.currentEpoch,
avgLoss=loss, avgAcc=acc, losses=epochLs,
accs=epochAs, valLoss=vloss, valAcc=vacc)
if restartFlag:
self.restarts += 1
self.arc.log("")
self.arc.log(
"Restarting for the "+intCounter(self.restarts)+" time."
)
self.reinit()
scheduler.refresh(lr=scheduler.lr*restartDecay)
return self.schedule(x, y, scheduler=scheduler,
batchSize=batchSize, validationData=validationData,
verbosity=verbosity, plotting=plotting,
saveWeights=saveWeights, name=name,
restartDecay=restartDecay)
if saveWeights:
self.arc.saveWeights(self,
name + " - epoch %d.h5" % scheduler.currentEpoch)
if validationData is not None:
return l[2], l[3]
else:
return loss, acc # }}}
def loadWeights(self, name): # {{{
f = h5py.File(name, 'r')
for layer in self.layers:
layer.loadWeights(f)
f.close() # }}}
def reinit(self, newArchiver=None): # {{{
if self.optimiser is not None:
self.optimiser.refresh()
for layer in self.layers:
layer.reinit()
if self.LSUVFlag:
self.LSUV(self.LSUVdata)
if newArchiver is not None:
self.arc = newArchiver # }}}
def show(self): # {{{
self.arc.log("")
self.arc.log("A depth %d Neural Net on %d parameters, with layers:" %
(self.depth, self.numWeights))
for layer in self.layers:
layer.show(self.arc) # }}}
def debugInit(self, x): # {{{
m, v = printStats(self.arc, x)
for i, layer in enumerate(self.layers):
self.arc.log("")
self.arc.log("The batch of reprentations is shape "+str(x.shape))
self.arc.log("Now passing through:")
layer.show(self.arc)
x = layer.debugCall(x)
m, v = printStats(self.arc, x) # }}}
def compileObservation(self): # {{{
previousTensor = self.trainInTensor
tlBuffer = []
for layer in self.layers:
if layer.name[:11] == "Observation":
self.observationLayers.append(layer)
f = theano.function([previousTensor], layer.tensor)
self.observationFs.append(f)
layer.setF(f)
layer.setPredecessors(tlBuffer)
tlBuffer = []
previousTensor = layer.tensor
elif layer.trainable:
tlBuffer.append(layer)
self.observeCompiled = True
def observe(self, x):
if not self.observeCompiled:
self.compileObservation()
self.arc.log("")
self.arc.log("The batch of reprentations is shape "+str(x.shape))
m, v = printStats(self.arc, x)
for layer in self.observationLayers:
x = layer.f(x)
self.arc.log("")
self.arc.log("The batch of reprentations is shape "+str(x.shape))
m, v = printStats(self.arc, x) # }}}
# {{{ Not quite LSUV as originally proposed, but closely based on.
# Passes one batch to start, making the required corrections to the weights.
# Next pass, it passes twice as many batches though at the same time,
# applying the (geometric) mean of their corrective factors.
# The variance is measured at observation points that are inserted by using
# Observation layers in the model, and the corrective factors are
# distributed over all trainable layers in between the observation points.
def LSUV(self, data, batchSize=256, debug=False, passes=5):
if not self.observeCompiled:
self.compileObservation()
for j in range(passes):
inds = np.arange(data.shape[0]); np.random.shuffle(inds)
xs = [data[inds[batchSize*i:batchSize*(i + 1)]] for i in range(2**j)]
for layer in self.observationLayers:
vs = []
for x in xs:
xtmp = layer.f(x)
m = xtmp.mean()
v = ((xtmp-m)**2).mean()
if v > 10**(-8):
vs.append(v)
std = pow(geoMean(vs), 1./(2*len(layer.predecessors)))
std += 10**(-6)
for pred in layer.predecessors:
for W in pred.trainableWeights:
W.set_value(W.get_value()/std)
for i, x in enumerate(xs):
xs[i] = layer.f(x)
if debug:
inds = np.random.random_integers(size=(batchSize,), low=0,
high=(data.shape[0]-1))
self.arc.log("")
self.arc.log("Dry run with fresh batch:")
self.observe(data[inds])
self.LSUVdata = data
self.LSUVFlag = True # }}}
# }}}