-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_multiclass_sgd.py
355 lines (270 loc) · 10.5 KB
/
main_multiclass_sgd.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
# pylint: disable=C, R, bare-except, arguments-differ, no-member, undefined-loop-variable
import argparse
import copy
import itertools
import os
import subprocess
from functools import partial
from time import perf_counter
import torch
from arch import FC
from arch.swish import swish
from dataset import get_dataset
from dynamics import ContinuousMomentum, gradient, loglinspace, make_step
def output_gradient(f, loss, x, y, out0, bs):
i = torch.randperm(len(x))[:bs]
o = f(x[i])
l = loss(o - out0[i], y[i]).mean()
grad = gradient(l, f.parameters())
return o, grad
def train_regular(f0, x, y, tau, loss, subf0, lr, bs):
f = copy.deepcopy(f0)
with torch.no_grad():
with torch.no_grad():
out0 = f0(x)
if not subf0:
out0 = torch.zeros_like(out0)
optimizer = ContinuousMomentum(f.parameters(), dt=lr, tau=tau)
t = 0
out, grad = output_gradient(f, loss, x, y, out0, bs)
for step in itertools.count():
state = {
'step': step,
't': t,
'dt': lr,
}
yield state, f, out, out0, grad
if torch.isnan(out).any():
break
# make 1 step:
state = copy.deepcopy((f.state_dict(), optimizer.state_dict(), t))
make_step(f, optimizer, lr, grad)
t += lr
out, grad = output_gradient(f, loss, x, y, out0, bs)
def loss_func(args, f, y):
if args.loss == 'crossentropy':
return torch.nn.functional.cross_entropy(args.alpha * f, y, reduction='none') / args.alpha
class SplitEval(torch.nn.Module):
def __init__(self, f, size):
super().__init__()
self.f = f
self.size = size
def forward(self, x):
return torch.cat([self.f(x[i: i + self.size]) for i in range(0, len(x), self.size)])
def run_regular(args, f0, xtr, ytr, xte, yte):
with torch.no_grad():
ote0 = f0(xte)
if args.f0 == 0:
ote0 = torch.zeros_like(ote0)
tau = args.tau_over_h * args.h
if args.tau_alpha_crit is not None:
tau *= min(1, args.tau_alpha_crit / args.alpha)
best_test_error = 1
tmp_outputs_index = -1
torch.manual_seed(args.seed_batch)
checkpoint_generator = loglinspace(100, 100 * 100)
checkpoint = next(checkpoint_generator)
wall = perf_counter()
dynamics = []
for state, f, _otr, otr0, grad in train_regular(f0, xtr, ytr, tau, partial(loss_func, args), bool(args.f0), args.lr, args.bs):
save = False
save_outputs = False
stop = False
if state['step'] == checkpoint:
checkpoint = next(checkpoint_generator)
save = True
if torch.isnan(_otr).any():
save = True
stop = True
if not save:
continue
with torch.no_grad():
otr = f(xtr) - otr0
ote = f(xte) - ote0
if args.save_outputs:
save_outputs = True
if (otr.argmax(1) != ytr).sum() == 0:
save_outputs = True
stop = True
if wall + args.train_time < perf_counter():
save_outputs = True
stop = True
test_err = (ote.argmax(1) != yte).double().mean().item()
if test_err < best_test_error:
if tmp_outputs_index != -1:
dynamics[tmp_outputs_index]['train']['outputs'] = None
dynamics[tmp_outputs_index]['train']['labels'] = None
dynamics[tmp_outputs_index]['test']['outputs'] = None
dynamics[tmp_outputs_index]['test']['labels'] = None
best_test_error = test_err
if not save_outputs:
tmp_outputs_index = len(dynamics)
save_outputs = True
state['grad_norm'] = grad.norm().item()
state['wall'] = perf_counter() - wall
state['norm'] = sum(p.norm().pow(2) for p in f.parameters()).sqrt().item()
state['dnorm'] = sum((p0 - p).norm().pow(2) for p0, p in zip(f0.parameters(), f.parameters())).sqrt().item()
if args.arch == 'fc':
def getw(f, i):
return torch.cat(list(getattr(f.f, "W{}".format(i))))
state['wnorm'] = [getw(f, i).norm().item() for i in range(f.f.L + 1)]
state['dwnorm'] = [(getw(f, i) - getw(f0, i)).norm().item() for i in range(f.f.L + 1)]
state['state'] = copy.deepcopy(f.state_dict()) if save_outputs and (args.save_state == 1) else None
state['train'] = {
'loss': loss_func(args, otr, ytr).mean().item(),
'aloss': args.alpha * loss_func(args, otr, ytr).mean().item(),
'err': (otr.argmax(1) != ytr).double().mean().item(),
'dfnorm': otr.pow(2).mean().sqrt(),
'fnorm': (otr + otr0).pow(2).mean().sqrt(),
'outputs': otr if save_outputs else None,
'labels': ytr if save_outputs else None,
}
state['test'] = {
'loss': loss_func(args, ote, yte).mean().item(),
'aloss': args.alpha * loss_func(args, ote, yte).mean().item(),
'err': test_err,
'dfnorm': ote.pow(2).mean().sqrt(),
'fnorm': (ote + ote0).pow(2).mean().sqrt(),
'outputs': ote if save_outputs else None,
'labels': yte if save_outputs else None,
}
print(
(
"[i={d[step]:d} t={d[t]:.2e} wall={d[wall]:.0f}] "
+ "[train aL={d[train][aloss]:.2e} err={d[train][err]:.2f}] "
+ "[test aL={d[test][aloss]:.2e} err={d[test][err]:.2f}]"
).format(d=state, p=len(ytr)),
flush=True
)
dynamics.append(state)
out = {
'dynamics': dynamics,
}
yield f, out
if stop:
break
def run_exp(args, f0, xtr, ytr, xte, yte):
run = {
'args': args,
'N': sum(p.numel() for p in f0.parameters()),
'finished': False,
}
if args.regular == 1:
wall = perf_counter()
for _f, out in run_regular(args, f0, xtr, ytr, xte, yte):
run['regular'] = out
if perf_counter() - wall > 120:
wall = perf_counter()
yield run
yield run
run['finished'] = True
yield run
def init(args):
torch.backends.cudnn.benchmark = True
if args.dtype == 'float64':
torch.set_default_dtype(torch.float64)
if args.dtype == 'float32':
torch.set_default_dtype(torch.float32)
[(xte, yte, ite), (xtr, ytr, itr)] = get_dataset(
args.dataset,
(args.pte, args.ptr),
(args.seed_testset + args.pte, args.seed_trainset + args.ptr),
args.d,
args.device,
torch.get_default_dtype()
)
torch.manual_seed(0)
if args.act == 'relu':
def act(x):
return torch.relu(x).mul(2 ** 0.5)
elif args.act == 'tanh':
def act(x):
return torch.tanh(x).mul(1.5927116424039378)
elif args.act == 'softplus':
factor = torch.nn.functional.softplus(torch.randn(100000, dtype=torch.float64), args.act_beta).pow(2).mean().rsqrt().item()
def act(x):
return torch.nn.functional.softplus(x, beta=args.act_beta).mul(factor)
elif args.act == 'swish':
act = swish
else:
raise ValueError('act not specified')
_d = abs(act(torch.randn(100000, dtype=torch.float64)).pow(2).mean().rsqrt().item() - 1)
assert _d < 1e-2, _d
torch.manual_seed(args.seed_init + hash(args.alpha) + args.ptr)
c = len(ytr.unique())
if args.arch == 'fc':
assert args.L is not None
xtr = xtr.flatten(1)
xte = xte.flatten(1)
f = FC(xtr.size(1), args.h, c, args.L, act, args.bias)
else:
raise ValueError('arch not specified')
f = SplitEval(f, args.chunk)
f = f.to(args.device)
return f, xtr, ytr, itr, xte, yte, ite
def execute(args):
f, xtr, ytr, itr, xte, yte, ite = init(args)
torch.manual_seed(0)
for run in run_exp(args, f, xtr, ytr, xte, yte):
run['dataset'] = {
'test': ite,
'train': itr,
}
yield run
def main():
print('deprecated')
return
git = {
'log': subprocess.getoutput('git log --format="%H" -n 1 -z'),
'status': subprocess.getoutput('git status -z'),
}
parser = argparse.ArgumentParser()
parser.add_argument("--device", type=str, default='cuda')
parser.add_argument("--dtype", type=str, default='float64')
parser.add_argument("--seed_init", type=int, default=0)
parser.add_argument("--seed_batch", type=int, default=0)
parser.add_argument("--seed_testset", type=int, default=0, help="determines the testset, will affect the trainset as well")
parser.add_argument("--seed_trainset", type=int, default=0, help="determines the trainset")
parser.add_argument("--dataset", type=str, required=True)
parser.add_argument("--ptr", type=int, required=True)
parser.add_argument("--pte", type=int)
parser.add_argument("--d", type=int)
parser.add_argument("--arch", type=str, required=True)
parser.add_argument("--act", type=str, required=True)
parser.add_argument("--act_beta", type=float, default=5.0)
parser.add_argument("--bias", type=float, default=0)
parser.add_argument("--L", type=int)
parser.add_argument("--h", type=int, required=True)
parser.add_argument("--regular", type=int, default=1)
parser.add_argument("--save_outputs", type=int, default=0)
parser.add_argument("--save_state", type=int, default=0)
parser.add_argument("--alpha", type=float, required=True)
parser.add_argument("--f0", type=int, default=1)
parser.add_argument("--tau_over_h", type=float, default=0.0)
parser.add_argument("--tau_alpha_crit", type=float)
parser.add_argument("--lr", type=float, required=True)
parser.add_argument("--bs", type=int, required=True)
parser.add_argument("--train_time", type=float, required=True)
parser.add_argument("--chunk", type=int)
parser.add_argument("--loss", type=str, default="crossentropy")
parser.add_argument("--pickle", type=str, required=True)
args = parser.parse_args()
if args.pte is None:
args.pte = args.ptr
if args.chunk is None:
args.chunk = max(args.ptr, args.pte, args.ptk)
torch.save(args, args.pickle)
saved = False
try:
for res in execute(args):
res['git'] = git
with open(args.pickle, 'wb') as f:
torch.save(args, f)
torch.save(res, f)
saved = True
except:
if not saved:
os.remove(args.pickle)
raise
if __name__ == "__main__":
main()