-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
412 lines (381 loc) · 15.3 KB
/
main.py
File metadata and controls
412 lines (381 loc) · 15.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
import argparse
from pathlib import Path
from DataLoader import dataGenerator
from ResMPS import ResMPS
from OtherNN import EM, FullLinear
from collections import namedtuple
from functools import partial
from Tools import timer
import torch as th
import dill
import sys
Ostate = namedtuple('Ostate', ('loss_train',
'loss_test',
'acc_train',
'acc_test',
))
def cal_loss(predict, target, lossfunc, mean=True):
batch_size, *_ = predict.shape
if lossfunc == 'mse':
predict2 = th.squeeze(predict)
loss = th.sum((target-predict2)**2)
elif lossfunc == 'nll':
predict2 = th.squeeze(predict)
target2 = th.argmax(target, axis=1)
loss = th.nn.NLLLoss(reduction='sum')(predict2, target2)
elif lossfunc == 'cross_entropy':
predict2 = th.squeeze(predict)
target2 = th.argmax(target, axis=1)
loss = th.nn.CrossEntropyLoss(reduction='sum')(predict2, target2)
else:
raise ValueError('loss function not defined.')
if mean:
loss = loss/batch_size
return loss
def cal_acc_and_loss(cf, input_data, target, lossfunc):
with th.no_grad( ):
predict = cf(input_data)
label_predict = th.argmax(th.squeeze(predict), dim=1)
label_target = th.argmax(target, dim=1)
total_number = len(label_predict)
true_number = th.sum(label_predict == label_target).item( )
loss = cal_loss(predict, target, lossfunc, mean=False).item( )
return true_number, loss, total_number
def evaluation(cf, data_gen, lossfunc):
total_true = 0
total_loss = 0
total_numb = 0
while True:
i, j, input_data, target = next(data_gen)
n1, n2, n3 = cal_acc_and_loss(cf, input_data, target, lossfunc)
total_true += n1
total_loss += n2
total_numb += n3
if i == j-1:
break
meanloss = total_loss/total_numb
meanacc = total_true/total_numb
return meanacc, meanloss
@timer
def epoch_step(cf, optim, gtrain1, gtrain2, gtest,
step_batch_num, lstate,
lossfunc='cross_entropy',
scheduler=None
):
# print learning rate
lr = [i['lr'] for i in optim.param_groups]
str_lr = ', '.join([str(i) for i in lr])
printfunc(f"learning rate={str_lr}")
printfunc('starting training process...')
while True:
step, total_step, input_data, target = next(gtrain1)
lstr = [ ]
lstr.append(f"(step/total)=({step+1:>4d}/{total_step:>4d}),")
predict = cf(input_data)
label_predict = th.argmax(th.squeeze(predict), dim=1)
label_target = th.argmax(target, dim=1)
total_number = len(label_predict)
true_number = th.sum(label_predict == label_target).item( )
lstr.append(f"batch accuracy = {true_number/total_number*100:.2f}%.")
loss = cal_loss(predict, target, lossfunc=lossfunc)
loss.backward( )
printfunc('\r'+' '.join(lstr), end='', flush=True)
if (step % step_batch_num == step_batch_num - 1) \
or (step == total_step-1):
with th.no_grad( ):
cf.project_grad( )
optim.step( )
with th.no_grad( ):
cf.project_tensor( )
optim.zero_grad( )
if step == total_step-1:
printfunc( )
printfunc('starting evaluating process...', flush=True)
cf.eval( )
acc1, loss1 = evaluation(cf, gtrain2, lossfunc)
acc2, loss2 = evaluation(cf, gtest, lossfunc)
cf.train( )
lstate.append(Ostate(loss1, loss2, acc1, acc2))
printfunc(f'train set accuracy = {acc1*100:.2f}%')
printfunc(f'test set accuracy = {acc2*100:.2f}%')
printfunc(f'train set loss = {loss1:.5f}')
printfunc(f'test set loss = {loss2:.5f}')
if len(lstate) > 20:
loss_list = [i[0] for i in lstate]
loss1 = min(loss_list[-10:])
loss2 = min(loss_list[-20:-10])
convergence_factor = loss1/loss2
else:
convergence_factor = 0
printfunc(f'convergence factor = {convergence_factor:.5f}')
break
return convergence_factor
def save_para(params, path_str):
Path(path_str).mkdir(parents=True, exist_ok=True)
th.save(params, f'{path_str}/parameter')
with open(f'{path_str}/parameter.txt', 'w') as f:
for key, value in params.items( ):
f.write(f'{key}:\t{value}\n')
return None
def run(chi=12,
batch_size_train1=1000,
batch_size_train2=1000,
batch_size_test=1000,
step_batch_num=1,
total_step=20,
max_prune_number=3,
critical_prune_number=.1,
critical_convergence_factor=.8,
prune_mode=None,
max_batch_num_train1=None,
max_batch_num_train2=None,
max_batch_num_test=None,
lr=1e-4,
std=1e-3,
dataset='mnist',
lossfunc="cross_entropy",
network="ResMPS",
optim_method='Adam',
root='.',
relu=False,
dropout_prob=0,
perturbation=True,
parallel=True,
forward_norm=1,
rotation=False,
norm_grad=False,
norm_tensor=False,
write2file=False,
path=None,
select=None,
device=None,
shuffle_mode=2,
fm_funcs=None,
tsne=False,
taskname='run'
):
path_str = f'{root}/result/{taskname}'
save_para(locals( ), path_str)
# overload print function
original_stdout = sys.stdout
f = open(f'{root}/result/{taskname}.log', 'w')
global printfunc
def printfunc(*fargs, **fkargs):
if write2file:
sys.stdout = f
print(*fargs, **fkargs)
sys.stdout = original_stdout
print(*fargs, **fkargs)
else:
print(*fargs, **fkargs)
if isinstance(select, str):
select = [int(i) for i in select]
if device is None:
device = 'cuda' if th.cuda.is_available( ) else 'cpu'
if prune_mode is not None:
assert prune_mode in ['percentage_add', 'percentage_mul', 'magnetude']
if select is None:
nlabel = 10
else:
nlabel = len(select)
if path == 'hilbert':
expand = True
n_site = 1025
else:
expand = False
n_site = 785
if fm_funcs is None or len(fm_funcs) == 0:
d = 2
else:
fm_funcs = eval(fm_funcs)
d = len(fm_funcs)
if network in ["ResMPS"]:
cf = ResMPS(d=d, D=chi, n_site=n_site, n_label=nlabel,
std=std,
renorm=norm_tensor,
renorm_grad=norm_grad,
rotation=rotation,
forward_norm=forward_norm,
parallel_forward=parallel,
perturbation=perturbation,
relu=relu,
dropout_prob=dropout_prob,
path=path,
device=device,
tsne=tsne,
feature_map_funcs=fm_funcs
).to(device)
elif network in ["LM", "FM"] + [f"ResMPS_order{i}" for i in range(1, 4)]:
if network in ["LM", "ResMPS_order1"]:
CF = partial(EM, order=1)
elif network in ["FM", "ResMPS_order2"]:
CF = partial(EM, order=2)
elif network in ["ResMPS_order3"]:
CF = partial(EM, order=3)
cf = CF(D=chi, n_res=n_site-1, n_class=nlabel,
relu=relu,
dropout_prob=dropout_prob,
device=device,
).to(device)
elif network == "FullLinear1":
cf = FullLinear(n_site-1, nlabel, device=device).to(device)
elif network == "FullLinear2":
cf = FullLinear(n_site-1, nlabel, depth=2, device=device).to(device)
elif network == "FullLinear3":
cf = FullLinear(n_site-1, nlabel, depth=3, device=device).to(device)
else:
raise KeyError(f"network {network} undefined !")
total_params = sum(p.numel() for p in cf.parameters())
printfunc(f"Total number of parameters is {total_params}.")
gtrain1 = dataGenerator(root=f"{root}/datasets",
batch_size=batch_size_train1,
max_batch_num=max_batch_num_train1,
train=True, seed=None,
device=device, expand=expand,
select=select,
dataset=dataset,
shuffle_mode=shuffle_mode
)
gtrain2 = dataGenerator(root=f"{root}/datasets",
batch_size=batch_size_train2,
max_batch_num=max_batch_num_train2,
train=True, seed=None,
device=device, expand=expand,
select=select,
dataset=dataset,
shuffle_mode=shuffle_mode
)
gtest = dataGenerator(root=f"{root}/datasets",
batch_size=batch_size_test,
max_batch_num=max_batch_num_test,
train=False, seed=None,
device=device, expand=expand,
select=select,
dataset=dataset,
shuffle_mode=shuffle_mode
)
optim_func_dict = {
"SGD": th.optim.SGD,
"Adam": th.optim.Adam
}
optim = optim_func_dict[optim_method](cf.parameters( ), lr=lr)
lstate = [ ]
prune_step = 0
prune_count = 0
for epoch in range(1, total_step+1):
prune_step += 1
printfunc('\nstep', epoch, flush=True)
convergence_factor = epoch_step(cf, optim, gtrain1, gtrain2, gtest,
step_batch_num, lstate,
lossfunc=lossfunc
)
th.save({
'epoch': epoch,
'model_state_dict': cf.state_dict( ),
'optimizer_state_dict': optim.state_dict( ),
'lstate': lstate
}, f'{path_str}/checkpoint.tar')
if tsne:
cf_copy=dill.dumps(cf)
th.save(cf_copy, f'{path_str}/network.tar')
# th.save(cf, f'{path_str}/network.tar', pickle_protocol=)
if convergence_factor > critical_convergence_factor \
and prune_step > 25 \
and prune_mode is not None:
th.save({
'epoch': epoch,
'model_state_dict': cf.state_dict( ),
'optimizer_state_dict': optim.state_dict( ),
'lstate': lstate
}, f'{path_str}/checkpoint_prune{prune_count}.tar')
if prune_count > max_prune_number:
break
if prune_mode == 'percentage_add':
temp = critical_prune_number*(prune_count + 1)
cf.prune(temp, mode='percent')
elif prune_mode == 'percentage_mul':
temp = 1 - (1 - critical_prune_number)**(prune_count + 1)
cf.prune(temp, mode='percent')
elif prune_mode == 'magnetude':
cf.prune(critical_prune_number/(1+prune_count),
mode='magnetude')
else:
raise ValueError(f"prune mode {prune_mode} undefined!")
prune_step = 0
prune_count += 1
if write2file:
f.close( )
if __name__ == "__main__":
parser = argparse.ArgumentParser( )
# dimension of hidden feature chi
parser.add_argument('--chi', type=int, default=12,
help='dimension of hidden feature chi')
# total training steps
parser.add_argument('--total_step', type=int, default=80,
help='total training steps')
# batch size
parser.add_argument('--batch_size', type=int, default=1000,
help='batch size')
# learning rate
parser.add_argument('--lr', type=float, default=1e-4,
help='learning rate')
# standard deviation of initial weights
parser.add_argument('--std', type=float, default=1e-3,
help='standard deviation of initial weights')
# mnist or fashion_mnist
parser.add_argument('--dataset', type=str, default='fashion_mnist',
help='mnist or fashion_mnist')
# ResMPS, ResMPS_order1, ResMPS_order2 or ResMPS_order3
parser.add_argument('--network', type=str, default='ResMPS',
help='ResMPS, ResMPS_order1, ResMPS_order2 or ResMPS_order3')
# Name your task as you like
parser.add_argument('--taskname', type=str, default='example_task',
help='Name your task as you like')
# normal, zigzag or random
parser.add_argument('--path', type=str, default='normal',
help='normal, zigzag or random')
# whether to use parallel acceleration, only valid for linear models
parser.add_argument('--parallel', default=False, action='store_true',
help='whether to use parallel acceleration, only valid for linear models')
# whether to use the traditional MPS model, see https://arxiv.org/abs/1906.06329
parser.add_argument('--classical', default=False, action='store_true',
help='whether to use the traditional MPS model, see https://arxiv.org/abs/1906.06329')
# whether to use ReLU, True or False
parser.add_argument('--relu', default=False, action='store_true',
help='whether to use ReLU, True or False')
# a float between 0 and 1, 0 is no dropout
parser.add_argument('--dropout_prob', type=float, default=0,
help='a float between 0 and 1, 0 is no dropout')
# cuda or cpu
parser.add_argument('--device', type=str, default='cpu',
help='cuda or cpu')
# string, which will be conveted to a tuple of lambda functions
parser.add_argument('--feature_map', type=str, default='(lambda x: x,)',
help='string, which will be conveted to a tuple of lambda functions')
# if true, collect intermediate hidden variables
parser.add_argument('--tsne', default=False, action='store_true',
help='if true, collect intermediate hidden variables')
# if true, write output to a log file
parser.add_argument('--write2file', default=False, action='store_true',
help='if true, write output to a log file')
args = parser.parse_args( )
run(chi=args.chi,
total_step=args.total_step,
batch_size_train1=args.batch_size,
batch_size_train2=args.batch_size,
batch_size_test=args.batch_size,
lr=args.lr,
std=args.std,
parallel=args.parallel,
dataset=args.dataset,
network=args.network,
relu=args.relu,
dropout_prob=args.dropout_prob,
device=args.device,
write2file=args.write2file,
path=args.path,
fm_funcs=args.feature_map,
perturbation=(not args.classical),
tsne=args.tsne,
taskname=args.taskname
)