-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathbonsaiTrainer.py
More file actions
828 lines (706 loc) · 34.3 KB
/
Copy pathbonsaiTrainer.py
File metadata and controls
828 lines (706 loc) · 34.3 KB
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT license.
from __future__ import print_function
import tensorflow as tf
import edgeml_tf.utils as utils
import numpy as np
import os
import sys
class BonsaiTrainer:
def __init__(self, bonsaiObj, lW, lT, lV, lZ, sW, sT, sV, sZ,
learningRate, X, Y, useMCHLoss=False, outFile=None, regLoss='huber', errorReg='mae'):
'''
bonsaiObj - Initialised Bonsai Object and Graph
lW, lT, lV and lZ are regularisers to Bonsai Params
sW, sT, sV and sZ are sparsity factors to Bonsai Params
learningRate - learningRate fro optimizer
X is the Data Placeholder - Dims [_, dataDimension]
Y - Label placeholder for loss computation
useMCHLoss - For choice between HingeLoss vs CrossEntropy
useMCHLoss - True - MultiClass - multiClassHingeLoss
useMCHLoss - False - MultiClass - crossEntropyLoss
'''
self.bonsaiObj = bonsaiObj
self.regressionLoss = regLoss
self.lW = lW
self.lV = lV
self.lT = lT
self.lZ = lZ
self.sW = sW
self.sV = sV
self.sT = sT
self.sZ = sZ
self.Y = Y
self.X = X
self.useMCHLoss = useMCHLoss
if outFile is not None:
print("Outfile : ", outFile)
self.outFile = open(outFile, 'w')
else:
self.outFile = sys.stdout
self.learningRate = learningRate
self.assertInit()
self.sigmaI = tf.placeholder(tf.float32, name='sigmaI')
self.score, self.X_ = self.bonsaiObj(self.X, self.sigmaI)
self.loss, self.marginLoss, self.regLoss = self.lossGraph()
self.trainStep = self.trainGraph()
'''
self.accuracy -> 'MAE' for Regression.
self.accuracy -> 'Accuracy' for Classification.
'''
self.accuracy = self.accuracyGraph(errorReg=errorReg)
self.prediction = self.bonsaiObj.getPrediction()
if self.sW > 0.99 and self.sV > 0.99 and self.sZ > 0.99 and self.sT > 0.99:
self.isDenseTraining = True
else:
self.isDenseTraining = False
self.hardThrsd()
self.sparseTraining()
def lossGraph(self):
'''
Loss Graph for given Bonsai Obj
'''
self.regLoss = 0.5 * (self.lZ * tf.square(tf.norm(self.bonsaiObj.Z)) +
self.lW * tf.square(tf.norm(self.bonsaiObj.W)) +
self.lV * tf.square(tf.norm(self.bonsaiObj.V)) +
self.lT * tf.square(tf.norm(self.bonsaiObj.T)))
# Loss functions for classification.
if (self.bonsaiObj.isRegression is False):
if (self.bonsaiObj.numClasses > 2):
if self.useMCHLoss is True:
self.batch_th = tf.placeholder(tf.int64, name='batch_th')
self.marginLoss = utils.multiClassHingeLoss(
tf.transpose(self.score), self.Y,
self.batch_th)
else:
self.marginLoss = utils.crossEntropyLoss(
tf.transpose(self.score), self.Y)
self.loss = self.marginLoss + self.regLoss
else:
self.marginLoss = tf.reduce_mean(tf.nn.relu(
1.0 - (2 * self.Y - 1) * tf.transpose(self.score)))
self.loss = self.marginLoss + self.regLoss
# Loss functions for regression.
elif (self.bonsaiObj.isRegression is True):
if(self.regressionLoss == 'huber'):
# Use of Huber Loss , because it is more robust to outliers.
self.marginLoss = tf.losses.huber_loss(
self.Y, tf.transpose(self.score))
self.loss = self.marginLoss + self.regLoss
elif (self.regressionLoss == 'l2'):
# L2 loss function.
self.marginLoss = tf.nn.l2_loss(
self.Y - tf.transpose(self.score))
self.loss = self.marginLoss + self.regLoss
return self.loss, self.marginLoss, self.regLoss
def trainGraph(self):
'''
Train Graph for the loss generated by Bonsai
'''
self.bonsaiObj.TrainStep = tf.train.AdamOptimizer(
self.learningRate).minimize(self.loss)
return self.bonsaiObj.TrainStep
def accuracyGraph(self, errorReg='mae'):
'''
Accuracy Graph to evaluate accuracy when needed
'''
if(self.bonsaiObj.isRegression is False):
if (self.bonsaiObj.numClasses > 2):
correctPrediction = tf.equal(
tf.argmax(tf.transpose(self.score), 1), tf.argmax(self.Y, 1))
self.accuracy = tf.reduce_mean(
tf.cast(correctPrediction, tf.float32))
else:
y_ = self.Y * 2 - 1
correctPrediction = tf.multiply(tf.transpose(self.score), y_)
correctPrediction = tf.nn.relu(correctPrediction)
correctPrediction = tf.ceil(tf.tanh(correctPrediction))
self.accuracy = tf.reduce_mean(
tf.cast(correctPrediction, tf.float32))
elif (self.bonsaiObj.isRegression is True):
# Accuracy for regression , in terms of mean absolute error.
if(errorReg == 'mse'):
self.accuracy = utils.mean_squared_error(tf.reshape(
self.score, [-1, 1]), tf.reshape(self.Y, [-1, 1]))
else:
self.accuracy = utils.mean_absolute_error(tf.reshape(
self.score, [-1, 1]), tf.reshape(self.Y, [-1, 1]))
return self.accuracy
def hardThrsd(self):
'''
Set up for hard Thresholding Functionality
'''
self.__Wth = tf.placeholder(tf.float32, name='Wth')
self.__Vth = tf.placeholder(tf.float32, name='Vth')
self.__Zth = tf.placeholder(tf.float32, name='Zth')
self.__Tth = tf.placeholder(tf.float32, name='Tth')
self.__Woph = self.bonsaiObj.W.assign(self.__Wth)
self.__Voph = self.bonsaiObj.V.assign(self.__Vth)
self.__Toph = self.bonsaiObj.T.assign(self.__Tth)
self.__Zoph = self.bonsaiObj.Z.assign(self.__Zth)
self.hardThresholdGroup = tf.group(
self.__Woph, self.__Voph, self.__Toph, self.__Zoph)
def sparseTraining(self):
'''
Set up for Sparse Retraining Functionality
'''
self.__Wops = self.bonsaiObj.W.assign(self.__Wth)
self.__Vops = self.bonsaiObj.V.assign(self.__Vth)
self.__Zops = self.bonsaiObj.Z.assign(self.__Zth)
self.__Tops = self.bonsaiObj.T.assign(self.__Tth)
self.sparseRetrainGroup = tf.group(
self.__Wops, self.__Vops, self.__Tops, self.__Zops)
def runHardThrsd(self, sess):
'''
Function to run the IHT routine on Bonsai Obj
'''
currW = self.bonsaiObj.W.eval()
currV = self.bonsaiObj.V.eval()
currZ = self.bonsaiObj.Z.eval()
currT = self.bonsaiObj.T.eval()
self.__thrsdW = utils.hardThreshold(currW, self.sW)
self.__thrsdV = utils.hardThreshold(currV, self.sV)
self.__thrsdZ = utils.hardThreshold(currZ, self.sZ)
self.__thrsdT = utils.hardThreshold(currT, self.sT)
fd_thrsd = {self.__Wth: self.__thrsdW, self.__Vth: self.__thrsdV,
self.__Zth: self.__thrsdZ, self.__Tth: self.__thrsdT}
sess.run(self.hardThresholdGroup, feed_dict=fd_thrsd)
def runSparseTraining(self, sess):
'''
Function to run the Sparse Retraining routine on Bonsai Obj
'''
currW = self.bonsaiObj.W.eval()
currV = self.bonsaiObj.V.eval()
currZ = self.bonsaiObj.Z.eval()
currT = self.bonsaiObj.T.eval()
newW = utils.copySupport(self.__thrsdW, currW)
newV = utils.copySupport(self.__thrsdV, currV)
newZ = utils.copySupport(self.__thrsdZ, currZ)
newT = utils.copySupport(self.__thrsdT, currT)
fd_st = {self.__Wth: newW, self.__Vth: newV,
self.__Zth: newZ, self.__Tth: newT}
sess.run(self.sparseRetrainGroup, feed_dict=fd_st)
def assertInit(self):
err = "sparsity must be between 0 and 1"
assert self.sW >= 0 and self.sW <= 1, "W " + err
assert self.sV >= 0 and self.sV <= 1, "V " + err
assert self.sZ >= 0 and self.sZ <= 1, "Z " + err
assert self.sT >= 0 and self.sT <= 1, "T " + err
errMsg = "Dimension Mismatch, Y has to be [_, " + \
str(self.bonsaiObj.numClasses) + "]"
errCont = " numClasses are 1 in case of Binary case by design"
assert (len(self.Y.shape) == 2 and
self.Y.shape[1] == self.bonsaiObj.numClasses), errMsg + errCont
def saveParams(self, currDir):
'''
Function to save Parameter matrices into a given folder
'''
paramDir = currDir + '/'
np.save(paramDir + "W.npy", self.bonsaiObj.W.eval())
np.save(paramDir + "V.npy", self.bonsaiObj.V.eval())
np.save(paramDir + "T.npy", self.bonsaiObj.T.eval())
np.save(paramDir + "Z.npy", self.bonsaiObj.Z.eval())
hyperParamDict = {'dataDim': self.bonsaiObj.dataDimension,
'projDim': self.bonsaiObj.projectionDimension,
'numClasses': self.bonsaiObj.numClasses,
'depth': self.bonsaiObj.treeDepth,
'sigma': self.bonsaiObj.sigma}
hyperParamFile = paramDir + 'hyperParam.npy'
np.save(hyperParamFile, hyperParamDict)
def saveParamsForSeeDot(self, currDir):
'''
Function to save Parameter matrices into a given folder for SeeDot compiler
'''
seeDotDir = currDir + '/SeeDot/'
if os.path.isdir(seeDotDir) is False:
try:
os.mkdir(seeDotDir)
except OSError:
print("Creation of the directory %s failed" %
seeDotDir)
np.savetxt(seeDotDir + "W",
utils.restructreMatrixBonsaiSeeDot(self.bonsaiObj.W.eval(),
self.bonsaiObj.numClasses,
self.bonsaiObj.totalNodes),
delimiter="\t")
np.savetxt(seeDotDir + "V",
utils.restructreMatrixBonsaiSeeDot(self.bonsaiObj.V.eval(),
self.bonsaiObj.numClasses,
self.bonsaiObj.totalNodes),
delimiter="\t")
np.savetxt(seeDotDir + "T", self.bonsaiObj.T.eval(), delimiter="\t")
np.savetxt(seeDotDir + "Z", self.bonsaiObj.Z.eval(), delimiter="\t")
np.savetxt(seeDotDir + "Sigma",
np.array([self.bonsaiObj.sigma]), delimiter="\t")
def loadModel(self, currDir):
'''
Load the Saved model and load it to the model using constructor
Returns two dict one for params and other for hyperParams
'''
paramDir = currDir + '/'
paramDict = {}
paramDict['W'] = np.load(paramDir + "W.npy")
paramDict['V'] = np.load(paramDir + "V.npy")
paramDict['T'] = np.load(paramDir + "T.npy")
paramDict['Z'] = np.load(paramDir + "Z.npy")
hyperParamDict = np.load(paramDir + "hyperParam.npy").item()
return paramDict, hyperParamDict
# Function to get aimed model size
def getModelSize(self):
'''
Function to get aimed model size
'''
nnzZ, sizeZ, sparseZ = utils.countnnZ(self.bonsaiObj.Z, self.sZ)
nnzW, sizeW, sparseW = utils.countnnZ(self.bonsaiObj.W, self.sW)
nnzV, sizeV, sparseV = utils.countnnZ(self.bonsaiObj.V, self.sV)
nnzT, sizeT, sparseT = utils.countnnZ(self.bonsaiObj.T, self.sT)
totalnnZ = (nnzZ + nnzT + nnzV + nnzW)
totalSize = (sizeZ + sizeW + sizeV + sizeT)
hasSparse = (sparseW or sparseV or sparseT or sparseZ)
return totalnnZ, totalSize, hasSparse
def train(self, batchSize, totalEpochs, sess,
Xtrain, Xtest, Ytrain, Ytest, dataDir, currDir):
'''
The Dense - IHT - Sparse Retrain Routine for Bonsai Training
'''
resultFile = open(dataDir + '/TFBonsaiResults.txt', 'a+')
numIters = Xtrain.shape[0] / batchSize
totalBatches = numIters * totalEpochs
bonsaiObjSigmaI = 1
counter = 0
if self.bonsaiObj.numClasses > 2:
trimlevel = 15
else:
trimlevel = 5
ihtDone = 0
if (self.bonsaiObj.isRegression is True):
maxTestAcc = 100000007
else:
maxTestAcc = -10000
if self.isDenseTraining is True:
ihtDone = 1
bonsaiObjSigmaI = 1
itersInPhase = 0
header = '*' * 20
for i in range(totalEpochs):
print("\nEpoch Number: " + str(i), file=self.outFile)
'''
trainAcc -> For Regression, it is 'Mean Absolute Error'.
trainAcc -> For Classification, it is 'Accuracy'.
'''
trainAcc = 0.0
trainLoss = 0.0
numIters = int(numIters)
for j in range(numIters):
if counter == 0:
msg = " Dense Training Phase Started "
print("\n%s%s%s\n" %
(header, msg, header), file=self.outFile)
# Updating the indicator sigma
if ((counter == 0) or (counter == int(totalBatches / 3.0)) or
(counter == int(2 * totalBatches / 3.0))) and (self.isDenseTraining is False):
bonsaiObjSigmaI = 1
itersInPhase = 0
elif (itersInPhase % 100 == 0):
indices = np.random.choice(Xtrain.shape[0], 100)
batchX = Xtrain[indices, :]
batchY = Ytrain[indices, :]
batchY = np.reshape(
batchY, [-1, self.bonsaiObj.numClasses])
_feed_dict = {self.X: batchX}
Xcapeval = self.X_.eval(feed_dict=_feed_dict)
Teval = self.bonsaiObj.T.eval()
sum_tr = 0.0
for k in range(0, self.bonsaiObj.internalNodes):
sum_tr += (np.sum(np.abs(np.dot(Teval[k], Xcapeval))))
if(self.bonsaiObj.internalNodes > 0):
sum_tr /= (100 * self.bonsaiObj.internalNodes)
sum_tr = 0.1 / sum_tr
else:
sum_tr = 0.1
sum_tr = min(
1000, sum_tr * (2**(float(itersInPhase) /
(float(totalBatches) / 30.0))))
bonsaiObjSigmaI = sum_tr
itersInPhase += 1
batchX = Xtrain[j * batchSize:(j + 1) * batchSize]
batchY = Ytrain[j * batchSize:(j + 1) * batchSize]
batchY = np.reshape(
batchY, [-1, self.bonsaiObj.numClasses])
if self.bonsaiObj.numClasses > 2:
if self.useMCHLoss is True:
_feed_dict = {self.X: batchX, self.Y: batchY,
self.batch_th: batchY.shape[0],
self.sigmaI: bonsaiObjSigmaI}
else:
_feed_dict = {self.X: batchX, self.Y: batchY,
self.sigmaI: bonsaiObjSigmaI}
else:
_feed_dict = {self.X: batchX, self.Y: batchY,
self.sigmaI: bonsaiObjSigmaI}
# Mini-batch training
_, batchLoss, batchAcc = sess.run(
[self.trainStep, self.loss, self.accuracy],
feed_dict=_feed_dict)
# Classification.
if (self.bonsaiObj.isRegression is False):
trainAcc += batchAcc
trainLoss += batchLoss
# Regression.
else:
trainAcc += np.mean(batchAcc)
trainLoss += np.mean(batchLoss)
# Training routine involving IHT and sparse retraining
if (counter >= int(totalBatches / 3.0) and
(counter < int(2 * totalBatches / 3.0)) and
counter % trimlevel == 0 and
self.isDenseTraining is False):
self.runHardThrsd(sess)
if ihtDone == 0:
msg = " IHT Phase Started "
print("\n%s%s%s\n" %
(header, msg, header), file=self.outFile)
ihtDone = 1
elif ((ihtDone == 1 and counter >= int(totalBatches / 3.0) and
(counter < int(2 * totalBatches / 3.0)) and
counter % trimlevel != 0 and
self.isDenseTraining is False) or
(counter >= int(2 * totalBatches / 3.0) and
self.isDenseTraining is False)):
self.runSparseTraining(sess)
if counter == int(2 * totalBatches / 3.0):
msg = " Sparse Retraining Phase Started "
print("\n%s%s%s\n" %
(header, msg, header), file=self.outFile)
counter += 1
try:
if (self.bonsaiObj.isRegression is True):
print("\nRegression Train Loss: " + str(trainLoss / numIters) +
"\nTraining MAE (Regression): " +
str(trainAcc / numIters),
file=self.outFile)
else:
print("\nClassification Train Loss: " + str(trainLoss / numIters) +
"\nTraining accuracy (Classification): " +
str(trainAcc / numIters),
file=self.outFile)
except:
continue
oldSigmaI = bonsaiObjSigmaI
bonsaiObjSigmaI = 1e9
if self.bonsaiObj.numClasses > 2:
if self.useMCHLoss is True:
_feed_dict = {self.X: Xtest, self.Y: Ytest,
self.batch_th: Ytest.shape[0],
self.sigmaI: bonsaiObjSigmaI}
else:
_feed_dict = {self.X: Xtest, self.Y: Ytest,
self.sigmaI: bonsaiObjSigmaI}
else:
_feed_dict = {self.X: Xtest, self.Y: Ytest,
self.sigmaI: bonsaiObjSigmaI}
# This helps in direct testing instead of extracting the model out
testAcc, testLoss, regTestLoss, pred = sess.run(
[self.accuracy, self.loss, self.regLoss, self.prediction], feed_dict=_feed_dict)
if ihtDone == 0:
if (self.bonsaiObj.isRegression is False):
maxTestAcc = -10000
maxTestAccEpoch = i
elif (self.bonsaiObj.isRegression is True):
maxTestAcc = testAcc
maxTestAccEpoch = i
else:
if (self.bonsaiObj.isRegression is False):
if maxTestAcc <= testAcc:
maxTestAccEpoch = i
maxTestAcc = testAcc
self.saveParams(currDir)
self.saveParamsForSeeDot(currDir)
elif (self.bonsaiObj.isRegression is True):
print("Minimum Training MAE : ", np.mean(maxTestAcc))
if maxTestAcc >= testAcc:
# For regression , we're more interested in the minimum
# MAE.
maxTestAccEpoch = i
maxTestAcc = testAcc
self.saveParams(currDir)
self.saveParamsForSeeDot(currDir)
if (self.bonsaiObj.isRegression is True):
print("Testing MAE %g" % np.mean(testAcc), file=self.outFile)
else:
print("Test accuracy %g" % np.mean(testAcc), file=self.outFile)
if (self.bonsaiObj.isRegression is True):
testAcc = np.mean(testAcc)
else:
testAcc = testAcc
maxTestAcc = maxTestAcc
print("MarginLoss + RegLoss: " + str(testLoss - regTestLoss) +
" + " + str(regTestLoss) + " = " + str(testLoss) + "\n",
file=self.outFile)
self.outFile.flush()
bonsaiObjSigmaI = oldSigmaI
# sigmaI has to be set to infinity to ensure
# only a single path is used in inference
bonsaiObjSigmaI = 1e9
print("\nNon-Zero : " + str(self.getModelSize()[0]) + " Model Size: " +
str(float(self.getModelSize()[1]) / 1024.0) + " KB hasSparse: " +
str(self.getModelSize()[2]) + "\n", file=self.outFile)
if (self.bonsaiObj.isRegression is True):
maxTestAcc = np.mean(maxTestAcc)
if (self.bonsaiObj.isRegression is True):
print("For Regression, Minimum MAE at compressed" +
" model size(including early stopping): " +
str(maxTestAcc) + " at Epoch: " +
str(maxTestAccEpoch + 1) + "\nFinal Test" +
" MAE: " + str(testAcc), file=self.outFile)
resultFile.write("MinTestMAE: " + str(maxTestAcc) +
" at Epoch(totalEpochs): " +
str(maxTestAccEpoch + 1) +
"(" + str(totalEpochs) + ")" + " ModelSize: " +
str(float(self.getModelSize()[1]) / 1024.0) +
" KB hasSparse: " + str(self.getModelSize()[2]) +
" Param Directory: " +
str(os.path.abspath(currDir)) + "\n")
elif (self.bonsaiObj.isRegression is False):
print("For Classification, Maximum Test accuracy at compressed" +
" model size(including early stopping): " +
str(maxTestAcc) + " at Epoch: " +
str(maxTestAccEpoch + 1) + "\nFinal Test" +
" Accuracy: " + str(testAcc), file=self.outFile)
resultFile.write("MaxTestAcc: " + str(maxTestAcc) +
" at Epoch(totalEpochs): " +
str(maxTestAccEpoch + 1) +
"(" + str(totalEpochs) + ")" + " ModelSize: " +
str(float(self.getModelSize()[1]) / 1024.0) +
" KB hasSparse: " + str(self.getModelSize()[2]) +
" Param Directory: " +
str(os.path.abspath(currDir)) + "\n")
print("The Model Directory: " + currDir + "\n")
resultFile.close()
self.outFile.flush()
if self.outFile is not sys.stdout:
self.outFile.close()
def weighted_stochastic_train(self, batchSize, totalEpochs, sess, Xtrain, Xtest, Ytrain, Ytest, dataDir, currDir):
'''
Changes comapared to original
-Stochastic gradient descent implementation to reduce training time for weighted Bonsai Trees.
-For Training regression Mean squared error used.
-Weighted bonsai node predictor used
'''
resultFile = open(dataDir + '/TFBonsaiResults.txt', 'a+')
numIters = Xtrain.shape[0] / batchSize
totalBatches = numIters * totalEpochs
bonsaiObjSigmaI = 1
counter = 0
if self.bonsaiObj.numClasses > 2:
trimlevel = 15
else:
trimlevel = 5
ihtDone = 0
if (self.bonsaiObj.isRegression is True):
maxTestAcc = 100000007
else:
maxTestAcc = -10000
if self.isDenseTraining is True:
ihtDone = 1
bonsaiObjSigmaI = 1
itersInPhase = 0
header = '*' * 20
for i in range(totalEpochs):
print("\nEpoch Number: " + str(i), file=self.outFile)
'''
trainAcc -> For Regression, it is 'Mean Squared Error'.
trainAcc -> For Classification, it is 'Accuracy'.
'''
trainAcc = 0.0
trainLoss = 0.0
numIters = int(numIters)
for j in range(numIters):
if counter == 0:
msg = " Dense Training Phase Started "
print("\n%s%s%s\n" %
(header, msg, header), file=self.outFile)
# Updating the indicator sigma
if ((counter == 0) or (counter == int(totalBatches / 3.0)) or
(counter == int(2 * totalBatches / 3.0))) and (self.isDenseTraining is False):
bonsaiObjSigmaI = 1
itersInPhase = 0
elif (itersInPhase % 100 == 0):
indices = np.random.choice(Xtrain.shape[0], 100)
batchX = Xtrain[indices, :]
batchY = Ytrain[indices, :]
batchY = np.reshape(
batchY, [-1, self.bonsaiObj.numClasses])
_feed_dict = {self.X: batchX}
Xcapeval = self.X_.eval(feed_dict=_feed_dict)
Teval = self.bonsaiObj.T.eval()
sum_tr = 0.0
for k in range(0, self.bonsaiObj.internalNodes):
sum_tr += (np.sum(np.abs(np.dot(Teval[k], Xcapeval))))
if(self.bonsaiObj.internalNodes > 0):
sum_tr /= (100 * self.bonsaiObj.internalNodes)
sum_tr = 0.1 / sum_tr
else:
sum_tr = 0.1
sum_tr = min(
1000, sum_tr * (2**(float(itersInPhase) /
(float(totalBatches) / 30.0))))
bonsaiObjSigmaI = sum_tr
itersInPhase += 1
batchX = Xtrain[j * batchSize:(j + 1) * batchSize]
batchY = Ytrain[j * batchSize:(j + 1) * batchSize]
batchY = np.reshape(
batchY, [-1, self.bonsaiObj.numClasses])
if self.bonsaiObj.numClasses > 2:
if self.useMCHLoss is True:
_feed_dict = {self.X: batchX, self.Y: batchY,
self.batch_th: batchY.shape[0],
self.sigmaI: bonsaiObjSigmaI}
else:
_feed_dict = {self.X: batchX, self.Y: batchY,
self.sigmaI: bonsaiObjSigmaI}
else:
_feed_dict = {self.X: batchX, self.Y: batchY,
self.sigmaI: bonsaiObjSigmaI}
# Stochastic training
_, batchLoss, batchAcc = sess.run(
[1, self.loss, self.accuracy],
feed_dict=_feed_dict)
# Classification.
if (self.bonsaiObj.isRegression is False):
trainAcc += batchAcc
trainLoss += batchLoss
# Regression.
else:
trainAcc += np.mean(batchAcc)
trainLoss += np.mean(batchLoss)
# Training routine involving IHT and sparse retraining
if (counter >= int(totalBatches / 3.0) and
(counter < int(2 * totalBatches / 3.0)) and
counter % trimlevel == 0 and
self.isDenseTraining is False):
self.runHardThrsd(sess)
if ihtDone == 0:
msg = " IHT Phase Started "
print("\n%s%s%s\n" %
(header, msg, header), file=self.outFile)
ihtDone = 1
elif ((ihtDone == 1 and counter >= int(totalBatches / 3.0) and
(counter < int(2 * totalBatches / 3.0)) and
counter % trimlevel != 0 and
self.isDenseTraining is False) or
(counter >= int(2 * totalBatches / 3.0) and
self.isDenseTraining is False)):
self.runSparseTraining(sess)
if counter == int(2 * totalBatches / 3.0):
msg = " Sparse Retraining Phase Started "
print("\n%s%s%s\n" %
(header, msg, header), file=self.outFile)
counter += 1
try:
if (self.bonsaiObj.isRegression is True):
print("\nRegression Train Loss: " + str(trainLoss / numIters) +
"\nTraining MSE (Regression): " +
str(trainAcc / numIters),
file=self.outFile)
else:
print("\nClassification Train Loss: " + str(trainLoss / numIters) +
"\nTraining accuracy (Classification): " +
str(trainAcc / numIters),
file=self.outFile)
except:
continue
oldSigmaI = bonsaiObjSigmaI
bonsaiObjSigmaI = 1e9
if self.bonsaiObj.numClasses > 2:
if self.useMCHLoss is True:
_feed_dict = {self.X: Xtest, self.Y: Ytest,
self.batch_th: Ytest.shape[0],
self.sigmaI: bonsaiObjSigmaI}
else:
_feed_dict = {self.X: Xtest, self.Y: Ytest,
self.sigmaI: bonsaiObjSigmaI}
else:
_feed_dict = {self.X: Xtest, self.Y: Ytest,
self.sigmaI: bonsaiObjSigmaI}
# This helps in direct testing instead of extracting the model out
testAcc, testLoss, regTestLoss, pred = sess.run(
[self.accuracy, self.loss, self.regLoss, self.prediction], feed_dict=_feed_dict)
if ihtDone == 0:
if (self.bonsaiObj.isRegression is False):
maxTestAcc = -10000
maxTestAccEpoch = i
elif (self.bonsaiObj.isRegression is True):
maxTestAcc = testAcc
maxTestAccEpoch = i
else:
if (self.bonsaiObj.isRegression is False):
if maxTestAcc <= testAcc:
maxTestAccEpoch = i
maxTestAcc = testAcc
self.saveParams(currDir)
self.saveParamsForSeeDot(currDir)
elif (self.bonsaiObj.isRegression is True):
print("Minimum Training MSE : ", np.mean(maxTestAcc))
if maxTestAcc >= testAcc:
# For regression , we're more interested in the minimum
# MSE.
maxTestAccEpoch = i
maxTestAcc = testAcc
self.saveParams(currDir)
self.saveParamsForSeeDot(currDir)
if (self.bonsaiObj.isRegression is True):
print("Testing MSE %g" % np.mean(testAcc), file=self.outFile)
else:
print("Test accuracy %g" % np.mean(testAcc), file=self.outFile)
if (self.bonsaiObj.isRegression is True):
testAcc = np.mean(testAcc)
else:
testAcc = testAcc
maxTestAcc = maxTestAcc
print("MarginLoss + RegLoss: " + str(testLoss - regTestLoss) +
" + " + str(regTestLoss) + " = " + str(testLoss) + "\n",
file=self.outFile)
self.outFile.flush()
bonsaiObjSigmaI = oldSigmaI
# sigmaI has to be set to infinity to ensure
# only a single path is used in inference
bonsaiObjSigmaI = 1e9
print("\nNon-Zero : " + str(self.getModelSize()[0]) + " Model Size: " +
str(float(self.getModelSize()[1]) / 1024.0) + " KB hasSparse: " +
str(self.getModelSize()[2]) + "\n", file=self.outFile)
if (self.bonsaiObj.isRegression is True):
maxTestAcc = np.mean(maxTestAcc)
if (self.bonsaiObj.isRegression is True):
print("For Regression, Minimum MSE at compressed" +
" model size(including early stopping): " +
str(maxTestAcc) + " at Epoch: " +
str(maxTestAccEpoch + 1) + "\nFinal Test" +
" MAE: " + str(testAcc), file=self.outFile)
resultFile.write("MinTestMSE: " + str(maxTestAcc) +
" at Epoch(totalEpochs): " +
str(maxTestAccEpoch + 1) +
"(" + str(totalEpochs) + ")" + " ModelSize: " +
str(float(self.getModelSize()[1]) / 1024.0) +
" KB hasSparse: " + str(self.getModelSize()[2]) +
" Param Directory: " +
str(os.path.abspath(currDir)) + "\n")
elif (self.bonsaiObj.isRegression is False):
print("For Classification, Maximum Test accuracy at compressed" +
" model size(including early stopping): " +
str(maxTestAcc) + " at Epoch: " +
str(maxTestAccEpoch + 1) + "\nFinal Test" +
" Accuracy: " + str(testAcc), file=self.outFile)
resultFile.write("MaxTestAcc: " + str(maxTestAcc) +
" at Epoch(totalEpochs): " +
str(maxTestAccEpoch + 1) +
"(" + str(totalEpochs) + ")" + " ModelSize: " +
str(float(self.getModelSize()[1]) / 1024.0) +
" KB hasSparse: " + str(self.getModelSize()[2]) +
" Param Directory: " +
str(os.path.abspath(currDir)) + "\n")
print("The Model Directory: " + currDir + "\n")
resultFile.close()
self.outFile.flush()
if self.outFile is not sys.stdout:
self.outFile.close()