-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
482 lines (394 loc) · 16.1 KB
/
models.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
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
import torch
import torch.nn as nn
from transformers import GPT2Model, GPT2Config
from tqdm import tqdm
from sklearn.svm import LinearSVC
from sklearn.linear_model import LogisticRegression, Lasso
import warnings
from sklearn import tree
import xgboost as xgb
from base_models import NeuralNetwork, ParallelNetworks
def build_model(conf):
if conf.family == "gpt2":
model = TransformerModel(
n_dims=conf.n_dims,
n_positions=conf.n_positions,
n_embd=conf.n_embd,
n_layer=conf.n_layer,
n_head=conf.n_head,
)
else:
raise NotImplementedError
return model
def get_relevant_baselines(task_name):
task_to_baselines = {
"linear_regression": [
(LeastSquaresModel, {}),
(NNModel, {"n_neighbors": 3}),
(AveragingModel, {}),
],
"linear_classification": [
(NNModel, {"n_neighbors": 3}),
(AveragingModel, {}),
],
"sparse_linear_regression": [
(LeastSquaresModel, {}),
(NNModel, {"n_neighbors": 3}),
(AveragingModel, {}),
]
+ [(LassoModel, {"alpha": alpha}) for alpha in [1, 0.1, 0.01, 0.001, 0.0001]],
"relu_2nn_regression": [
(LeastSquaresModel, {}),
(NNModel, {"n_neighbors": 3}),
(AveragingModel, {}),
(
GDModel,
{
"model_class": NeuralNetwork,
"model_class_args": {
"in_size": 20,
"hidden_size": 100,
"out_size": 1,
},
"opt_alg": "adam",
"batch_size": 100,
"lr": 5e-3,
"num_steps": 100,
},
),
],
"decision_tree": [
(LeastSquaresModel, {}),
(NNModel, {"n_neighbors": 3}),
(DecisionTreeModel, {"max_depth": 4}),
(DecisionTreeModel, {"max_depth": None}),
(XGBoostModel, {}),
(AveragingModel, {}),
],
"noisy_linear_regression": [
(LeastSquaresModel, {}),
(NNModel, {"n_neighbors": 3}),
(AveragingModel, {}),
]
}
models = [model_cls(**kwargs) for model_cls, kwargs in task_to_baselines[task_name]]
return models
class TransformerModel(nn.Module):
def __init__(self, n_dims, n_positions, n_embd=128, n_layer=12, n_head=4):
super(TransformerModel, self).__init__()
configuration = GPT2Config(
n_positions=2 * n_positions,
n_embd=n_embd,
n_layer=n_layer,
n_head=n_head,
resid_pdrop=0.0,
embd_pdrop=0.0,
attn_pdrop=0.0,
use_cache=False,
)
self.name = f"gpt2_embd={n_embd}_layer={n_layer}_head={n_head}"
self.n_positions = n_positions
self.n_dims = n_dims
self._read_in = nn.Linear(n_dims, n_embd)
self._backbone = GPT2Model(configuration)
self._read_out = nn.Linear(n_embd, 1)
@staticmethod
def _combine(xs_b, ys_b):
"""Interleaves the x's and the y's into a single sequence."""
bsize, points, dim = xs_b.shape
ys_b_wide = torch.cat(
(
ys_b.view(bsize, points, 1),
torch.zeros(bsize, points, dim - 1, device=ys_b.device),
),
axis=2,
)
zs = torch.stack((xs_b, ys_b_wide), dim=2)
zs = zs.view(bsize, 2 * points, dim)
return zs
def forward(self, xs, ys, inds=None):
if inds is None:
inds = torch.arange(ys.shape[1])
else:
inds = torch.tensor(inds)
if max(inds) >= ys.shape[1] or min(inds) < 0:
raise ValueError("inds contain indices where xs and ys are not defined")
zs = self._combine(xs, ys)
embeds = self._read_in(zs)
output = self._backbone(inputs_embeds=embeds).last_hidden_state
prediction = self._read_out(output)
return prediction[:, ::2, 0][:, inds] # predict only on xs
class NNModel:
def __init__(self, n_neighbors, weights="uniform"):
# should we be picking k optimally
self.n_neighbors = n_neighbors
self.weights = weights
self.name = f"NN_n={n_neighbors}_{weights}"
def __call__(self, xs, ys, inds=None):
if inds is None:
inds = range(ys.shape[1])
else:
if max(inds) >= ys.shape[1] or min(inds) < 0:
raise ValueError("inds contain indices where xs and ys are not defined")
preds = []
for i in inds:
if i == 0:
preds.append(torch.zeros_like(ys[:, 0])) # predict zero for first point
continue
train_xs, train_ys = xs[:, :i], ys[:, :i]
test_x = xs[:, i : i + 1]
dist = (train_xs - test_x).square().sum(dim=2).sqrt()
if self.weights == "uniform":
weights = torch.ones_like(dist)
else:
weights = 1.0 / dist
inf_mask = torch.isinf(weights).float() # deal with exact match
inf_row = torch.any(inf_mask, axis=1)
weights[inf_row] = inf_mask[inf_row]
pred = []
k = min(i, self.n_neighbors)
ranks = dist.argsort()[:, :k]
for y, w, n in zip(train_ys, weights, ranks):
y, w = y[n], w[n]
pred.append((w * y).sum() / w.sum())
preds.append(torch.stack(pred))
return torch.stack(preds, dim=1)
# xs and ys should be on cpu for this method. Otherwise the output maybe off in case when train_xs is not full rank due to the implementation of torch.linalg.lstsq.
class LeastSquaresModel:
def __init__(self, driver=None):
self.driver = driver
self.name = f"OLS_driver={driver}"
def __call__(self, xs, ys, inds=None):
xs, ys = xs.cpu(), ys.cpu()
if inds is None:
inds = range(ys.shape[1])
else:
if max(inds) >= ys.shape[1] or min(inds) < 0:
raise ValueError("inds contain indices where xs and ys are not defined")
preds = []
for i in inds:
if i == 0:
preds.append(torch.zeros_like(ys[:, 0])) # predict zero for first point
continue
train_xs, train_ys = xs[:, :i], ys[:, :i]
test_x = xs[:, i : i + 1]
ws, _, _, _ = torch.linalg.lstsq(
train_xs, train_ys.unsqueeze(2), driver=self.driver
)
pred = test_x @ ws
preds.append(pred[:, 0, 0])
return torch.stack(preds, dim=1)
class AveragingModel:
def __init__(self):
self.name = "averaging"
def __call__(self, xs, ys, inds=None):
if inds is None:
inds = range(ys.shape[1])
else:
if max(inds) >= ys.shape[1] or min(inds) < 0:
raise ValueError("inds contain indices where xs and ys are not defined")
preds = []
for i in inds:
if i == 0:
preds.append(torch.zeros_like(ys[:, 0])) # predict zero for first point
continue
train_xs, train_ys = xs[:, :i], ys[:, :i]
test_x = xs[:, i : i + 1]
train_zs = train_xs * train_ys.unsqueeze(dim=-1)
w_p = train_zs.mean(dim=1).unsqueeze(dim=-1)
pred = test_x @ w_p
preds.append(pred[:, 0, 0])
return torch.stack(preds, dim=1)
# Lasso regression (for sparse linear regression).
# Seems to take more time as we decrease alpha.
class LassoModel:
def __init__(self, alpha, max_iter=100000):
# the l1 regularizer gets multiplied by alpha.
self.alpha = alpha
self.max_iter = max_iter
self.name = f"lasso_alpha={alpha}_max_iter={max_iter}"
# inds is a list containing indices where we want the prediction.
# prediction made at all indices by default.
def __call__(self, xs, ys, inds=None):
xs, ys = xs.cpu(), ys.cpu()
if inds is None:
inds = range(ys.shape[1])
else:
if max(inds) >= ys.shape[1] or min(inds) < 0:
raise ValueError("inds contain indices where xs and ys are not defined")
preds = [] # predict one for first point
# i: loop over num_points
# j: loop over bsize
for i in inds:
pred = torch.zeros_like(ys[:, 0])
if i > 0:
pred = torch.zeros_like(ys[:, 0])
for j in range(ys.shape[0]):
train_xs, train_ys = xs[j, :i], ys[j, :i]
# If all points till now have the same label, predict that label.
clf = Lasso(
alpha=self.alpha, fit_intercept=False, max_iter=self.max_iter
)
# Check for convergence.
with warnings.catch_warnings():
warnings.filterwarnings("error")
try:
clf.fit(train_xs, train_ys)
except Warning:
print(f"lasso convergence warning at i={i}, j={j}.")
raise
w_pred = torch.from_numpy(clf.coef_).unsqueeze(1)
test_x = xs[j, i : i + 1]
y_pred = (test_x @ w_pred.float()).squeeze(1)
pred[j] = y_pred[0]
preds.append(pred)
return torch.stack(preds, dim=1)
# Gradient Descent and variants.
# Example usage: gd_model = GDModel(NeuralNetwork, {'in_size': 50, 'hidden_size':400, 'out_size' :1}, opt_alg = 'adam', batch_size = 100, lr = 5e-3, num_steps = 200)
class GDModel:
def __init__(
self,
model_class,
model_class_args,
opt_alg="sgd",
batch_size=1,
num_steps=1000,
lr=1e-3,
loss_name="squared",
):
# model_class: torch.nn model class
# model_class_args: a dict containing arguments for model_class
# opt_alg can be 'sgd' or 'adam'
# verbose: whether to print the progress or not
# batch_size: batch size for sgd
self.model_class = model_class
self.model_class_args = model_class_args
self.opt_alg = opt_alg
self.lr = lr
self.batch_size = batch_size
self.num_steps = num_steps
self.loss_name = loss_name
self.name = f"gd_model_class={model_class}_model_class_args={model_class_args}_opt_alg={opt_alg}_lr={lr}_batch_size={batch_size}_num_steps={num_steps}_loss_name={loss_name}"
def __call__(self, xs, ys, inds=None, verbose=False, print_step=100):
# inds is a list containing indices where we want the prediction.
# prediction made at all indices by default.
# xs: bsize X npoints X ndim.
# ys: bsize X npoints.
xs, ys = xs.cuda(), ys.cuda()
if inds is None:
inds = range(ys.shape[1])
else:
if max(inds) >= ys.shape[1] or min(inds) < 0:
raise ValueError("inds contain indices where xs and ys are not defined")
preds = [] # predict one for first point
# i: loop over num_points
for i in tqdm(inds):
pred = torch.zeros_like(ys[:, 0])
model = ParallelNetworks(
ys.shape[0], self.model_class, **self.model_class_args
)
model.cuda()
if i > 0:
pred = torch.zeros_like(ys[:, 0])
train_xs, train_ys = xs[:, :i], ys[:, :i]
test_xs, test_ys = xs[:, i : i + 1], ys[:, i : i + 1]
if self.opt_alg == "sgd":
optimizer = torch.optim.SGD(model.parameters(), lr=self.lr)
elif self.opt_alg == "adam":
optimizer = torch.optim.Adam(model.parameters(), lr=self.lr)
else:
raise NotImplementedError(f"{self.opt_alg} not implemented.")
if self.loss_name == "squared":
loss_criterion = nn.MSELoss()
else:
raise NotImplementedError(f"{self.loss_name} not implemented.")
# Training loop
for j in range(self.num_steps):
# Prepare batch
mask = torch.zeros(i).bool()
perm = torch.randperm(i)
mask[perm[: self.batch_size]] = True
train_xs_cur, train_ys_cur = train_xs[:, mask, :], train_ys[:, mask]
if verbose and j % print_step == 0:
model.eval()
with torch.no_grad():
outputs = model(train_xs_cur)
loss = loss_criterion(
outputs[:, :, 0], train_ys_cur
).detach()
outputs_test = model(test_xs)
test_loss = loss_criterion(
outputs_test[:, :, 0], test_ys
).detach()
print(
f"ind:{i},step:{j}, train_loss:{loss.item()}, test_loss:{test_loss.item()}"
)
optimizer.zero_grad()
model.train()
outputs = model(train_xs_cur)
loss = loss_criterion(outputs[:, :, 0], train_ys_cur)
loss.backward()
optimizer.step()
model.eval()
pred = model(test_xs).detach()
assert pred.shape[1] == 1 and pred.shape[2] == 1
pred = pred[:, 0, 0]
preds.append(pred)
return torch.stack(preds, dim=1)
class DecisionTreeModel:
def __init__(self, max_depth=None):
self.max_depth = max_depth
self.name = f"decision_tree_max_depth={max_depth}"
# inds is a list containing indices where we want the prediction.
# prediction made at all indices by default.
def __call__(self, xs, ys, inds=None):
xs, ys = xs.cpu(), ys.cpu()
if inds is None:
inds = range(ys.shape[1])
else:
if max(inds) >= ys.shape[1] or min(inds) < 0:
raise ValueError("inds contain indices where xs and ys are not defined")
preds = []
# i: loop over num_points
# j: loop over bsize
for i in inds:
pred = torch.zeros_like(ys[:, 0])
if i > 0:
pred = torch.zeros_like(ys[:, 0])
for j in range(ys.shape[0]):
train_xs, train_ys = xs[j, :i], ys[j, :i]
clf = tree.DecisionTreeRegressor(max_depth=self.max_depth)
clf = clf.fit(train_xs, train_ys)
test_x = xs[j, i : i + 1]
y_pred = clf.predict(test_x)
pred[j] = y_pred[0]
preds.append(pred)
return torch.stack(preds, dim=1)
class XGBoostModel:
def __init__(self):
self.name = "xgboost"
# inds is a list containing indices where we want the prediction.
# prediction made at all indices by default.
def __call__(self, xs, ys, inds=None):
xs, ys = xs.cpu(), ys.cpu()
if inds is None:
inds = range(ys.shape[1])
else:
if max(inds) >= ys.shape[1] or min(inds) < 0:
raise ValueError("inds contain indices where xs and ys are not defined")
preds = []
# i: loop over num_points
# j: loop over bsize
for i in tqdm(inds):
pred = torch.zeros_like(ys[:, 0])
if i > 0:
pred = torch.zeros_like(ys[:, 0])
for j in range(ys.shape[0]):
train_xs, train_ys = xs[j, :i], ys[j, :i]
clf = xgb.XGBRegressor()
clf = clf.fit(train_xs, train_ys)
test_x = xs[j, i : i + 1]
y_pred = clf.predict(test_x)
pred[j] = y_pred[0].item()
preds.append(pred)
return torch.stack(preds, dim=1)