-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRKNet.py
More file actions
580 lines (446 loc) · 21.8 KB
/
RKNet.py
File metadata and controls
580 lines (446 loc) · 21.8 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
# RKNet.py
from utils import *
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.init as init
import torchvision
import torchvision.transforms as transforms
import torch.optim as optim
from torch.autograd import Variable
import time
import datetime
import math
import copy
import os
import sys
from modules.ConnectingLayer import ConnectingLayer
from modules.rk4 import rk4
from modules.rk1 import rk1
from modules.TvNorm import TvNorm
from modules.DoubleSymLayer import DoubleSymLayer
from modules.DoubleLayer import DoubleLayer
from modules.ClippedModule import *
class RKNet(ClippedModule):
def __init__(self, tY, tTheta, nChanIn, nClasses, vFeat, dynamicScheme=rk4, layer=DoubleSymLayer,
openParams=None, dynamicParams=None, connParams=None, linear=None):
"""
The main workhorse network. Consists of an opening layer (a single layer), multiple dynamicBlocks each
of which is connected by a ConnectingLayer (a single layer), then
:param tY: list, time discretization for the states , [0,1,2,3,4]
:param tTheta: list, time sdiscretization for the controls, [0,1,2,3,4]
:param nChanIn: int, number of input channels (RGB = 3)
:param nClasses: int, number of output classes
:param vFeat: list, number of channels per dynamic block...last value for final connecting layer [16,32,64,64]
:param dynamicScheme: module, the time integrator / ODE solver (ex: rk1, rk4)
:param layer: module, the primary layer in the dynamic blocks (ex: DoubleLayer, DoubleSymLayer)
:param openParams: dict, the params dict to pass to the opening layer
:param dynamicParams: dict, the params dict to pass to the dynamic scheme
also, can pass strings "TvNorm", "NoNorm", or "Batch" for the corresponding norm with the default
:param connParams: dict, the params dict to pass to the connecting layers
:param linear: module, a nn.Linear module already initialized to be used as final fully connected layer
"""
super().__init__()
self.nBlocks = len(vFeat) - 1
self.vFeat = vFeat
self.tY = tY
self.tTheta = tTheta
if dynamicParams is None or dynamicParams=="TvNorm" or dynamicParams=="Batch" or dynamicParams=="NoNorm":
dynamicParams = [dynamicParams] * self.nBlocks
if connParams is None:
connParams = [None] * self.nBlocks
self.dynamicBlocks = nn.ModuleList([])
self.connectors = nn.ModuleList([])
act = nn.ReLU()
if openParams is None:
openParams = { 'act': act,
'normLayer': nn.BatchNorm2d(num_features=vFeat[0], eps=1e-4),
'conv': nn.Conv2d(nChanIn, vFeat[0], kernel_size=3, padding=1, stride=1)}
self.open = ConnectingLayer([nChanIn, vFeat[0]], params=openParams)
self.dynamicBlocks = nn.ModuleList([])
self.connectors = nn.ModuleList([])
for blk in range(self.nBlocks):
if dynamicParams[blk] is None or dynamicParams[blk]=="TvNorm":
# default setup for the dynamic block
dynamicParams[blk] = {'act': act,
'vFeat' : vFeat[blk],
'normLayer': TvNorm(vFeat[blk], eps=1e-4)}
elif dynamicParams[blk]=="Batch":
dynamicParams[blk] = {'act': act,
'vFeat': vFeat[blk],
'normLayer': nn.BatchNorm2d(vFeat[blk])}
elif dynamicParams[blk]=="NoNorm":
dynamicParams[blk] = {'act': act,
'vFeat': vFeat[blk]}
self.dynamicBlocks.append(dynamicScheme(tTheta, tY, layer, layerParams = dynamicParams[blk] ))
if connParams[blk] is None:
connParams[blk] = {'act': act,
'normLayer': nn.BatchNorm2d(num_features=vFeat[blk+1], eps=1e-4),
'conv': nn.Conv2d(vFeat[blk], vFeat[blk+1], kernel_size=1, padding=0, stride=1)}
connLayer = ConnectingLayer( [vFeat[blk],vFeat[blk+1]] , params = connParams[blk])
self.connectors.append(connLayer)
if linear is None:
self.linear = nn.Linear(vFeat[-1], nClasses)
self.linear.weight.data = normalInit(self.linear.weight.shape)
self.linear.bias.data = self.linear.bias.data*0
else:
self.linear = linear
def forward(self, x):
x = self.open(x)
for blk in range(self.nBlocks):
x = self.dynamicBlocks[blk](x)
x = self.connectors[blk](x)
if blk < self.nBlocks - 1:
# x = self.connectors[blk](x) # move connectors here ?????????????? Kind of a big change????????
x = F.avg_pool2d(x, 2) # TODO: make the pooling operator abstract for max vs avg pooling etc.
else:
# average each channel
x = F.avg_pool2d(x, x.shape[2:4])
x = x.view(x.size(0), -1)
x = self.linear(x)
return x
def setup_checkpointing(self,optimizer):
self.optimizers=[]
opt1=copy.deepcopy(optimizer)
opt1.defaults=optimizer.defaults
opt1.param_groups=[]
opt1.add_param_group({"params": self.open.parameters()})
self.optimizers.append(opt1)
for blk in range(self.nBlocks):
opt = copy.deepcopy(optimizer)
opt.defaults = optimizer.defaults
opt.param_groups = []
opt.add_param_group({"params":self.dynamicBlocks[blk].parameters()})
opt.add_param_group({"params":self.connectors[blk].parameters()})
self.optimizers.append(opt)
opt = copy.deepcopy(optimizer)
opt.defaults = optimizer.defaults
opt.param_groups = []
opt.add_param_group({'params': self.linear.weight})
self.optimizers.append(opt)
def checkpoint_train(self,data,optimizer,device,regularization=0):
self.setup_checkpointing(optimizer)
for optimizer in self.optimizers:
optimizer.zero_grad()
y=[]
inputs, labels = data
self.train() # set model to train mode
with torch.no_grad():
inputs, labels = inputs.to(device), labels.to(device)
# forward + backward + optimize
x = self.open(inputs)
y.append(x) # checkpointing
for blk in range(self.nBlocks):
x = self.dynamicBlocks[blk](x)
x = self.connectors[blk](x)
if blk < self.nBlocks - 1:
x = F.avg_pool2d(x, 2)
else:
x = F.avg_pool2d(x, x.shape[2:4])
# x = x.view(x.size(0), -1)
# x = self.linear(x)
y.append(x) # checkpointing
with torch.enable_grad():
y_tag=Variable(y[-1],requires_grad=True)
# net.linear.weight.data = torch.transpose(net.W[0:-1, :], 0, 1)
# net.linear.bias.data = net.W[-1, :]
loss, Si = misfitW(y_tag, torch.transpose(self.linear.weight,0,1), labels, device)
final_loss=loss.item()
loss.backward()
prev_grad = y_tag.grad
## check what we have on hand
self.optimizers[-1].step()
self.optimizers[-1].zero_grad()
for blk in reversed(range(self.nBlocks)):
y_tag = Variable(y[-2], requires_grad=True)
x = self.dynamicBlocks[blk](y_tag)
x = self.connectors[blk](x)
if blk < self.nBlocks - 1:
x = F.avg_pool2d(x, 2) # TODO: make the pooling operator abstract for max vs avg pooling etc.
else:
# average each channel
x = F.avg_pool2d(x, x.shape[2:4])
loss = torch.dot(x.view(-1),prev_grad.view(-1))
loss=loss+regularization*sum(self.dynamicBlocks[blk].weight_variance())
y.pop()
loss.backward()
prev_grad = y_tag.grad
self.optimizers[1+blk].step()
self.optimizers[1+blk].zero_grad()
x = self.open(inputs)
loss = torch.dot(x.view(-1),prev_grad.view(-1))
y.pop()
loss.backward()
self.optimizers[0].step()
self.optimizers[0].zero_grad()
_ , numCorrect, numTotal = getAccuracy(Si,labels)
torch.cuda.empty_cache()
return final_loss , numCorrect, numTotal
def checkpoint_train_debug(self,data,optimizer,device,net_other):
net_other.train() # set model to train mode
inputs, labels = data
optimizer.zero_grad()
inputs, labels = inputs.to(device), labels.to(device)
z = net_other.forward(inputs)
aa=[]
aa.append(inputs)
x = self.open(inputs)
aa.append(x)
for blk in range(self.nBlocks):
x = self.dynamicBlocks[blk](x)
x = self.connectors[blk](x)
if blk < self.nBlocks - 1:
x = F.avg_pool2d(x, 2) # TODO: make the pooling operator abstract for max vs avg pooling etc.
else:
# average each channel
x = F.avg_pool2d(x, x.shape[2:4])
aa.append(x)
loss_other, Si = misfit(z, net_other.linear.weight, labels, device)
loss_other.backward()
self.setup_checkpointing(optimizer)
for optimizer in self.optimizers:
optimizer.zero_grad()
y=[]
inputs, labels = data
self.train() # set model to train mode
with torch.no_grad():
inputs, labels = inputs.to(device), labels.to(device)
# forward + backward + optimize
y.append(inputs) # checkpointing
x = self.open(inputs)
y.append(x) # checkpointing
for blk in range(self.nBlocks):
x = self.dynamicBlocks[blk](x)
x = self.connectors[blk](x)
if blk < self.nBlocks - 1:
x = F.avg_pool2d(x, 2)
else:
x = F.avg_pool2d(x, x.shape[2:4])
y.append(x) # checkpointing
with torch.enable_grad():
y[-1]=Variable(y[-1],requires_grad=True)
for reg,chkpt in zip(aa,y):
print("forward vs checkpoint:")
print(torch.norm(reg - chkpt))
print(reg.shape)
loss, Si = misfit(y[-1], self.linear.weight, labels, device)
final_loss=loss.item()
print("loss is:")
print(torch.norm(loss-loss_other))
loss.backward()
print("linear.weight:")
print(torch.norm(self.linear.weight-net_other.linear.weight))
print(torch.norm(self.linear.weight.grad-net_other.linear.weight.grad))
## check what we have on hand
self.optimizers[-1].step()
self.optimizers[-1].zero_grad()
for blk in reversed(range(self.nBlocks)):
y[-2] = Variable(y[-2], requires_grad=True)
x = self.dynamicBlocks[blk](y[-2])
x = self.connectors[blk](x)
if blk < self.nBlocks - 1:
x = F.avg_pool2d(x, 2) # TODO: make the pooling operator abstract for max vs avg pooling etc.
else:
# average each channel
x = F.avg_pool2d(x, x.shape[2:4])
loss = torch.dot(x.view(-1),y[-1].grad.view(-1))
loss.backward()
y.pop()
print("connectors " + str(blk)+ ":")
P1=self.connectors[blk].parameters()
P2=net_other.connectors[blk].parameters()
for p1,p2 in zip(P1,P2):
print(torch.norm(p1.grad-p2.grad)/torch.norm(p1.grad))
print(p1.shape)
print("dynamicBlocks " + str(blk)+ ":")
P1=self.dynamicBlocks[blk].parameters()
P2=net_other.dynamicBlocks[blk].parameters()
for p1,p2 in zip(P1,P2):
print(torch.norm(p1.grad-p2.grad)/torch.norm(p1.grad))
print(p1.shape)
self.optimizers[1+blk].step()
self.optimizers[1+blk].zero_grad()
x = self.open(y[0])
loss = torch.dot(x.view(-1),y[1].grad.view(-1))
y.pop()
loss.backward()
print("Opening :")
P1 = self.open.parameters()
P2 = net_other.open.parameters()
for p1, p2 in zip(P1, P2):
print(torch.norm(p1.grad - p2.grad)/torch.norm(p1.grad))
print("finished")
self.optimizers[0].step()
self.optimizers[0].zero_grad()
y.pop()
_ , numCorrect, numTotal = getAccuracy(Si,labels)
torch.cuda.empty_cache()
return final_loss , numCorrect, numTotal
def regularization(self):
reg=0
for block in self.dynamicBlocks:
reg = reg + sum(block.weight_variance())
#print(reg)
return reg
def runRKNet(sDataset, sTitle, net=None,
tTheta=None, tY=None, vFeat = None,
dynamicScheme=rk4, dynamicParams=None, layer=DoubleSymLayer,
writeFile=True, gpu=0, fTikh=4e-4 , fMomentum=0.9, bNesterov=False,
transTrain=None, transTest=None, lr=None, batchSize=None,
regularization_param=0.0,checkpointing=False,reparametrization_epochs=None,
nTrain=None, nVal=None, percentVal=0.2,
loaderTrain=None, loaderVal=None, loaderTest=None, nChanIn=3, nClasses=None):
"""
:param sDataset: string, name of dataset ; 'cifar10' , 'stl10' , 'cifar100'
:param sTitle: string, prefix to name output files
:param net: Module, the network if want different from the default network
:param tTheta: list , time discretization for the controls (where the weights are)
:param tY: list , time discretization for the states (where the layers are)
:param vFeat: list , the width/ number of channels for each dynamic block (last entry for last connecting block)
:param dynamicScheme: Module name, time integrator/ ODE solver (ex: rk1, rk4)
:param layer: Module name, layer used for dynamic block (ex: DoubleLayer, DoubleSymLayer)
:param writeFile: boolean, True means write the log to a text file, False means print to terminal
:param gpu: int , which gpu to use to operate (must be < number of gpus accessible)
:param fTikh: float, weight decay / tikhonov regularization scalar for optimizer
:param fMomentum: float, momentum value for optimizer
:param bNesterov: boolean, True means use Nesterov, False means no Nesterov for optimizer
:param lr: numpy array, learning rate per epoch (length=number of epochs)
:param batchSize: int, number of input examples (images) per batch
:param transTrain: pytorch transform, defines the augmentation and conversion to Tensor for training set
:param transTest: pytorch transform, defines the augmentation and conversion to Tensor for testing/validation set
:param regularization_param: float, for the regularization in time within each dynamic block, the loss
becomes: Loss + regularization_param * regMetric(prevLayer,currLayer) (regMetric defined in utils)
:param checkpointing: boolean, True means implement checkpointing, False runs normally
:param reparametrization_epochs:
To use entire dataset, leave nTrain and nVal as None. nTrain and nVal values override percentVal.
:param nTrain: int, number of training examples (must be <= total examples in training set)
:param nVal: int, number of validation examples ( must be <= total examples in validation set)
:param percentVal: float, percentage of training set that will be split out as validation
if passing one of the following, must pass all 5:
loaderTrain, loaderVal, loaderTest, nChanIn, and nClasses
:param loaderTrain: dataloader for training data
:param loaderVal: dataloader for validation data
:param loaderTest: dataloader for testing data
:param nChanIn: int, number of input channels
:param nClasses: int, number of output classes
:return: net, sBasePath, device
net: the model
sBasePath: string, path used to name files; (ex. sBasePath_model.pt, sBasePath.txt)
device: torch device used to train the model
"""
start = time.time() # timing
sStartTime = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
print('start time: ', sStartTime)
sBasePath = 'results/' + sTitle + '_' + sDataset + '_' + sStartTime
print('file: ', sBasePath)
# get defaults -----------------------------------------------------------------------------
vFeatDef, transTrainDef, transTestDef, batchSizeDef, lrDef, tThetaDef, nTrainDef, nValDef, device = \
getRunDefaults(sDataset, gpu=gpu)
if writeFile: # write output to a text file instead of stdout
sys.stdout = open(sBasePath + '.txt' , 'wt' )
# For the option to reparametrize during training:
if reparametrization_epochs is not None:
tY_array=tY
tTheta_array=tTheta
tY=tY[0]
tTheta=tTheta[0]
else:
tY_array = None
tTheta_array = None
if net is None:
if vFeat is None:
vFeat = vFeatDef
if tTheta is None:
tTheta = tThetaDef
else:
vFeat = net.vFeat
tTheta = net.tTheta
tY = net.tY
if transTrain is None:
transTrain = transTrainDef
if transTest is None:
transTest = transTestDef
if batchSize is None:
batchSize = batchSizeDef
if lr is None:
lr = lrDef
if nTrain is None:
nTrain = nTrainDef
if nVal is None:
nVal = nValDef
if tY is None:
tY = tTheta
# percentVal = 0.20 # use 20% of the training set as validation
# fTikh = 4e-4 # tikhonov regularization / weight decay value
# fMomentum = 0.9
# bNesterov = False
#-------------------------------------------------------------------------------------------
nEpoch = lr.size
print("device", device)
print('time steps Y: ' , tY)
print('time steps theta: ' , tTheta)
print('no. of channels: ' , vFeat)
print('no. of epochs: ' , nEpoch)
print('ODE reg param: ' , regularization_param)
print('Use checkpointing: ' , checkpointing)
# assume that if trainLoader is passed, then all loaders are passed
if loaderTrain is None:
if nTrain is not None and nVal is not None:
loaderTrain, loaderVal, loaderTest, nChanIn, nClasses = \
getDataLoaders(sDataset, batchSize, device,
batchSizeTest=batchSize, percentVal=percentVal,
transformTrain=transTrain, transformTest=transTest,
nTrain=nTrain, nVal=nVal)
else:
loaderTrain, loaderVal, loaderTest, nChanIn, nClasses = \
getDataLoaders( sDataset , batchSize, device,
batchSizeTest=batchSize, percentVal=percentVal,
transformTrain=transTrain, transformTest=transTest,
nTrain=None, nVal=None )
# print the matrix presenting the channels and conv sizes for every layer
# printFeatureMatrix(nt, nBlocks, vFeat)
if net is None:
net = RKNet(tY, tTheta, nChanIn, nClasses, vFeat, dynamicScheme=dynamicScheme, dynamicParams=dynamicParams, layer=layer )
net.to(device)
# count number of weights and layers
nWeights = sum(p.numel() for p in net.parameters())
nLayers = len(list(net.parameters()))
print('Training ',nWeights,' weights in ', nLayers, ' layers',flush=True)
# separate out the norms
normParams = []
convParams = []
for name, param in net.named_parameters():
if 'normLayer' in name:
normParams.append(param)
else:
convParams.append(param)
allParams = [ {'params': normParams, 'weight_decay': 0 },
{'params': convParams} ]
optimizer = optim.SGD( allParams, lr=lr.item(0), momentum=fMomentum, weight_decay=fTikh, nesterov=bNesterov)
print('optimizer = SGD with momentum=%.1e, weight_decay=%.1e, nesterov=%d, batchSize=%d'
% (fMomentum, fTikh, bNesterov, batchSize))
# train the network
sPathOpt = trainNet(net, optimizer, lr, nEpoch, device, sBasePath, loaderTrain, loaderVal, nMini=1, verbose=True,
regularization_param=regularization_param,checkpointing=checkpointing)
end = time.time()
print('Time elapsed: ' , end-start)
print('Training complete. Now testing...')
#--------------TESTING-----------------
# evaluate performance on test set
# load best model weights based on validation accuracy
net = torch.load(sPathOpt + '.pt')
testLoss, testAcc = evalNet(net, device, loaderTest)
print('\ntesting loss: %-9.2e testing accuracy: %-9.2f' %
( testLoss, testAcc * 100 ) )
print(net) # just so we can see what does what
return net, sBasePath, device
print('done')
def runDefault():
runRKNet( sDataset='cifar10', sTitle = 'default_RK4_DoubleSym', writeFile=False)
def runDefaultRegularized():
runRKNet( sDataset='stl10', sTitle = 'regularizedBN_RK1_Double', dynamicScheme=rk1, layer = DoubleLayer,
writeFile=True,regularization_param=0.01, checkpointing=False, gpu=1, dynamicParams="Batch")
# For Testing!!
if __name__ == '__main__':
runDefault()
# runDefaultRegularized()