forked from waynewu6250/Multi-intent-dialoguer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bert_nsp.py
372 lines (277 loc) · 12.6 KB
/
bert_nsp.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
"""For model pretraining
Data input should be a adjacency pair in dialogue turn.
"""
import random
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch.optim import Adam, RMSprop
from transformers import BertTokenizer, BertModel, BertConfig, AdamW
from keras.preprocessing.sequence import pad_sequences
from sklearn.model_selection import train_test_split
import pickle
import copy
import numpy as np
import collections
import tqdm
from model import BertEmbedding, BertForNextSentence
from all_data import get_dataloader
from config import opt
def load_data(X):
input_ids = pad_sequences(X, maxlen=opt.maxlen, dtype="long", truncating="post", padding="post")
attention_masks = []
segments = []
for seq in input_ids:
# mask
seq_mask = [float(i>0) for i in seq]
attention_masks.append(seq_mask)
# segments
seq = np.array(seq)
seg = np.zeros_like(seq)
pivot = np.where(seq==102)[0]
if len(pivot) == 0:
pass
elif len(pivot) == 1:
seg[pivot[0]:] = 1.0
elif len(pivot) == 2:
seg[pivot[0]+1:pivot[1]+1] = 1.0
segments.append(seg)
return input_ids, attention_masks, segments
def padding(X, seg, mask):
X = [i[0] for i in X]
seg = [i[0] for i in seg]
mask = [i[0] for i in mask]
X = pad_sequences(X, maxlen=opt.maxlen, dtype="long", truncating="post", padding="post")
seg = pad_sequences(seg, maxlen=opt.maxlen, dtype="long", truncating="post", padding="post")
mask = pad_sequences(mask, maxlen=opt.maxlen, dtype="long", truncating="post", padding="post")
return X, seg, mask
def calc_score(outputs, labels):
corrects = 0
totals = 0
if opt.data_mode == 'single':
corrects += torch.sum(torch.max(outputs, 1)[1] == labels)
else:
for i, logits in enumerate(outputs):
log = torch.sigmoid(logits)
correct = (labels[i][torch.where(log>0.5)[0]]).sum()
total = len(torch.where(labels[i]==1)[0])
corrects += correct
totals += total
return corrects, totals
#####################################
def train(**kwargs):
# attributes
for k, v in kwargs.items():
setattr(opt, k, v)
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
torch.backends.cudnn.enabled = False
# dataset
# with open(opt.dic_path, 'rb') as f:
# dic = pickle.load(f)
# with open(opt.train_path, 'rb') as f:
# train_data = pickle.load(f)
# if opt.test_path:
# with open(opt.test_path, 'rb') as f:
# test_data = pickle.load(f)
# all_data = []
# dialogue_id = {}
# dialogue_counter = 0
# counter = 0
# for data in train_data:
# for instance in data:
# all_data.append(instance)
# dialogue_id[counter] = dialogue_counter
# counter += 1
# dialogue_counter += 1
# X, y = zip(*all_data)
# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=42)
# X_train, mask_train, seg_train = load_data(X_train)
# X_test, mask_test, seg_test = load_data(X_test)
# train_loader = get_dataloader_dialogue(X_train, y_train, mask_train, seg_train, len(dic), opt)
# val_loader = get_dataloader_dialogue(X_test, y_test, mask_test, seg_test, len(dic), opt)
# pretrain next-sentence prediction
with open("data/e2e_dialogue/dialogue_data_pretrain.pkl", 'rb') as f:
e2e_data = pickle.load(f)
with open("data/sgd_dialogue/dialogue_data_pretrain.pkl", 'rb') as f:
sgd_data = pickle.load(f)
all_data = e2e_data + sgd_data
#all_data = e2e_data
all_train, all_test = train_test_split(all_data, test_size=0.4, random_state=42)
X_train, seg_train, mask_train, y_train = zip(*all_train)
X_test, seg_test, mask_test, y_test = zip(*all_test)
X_train, seg_train, mask_train = padding(X_train, seg_train, mask_train)
X_test, seg_test, mask_test = padding(X_test, seg_test, mask_test)
train_loader = get_dataloader(X_train, y_train, mask_train, 2, opt, seg_train)
val_loader = get_dataloader(X_test, y_test, mask_test, 2, opt, seg_test)
# model
config = BertConfig(vocab_size_or_config_json_file=32000, hidden_size=768,
num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072)
model = BertForNextSentence(config, 2)
if opt.model_path:
model.load_state_dict(torch.load(opt.model_path))
print("Pretrained model has been loaded.\n")
model = model.to(device)
# optimizer, criterion
param_optimizer = list(model.named_parameters())
no_decay = ['bias', 'gamma', 'beta']
optimizer_grouped_parameters = [
{'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)],
'weight_decay_rate': 0.01},
{'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)],
'weight_decay_rate': 0.0}
]
optimizer = AdamW(model.parameters(), weight_decay=0.01, lr=opt.learning_rate_bert)
if opt.data_mode == 'single':
criterion = nn.CrossEntropyLoss().to(device)
else:
criterion = nn.BCEWithLogitsLoss(reduction='sum').to(device)
best_loss = 10000
best_accuracy = 0
# Start training
for epoch in range(opt.epochs):
print("====== epoch %d / %d: ======"% (epoch, opt.epochs))
# Training Phase
total_train_loss = 0
train_corrects = 0
totals = 0
model.train()
for (captions_t, labels, masks, segs) in tqdm.tqdm(train_loader):
captions_t = captions_t.to(device)
labels = labels.to(device)
masks = masks.to(device)
segs = segs.to(device)
optimizer.zero_grad()
#train_loss = model(captions_t, masks, labels)
_, _, outputs = model(captions_t, masks, segs)
train_loss = criterion(outputs, labels)
train_loss.backward()
optimizer.step()
total_train_loss += train_loss
co, to = calc_score(outputs, labels)
train_corrects += co
totals += to
train_acc = train_corrects.double() / train_loader.dataset.num_data if opt.data_mode == 'single' else train_corrects.double() / totals
print('Average train loss: {:.4f} '.format(total_train_loss / train_loader.dataset.num_data))
print('Train accuracy: {:.4f}'.format(train_acc))
# Validation Phase
total_val_loss = 0
val_corrects = 0
totals = 0
model.eval()
for (captions_t, labels, masks, segs) in val_loader:
captions_t = captions_t.to(device)
labels = labels.to(device)
masks = masks.to(device)
segs = segs.to(device)
with torch.no_grad():
_, pooled_output, outputs = model(captions_t, masks)
val_loss = criterion(outputs, labels)
total_val_loss += val_loss
co, to = calc_score(outputs, labels)
val_corrects += co
totals += to
val_acc = val_corrects.double() / val_loader.dataset.num_data if opt.data_mode == 'single' else val_corrects.double() / totals
print('Average val loss: {:.4f} '.format(total_val_loss / val_loader.dataset.num_data))
print('Val accuracy: {:.4f}'.format(val_acc))
if val_acc > best_accuracy:
print('saving with loss of {}'.format(total_val_loss),
'improved over previous {}'.format(best_loss))
best_loss = total_val_loss
best_accuracy = val_acc
torch.save(model.state_dict(), 'checkpoints/best_{}_pretrain.pth'.format(opt.datatype))
print()
print('Best total val loss: {:.4f}'.format(total_val_loss))
print('Best Test Accuracy: {:.4f}'.format(best_accuracy))
def test(**kwargs):
#dataset
with open(opt.woz_path, 'rb') as f:
train_data = pickle.load(f)
with open(opt.woz_dic_path, 'rb') as f:
dic = pickle.load(f)
reverse_dic = {v: k for k,v in dic.items()}
all_data = []
dialogue_id = {}
dialogue_counter = 0
counter = 0
for data in train_data:
for instance in data:
all_data.append(instance)
dialogue_id[counter] = dialogue_counter
counter += 1
dialogue_counter += 1
with open(opt.woz_dialogue_id_path, 'wb') as f:
pickle.dump(dialogue_id, f)
X, y = zip(*all_data)
model_path = opt.woz_model_path
embedding_path = opt.woz_embedding_path
# attributes
for k, v in kwargs.items():
setattr(opt, k, v)
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
torch.backends.cudnn.enabled = False
# model
config = BertConfig(vocab_size_or_config_json_file=32000, hidden_size=768,
num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072)
model = BertEmbedding(config, len(dic))
if model_path:
model.load_state_dict(torch.load(model_path))
print("Pretrained model has been loaded.\n")
model = model.to(device)
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased', do_lower_case=True)
# Store embeddings
if opt.mode == "embedding":
X_train, mask_train, seg_train = load_data(X)
train_loader = get_dataloader_dialogue(X_train, y, mask_train, seg_train, len(dic), opt)
results = []
for i, (captions_t, labels, masks, segs) in enumerate(train_loader):
captions_t = captions_t.to(device)
labels = labels.to(device)
masks = masks.to(device)
segs = segs.to(device)
with torch.no_grad():
# Skip the last sentence in each dialogue at this time
for ii in range(len(labels)):
seq = torch.zeros(1, 25).to(device)
mask = torch.zeros(1, 25).to(device)
seg = torch.zeros(1, 25).to(device)
pivot = torch.where(captions_t[ii]==102)[0]
if len(pivot) > 0 and pivot[0] < 25:
seq[0][:pivot[0]+1] = captions_t[ii][:pivot[0]+1]
mask[0][:pivot[0]+1] = (captions_t[ii][:pivot[0]+1]>0).long()
seg[0][:pivot[0]+1] = segs[ii][:pivot[0]+1]
else:
seq[0] = captions_t[ii][:25]
mask[0] = (captions_t[ii][:25]>0).long()
seg[0] = segs[ii][:25]
hidden_states, pooled_output, outputs = model(seq.long(), mask.long(), seg.long())
#hidden_states, pooled_output, outputs = model(captions_t, masks, segs)
# one hot to sparse index
key = torch.where(labels[ii]==1)[0].data.cpu().numpy()
embedding = pooled_output[0].data.cpu().numpy().reshape(-1)
word_embeddings = hidden_states[-1][0].data.cpu().numpy()
tokens = tokenizer.convert_ids_to_tokens(seq[0].data.cpu().numpy())
# embedding = pooled_output[ii].data.cpu().numpy().reshape(-1)
# word_embeddings = hidden_states[-1][ii].data.cpu().numpy()
# tokens = tokenizer.convert_ids_to_tokens(captions_t[ii].data.cpu().numpy())
tokens = [token for token in tokens if token != "[CLS]" and token != "[SEP]" and token != "[PAD]"]
original_sentence = " ".join(tokens)
results.append((original_sentence, embedding, word_embeddings, key))
print("Saving Data: %d" % i)
if i == 50:
break
torch.save(results, embedding_path)
# Run test classification
elif opt.mode == "data":
index = np.random.randint(0, len(X_test), 1)[0]
input_ids = X_test[index]
attention_masks = mask_test[index]
print(" ".join(tokenizer.convert_ids_to_tokens(input_ids)))
captions_t = torch.LongTensor(input_ids).unsqueeze(0).to(device)
mask = torch.LongTensor(attention_masks).unsqueeze(0).to(device)
with torch.no_grad():
pooled_output, outputs = model(captions_t, mask)
print("Predicted label: ", reverse_dic[torch.max(outputs, 1)[1].item()])
print("Real label: ", reverse_dic[y_test[index]])
if __name__ == '__main__':
import fire
fire.Fire()