-
Notifications
You must be signed in to change notification settings - Fork 1
/
runner_synth.py
181 lines (154 loc) · 6.3 KB
/
runner_synth.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
from fastcore.script import *
from utils import *
import argparse
from types import SimpleNamespace
import torch as th
import torch.nn.functional as F
import numpy as np
import time
import os
import json
dev = 'cuda' if th.cuda.is_available() else 'cpu'
def fit(m, ds, epochs=200, bs=128, autocast=True, opt=None, sched=None, fix_batch=np.zeros(2), loss='ce'):
if np.max(fix_batch) == 0:
batch_sampler = None
else:
batch_sampler = th.utils.data.BatchSampler(
fix_batch.flatten(), bs, False)
trainloader = th.utils.data.DataLoader(
ds['train'], batch_size=bs, shuffle=True, num_workers=0, batch_sampler=batch_sampler)
fixed_trainloader = th.utils.data.DataLoader(
ds['train'], batch_size=bs, shuffle=False, num_workers=0)
testloader = th.utils.data.DataLoader(
ds['val'], batch_size=bs, shuffle=False, num_workers=0)
def helper(t):
m.eval()
start = time.time()
with th.no_grad():
# train error
yh, f, e = [], [], []
for i, (x, y) in enumerate(fixed_trainloader):
x, y = x.to(dev), y.to(dev)
yh.append(F.log_softmax(m(x), dim=1))
f.append(-th.gather(yh[-1], 1, y.view(-1, 1)))
e.append((y != th.argmax(yh[-1], dim=1).long()).float())
# val error
yvh, fv, ev = [], [], []
for i, (xv, yv) in enumerate(testloader):
xv, yv = xv.to(dev), yv.to(dev)
yvh.append(F.log_softmax(m(xv), dim=1))
fv.append(-th.gather(yvh[-1], 1, yv.view(-1, 1)))
ev.append((yv != th.argmax(yvh[-1], dim=1).long()).float())
ss = dict(yh=yh, f=f, e=e, yvh=yvh, fv=fv, ev=ev)
for k, _ in ss.items():
ss[k] = th.cat(ss[k], dim=0).to('cpu')
f = ss['f'].mean()
acc = 100-ss['e'].mean()*100
fv = ss['fv'].mean()
accv = 100-ss['ev'].mean()*100
lr = opt.param_groups[0]['lr']
print('[%06d] f: %2.3f, acc: %2.2f, fv: %2.3f, accv: %2.2f, lr: %2.4f, time: %2.2f' %
(t, f, acc, fv, accv, lr, time.time()-start))
m.train()
return ss
scaler = th.cuda.amp.GradScaler()
m.train()
ss = []
t = 0
ss.append(helper(t))
for epoch in range(epochs):
for i, (x, y) in enumerate(trainloader):
x, y = x.to(dev), y.to(dev)
with th.autocast(enabled=autocast, device_type='cuda'):
m.zero_grad()
yyh = m(x)
if loss == 'mse':
f = -0.5*((yyh-y)**2).sum(-1).mean()
elif loss == 'ce':
yyh = F.log_softmax(yyh, dim=1)
f = -th.gather(yyh, 1, y.view(-1, 1)).mean()
e = (y != th.argmax(yyh, dim=1).long()).float().mean()
if autocast:
scaler.scale(f).backward()
scaler.step(opt)
scaler.update()
else:
f.backward()
opt.step()
sched.step()
t += 1
if epoch < 5 and i % (len(trainloader) // 4) == 0:
ss.append(helper(t))
if 5 <= epoch <= 25:
ss.append(helper(t))
elif 25 < epoch <= 65 and epoch % 4 == 0:
ss.append(helper(t))
elif epoch > 65 and epoch % 15 == 0 or (epoch == epochs-1):
ss.append(helper(t))
return ss
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--seed', type=int, default=42)
parser.add_argument('--init-seed', type=int, default=42)
parser.add_argument('--data-config', '-d', type=str,
default='./configs/data/cifar10-full.yaml',
help='dataset name, augmentation')
parser.add_argument('--model-config', '-m', type=str,
default='./configs/model/convmixer.yaml',
help='model name, model aruguments, batch normalization')
parser.add_argument('--optim-config', '-o', type=str,
default='./configs/optim/adam-200-0.001.yaml',
help='batch size, batch seed, learning rate, optimizer, weight decay')
parser.add_argument('--init-config', '-i', type=str,
default='./configs/init/normal.yaml',
help='start from corner')
parser.add_argument('--save-dir', '-s', type=str,
default='results/models/all',
help='directory to save results')
args = parser.parse_args()
seed = args.seed
root = args.save_dir
os.makedirs(root, exist_ok=True)
data_args = get_configs(args.data_config)
model_args = get_configs(args.model_config)
optim_args = get_configs(args.optim_config)
init_args = get_configs(args.init_config)
args = SimpleNamespace(
**{**vars(args), **data_args, **model_args, **optim_args, **init_args})
print(args)
fn = json.dumps(
dict(seed=seed,
bseed=args.batch_seed,
iseed=args.init_seed,
aug=args.aug,
m=os.path.basename(args.model_config)[:-5],
init=init_args['init_fn'],
opt=os.path.basename(args.optim_config).split('-')[0],
bs=args.bs,
lr=args.opt_args['lr'],
wd=args.opt_args['weight_decay'])
).replace(' ', '')
print(fn)
if os.path.exists(os.path.join(root, fn+'.p')):
return
setup(2)
ds = get_data(data_args)
N_train = len(ds['train'])
T = args.epochs * N_train // args.bs
optim_args['T'] = T
if args.batch_seed < 0:
fix_batch = np.zeros(2)
else:
setup(args.batch_seed)
fix_batch = np.random.randint(N_train, size=(T, args.bs))
setup(seed)
m = get_model(model_args, dev=dev)
optimizer, scheduler = get_opt(optim_args, m)
m = get_init(init_args, m, init_seed=args.init_seed,
save_fn=os.path.basename(args.model_config)[:-5])
ss = fit(m, ds, epochs=args.epochs, bs=optim_args['bs'], autocast=optim_args['autocast'],
opt=optimizer, sched=scheduler, fix_batch=fix_batch, fname=fn)
th.save({"data": ss, "configs": args}, os.path.join(root, fn+'.p'))
if __name__ == '__main__':
# default setting gives 92% acc
main()