-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHBERT_simple.py
More file actions
488 lines (312 loc) · 16.6 KB
/
HBERT_simple.py
File metadata and controls
488 lines (312 loc) · 16.6 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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 17 11:05:38 2019
@author: peterawest
"""
import random
from pytorch_pretrained_bert import BertTokenizer, BertModel
import torch
import numpy as np
import math
import os
import time
from word_embeddings import BERT_word_embedding
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
bert = BertModel.from_pretrained('bert-base-uncased')
bert.eval()
bert.to('cuda')
sig = torch.nn.Sigmoid()
from torch import nn
MAX_LENGTH = 100
sm_1 = torch.nn.Softmax(dim=1)
sm = torch.nn.Softmax()
max_posts = 1000
# calculates position embeddings given seq_len and n_dim
def position_embeddings(n_dim, seq_len):
inds = torch.arange(n_dim).expand([seq_len,n_dim]).float().to('cuda')
pos = torch.arange(seq_len).view(-1,1).expand([seq_len, n_dim]).float().to('cuda')
position_embeddings = (inds > n_dim/2).float()*torch.sin(pos/(1000**(2*inds/n_dim)) ) + (inds <= n_dim/2).float()*torch.cos(pos/(1000**(2*inds/n_dim)) )
return position_embeddings
class AttnDot_batch(nn.Module):
def __init__(self, hidden_size):
torch.nn.Module.__init__(self)
self.hidden_size = hidden_size
#!!! initialize this a better way
self.query_vec = torch.nn.parameter.Parameter(torch.randn(hidden_size).to('cuda'))
def forward(self, input, attn_mask = None):
inp_shape = input.shape
input = input.view(inp_shape[0]*inp_shape[1], inp_shape[2])
attn_logits = input.matmul(self.query_vec.view(1,-1,1)).view(inp_shape[0],inp_shape[1])
if attn_mask is not None:
logits = attn_logits - 100000*(attn_mask == 0).float()
else:
logits = attn_logits
attn_weights = sm(logits)
weights = attn_weights*((attn_weights == attn_weights).float()) + 0*((attn_weights != attn_weights).float()) #attn_weights[attn_weights != attn_weights] = 0
#assert((weights.sum(dim=1) < 1.0001).all() and (weights.sum(dim=1) > 0.999).all())
input = input.view(inp_shape[0], inp_shape[1], inp_shape[2])
weighted = weights.unsqueeze(2)*input
rep = weighted.sum(dim=1)
return rep
from pytorch_pretrained_bert import BertTokenizer, BertModel
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
bert = BertModel.from_pretrained('bert-base-uncased')
bert.eval()
bert.to('cuda')
sig = torch.nn.Sigmoid()
from pytorch_pretrained_bert import BertTokenizer, BertModel
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
bert = BertModel.from_pretrained('bert-base-uncased')
bert.eval()
bert.to('cuda')
sig = torch.nn.Sigmoid()
class Hierarchical_BERT(nn.Module):
def __init__(self, h_size_sent, h_size_user, tokenize = True, max_posts=1000,
max_len = 512, max_tokens_batch = 10000, temp_save = None, PW = False,
seq = False):
torch.nn.Module.__init__(self)
self.h_size_sent = h_size_sent
self.h_size_user = h_size_user
self.linear0 = torch.nn.Linear(768,h_size_sent).to('cuda')
self.attn_post = AttnDot_batch(h_size_sent)
self.linear1 = torch.nn.Linear(h_size_sent,h_size_user).to('cuda')
self.attn_user = AttnDot_batch(h_size_user)
self.PW = PW
if self.PW:
self.classifier_0 = torch.nn.Linear(h_size_user + 1,50).to('cuda')
self.classifier_1 = torch.nn.Linear(50,1).to('cuda')
else:
self.classifier = torch.nn.Linear(h_size_user,1).to('cuda')
# whether or not HBERT model will have to tokenize (or data is pretokenized)
self.tokenize = tokenize
self.max_posts = max_posts
self.max_len = max_len
self.max_tokens_batch = max_tokens_batch
self.temp_save = temp_save
self.bert_embedding = BERT_word_embedding(tokenize = self.tokenize, max_tokens_batch=self.max_tokens_batch,
temp_save = self.temp_save, max_len=self.max_len,max_posts=self.max_posts)
self.seq = seq
if self.seq:
self.seq_w = torch.nn.parameter.Parameter(torch.randn(1).to('cuda'))
def preembed(self, post_list):
flat = []
for posts in post_list:
flat += posts
embedding_tensor, attention_mask = self.bert_embedding( flat )
lens = [0] + [len(posts) for posts in post_list]
for i in range(1,len(lens)):
lens[i] = lens[i] + lens[i-1]
embedding_list = [ embedding_tensor[lens[i]:lens[i+1]] for i in range(len(lens) - 1)]
attention_list = [ attention_mask[lens[i]:lens[i+1]] for i in range(len(lens) - 1)]
return list(zip(embedding_list, attention_list))
def forward(self, X, precalc = None, preembedded = None):
# post_reps = []
#
# for post in posts:
# post_reps += [self.forward_post(post).view(1,-1)]
#
# post_tensor = torch.cat(post_reps,dim=0).unsqueeze(0)
if self.PW:
posts, treatment = X[0], X[1]
else:
posts= X
if preembedded is not None:
embedding_tensor, attention_mask = preembedded
# print('e_ten = {}'.format(embedding_tensor.shape[0]))
# print('posts = {}'.format(len(posts)))
#assert(embedding_tensor.shape[0] == len(posts))
else:
embedding_tensor, attention_mask = self.bert_embedding( posts )
# if sequential, add sine waves underneath!
if self.seq:
embedding_tensor, attention_mask = self.bert_embedding( posts )
temp = self.linear1( self.attn_post( self.linear0( embedding_tensor ), attn_mask = attention_mask)).unsqueeze(0)
temp = self.forward_post_reps(posts, precalc)
post_tensor = temp + self.seq_w*position_embeddings(self.h_size_user, temp.shape[1]).unsqueeze(0)
else:
post_tensor = self.linear1( self.attn_post( self.linear0( embedding_tensor ), attn_mask = attention_mask)).unsqueeze(0)
#assert(post_tensor.shape[1] == len(X))
user_rep = self.attn_user(post_tensor)
if self.PW:
treat_tensor = torch.tensor([treatment]).float().to('cuda')
classifier_input = torch.cat([user_rep,treat_tensor])
out = sig(self.classifier_1( sig(self.classifier_0(classifier_input) ) ).view(1) )
else:
classifier_input = user_rep
out = sig(self.classifier(classifier_input).view(1))
return out
class Average_BERT(nn.Module):
def __init__(self, h_size_sent, h_size_user, tokenize = True, max_posts=1000,
max_len = 512, max_tokens_batch = 10000, temp_save = None, PW = False):
torch.nn.Module.__init__(self)
self.linear0 = torch.nn.Linear(768,h_size_user.to('cuda'))
#self.linear1 = torch.nn.Linear(h_size_sent,h_size_user).to('cuda')
self.PW = PW
if self.PW:
self.classifier_0 = torch.nn.Linear(h_size_user + 1,50).to('cuda')
self.classifier_1 = torch.nn.Linear(50,1).to('cuda')
else:
self.classifier = torch.nn.Linear(h_size_user,1).to('cuda')
# whether or not HBERT model will have to tokenize (or data is pretokenized)
self.tokenize = tokenize
self.max_posts = max_posts
self.max_len = max_len
self.max_tokens_batch = max_tokens_batch
self.temp_save = temp_save
self.bert_embedding = BERT_word_embedding(tokenize = self.tokenize, max_tokens_batch=self.max_tokens_batch,
temp_save = self.temp_save, max_len=self.max_len,max_posts=self.max_posts)
def preembed(self, post_list):
flat = []
for posts in post_list:
flat += posts
embedding_tensor, attention_mask = self.bert_embedding( flat )
lens = [0] + [len(posts) for posts in post_list]
for i in range(1,len(lens)):
lens[i] = lens[i] + lens[i-1]
embedding_list = [ embedding_tensor[lens[i]:lens[i+1]] for i in range(len(lens) - 1)]
attention_list = [ attention_mask[lens[i]:lens[i+1]] for i in range(len(lens) - 1)]
return list(zip(embedding_list, attention_list))
def forward(self, X, precalc = None, preembedded = None):
# post_reps = []
#
# for post in posts:
# post_reps += [self.forward_post(post).view(1,-1)]
#
# post_tensor = torch.cat(post_reps,dim=0).unsqueeze(0)
if self.PW:
posts, treatment = X[0], X[1]
else:
posts= X
if preembedded is not None:
embedding_tensor, attention_mask = preembedded
# print('e_ten = {}'.format(embedding_tensor.shape[0]))
# print('posts = {}'.format(len(posts)))
#assert(embedding_tensor.shape[0] == len(posts))
else:
embedding_tensor, attention_mask = self.bert_embedding( posts )
for i in attention_mask.shape[0]:
embedding_tensor[:, attention_mask == 0] = 0. # zero out omitted vectors
post_tensor = self.linear0( embedding_tensor ).mean(dim=1)
user_rep = post_tensor.mean(dim = 0)
if self.PW:
treat_tensor = torch.tensor([treatment]).float().to('cuda')
classifier_input = torch.cat([user_rep,treat_tensor])
out = sig(self.classifier_1( sig(self.classifier_0(classifier_input) ) ).view(1) )
else:
classifier_input = user_rep
out = sig(self.classifier(classifier_input).view(1))
return out
class Hierarchical_BERT_propensity_model():
def __init__(self, n_it = 100000, val_interval = 1000, batch_size = 1, lr = 0.001, h_size_sent = 768, h_size_user = 768, tokenize = True, max_tokens_batch = 10000,
precalc_path = None, experiment_name = 'hbert', PW = False, seq = False, preembed_size = 10,
agg = 'attn'):
if agg is 'attn':
self.hb = Hierarchical_BERT(h_size_sent=h_size_sent, h_size_user=h_size_user, tokenize = tokenize, max_tokens_batch = max_tokens_batch, PW = PW, seq=seq)
elif agg is 'avg':
self.hb = Average_BERT(h_size_sent=h_size_sent, h_size_user=h_size_user, tokenize = tokenize, max_tokens_batch = max_tokens_batch, PW = PW)
else:
assert(False)
self.batch_size = batch_size
self.val_interval = val_interval
self.n_it = n_it # number of training iterations
self.lr = lr
self.precalc_path= precalc_path
assert(val_interval < n_it)
print('val_interval is: {}'.format(val_interval))
self.experiment_name = experiment_name
self.preembed_size = preembed_size
def fit(self, dataset, verbose = True):
self.learning_curve = []
opt = torch.optim.Adam(self.hb.parameters(), lr=self.lr)
opt.zero_grad()
preembed_size = self.preembed_size
min_val_loss = None
t_start = time.time()
iterations = []
posts_list = []
### loop over sgd iterations
for it_, (posts_,label_,_, ind_) in enumerate(dataset.train_epoch(size=self.n_it, include_ind = True)):
# if it_ % 100 == 0:
# print('at it_ {}, time is {}'.format(it_,time.time() - t_start))
# ex = random.randint(0, len(X_train) - 1)
# posts = X_train[ex]
# label = Z_train[ex]
if len(posts_) > 1000:
print('posts is too long ({})'.format(len(posts_)))
random.shuffle(posts_)
posts_ = posts_[:1000]
iterations += [(it_, posts_,label_, ind_)]
posts_list += [posts_]
# print('it_:{}'.format(it_))
# if
if ((it_ + 1) % preembed_size) == 0:
# print('it_ = {}'.format(it_))
# print('preembed time')
preembedded = self.hb.preembed(posts_list)
for j, iteration in enumerate(iterations):
it, posts,label, ind = iteration
# if precalculating BERT reps, do this here
if False:# self.precalc_path is not None:
precalc = self.precalc_path + 'ex_{}'.format(ind)
logit = self.hb.forward(posts, precalc = precalc)
else:
logit = self.hb.forward(posts, preembedded = preembedded[j])
#logit = self.hb.forward(posts)
loss = -(float(label)*torch.log(logit) + float(1-label)*torch.log(1-logit))
if math.isnan(loss.item()):
print('Is NAN! iteration: {}',format(it))
self.lr = self.lr/2.
print('reloading model, dropping lr to {}'.format(self.lr))
self.load_best()
opt = torch.optim.Adam(self.hb.parameters(), lr=self.lr)
opt.zero_grad()
continue
(loss/self.batch_size).backward()
if ((it % self.batch_size) == 0) and it > 0:
torch.cuda.empty_cache()
opt.step()
opt.zero_grad()
torch.cuda.empty_cache()
if ((it) % self.val_interval) == 0:
print('time to validate')
t_val_start = time.time()
torch.cuda.empty_cache()
with torch.no_grad():
#print('starting validation')
val_loss = 0
for val_i, (posts, label,_) in enumerate(dataset.valid_epoch()):
# posts = X_val[val_i]
# label = Z_val[val_i]
logit = self.hb.forward(posts)
loss = -(float(label)*torch.log(logit) + float((1-label))*torch.log(1-logit))
val_loss += float(loss.item())
self.learning_curve += [val_loss]
#print('val_loss: {}'.format(val_loss))
if min_val_loss is None or val_loss < min_val_loss:
min_val_loss = val_loss
self.save_best()
# torch.save(self.hb.state_dict(),'best.pt')
elif val_loss > 1.5*min_val_loss:
break
if verbose:
print('it: {}, val_loss: {}, time: {}'.format(it ,val_loss, time.time() - t_start))
print('time to val: {}'.format(time.time() - t_val_start))
# print('time = {}'.format(time.time() - t_start))
iterations = []
posts_list = []
self.load_best()
# self.hb.load_state_dict(torch.load('best.pt'))
def load_best(self):
self.hb.load_state_dict(torch.load('{}_best.pt'.format(self.experiment_name)))
def save_best(self):
torch.save(self.hb.state_dict(),'{}_best.pt'.format(self.experiment_name))
def score(self, X):
n_ex = len(X)
scores = np.zeros(n_ex)
for i in range(len(X)):
data_in = X[i]
with torch.no_grad():
logit = self.hb.forward(data_in).item()
scores[i] = logit
return scores