forked from msamogh/schema_attention_model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
441 lines (371 loc) · 19.3 KB
/
train.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
import argparse
import json
import numpy as np
import os
import random
import torch
from typing import Any, Dict, Tuple
from collections import Counter, defaultdict
from torch.utils.data import DataLoader
from tqdm import tqdm, trange
from transformers import AdamW, GPT2Tokenizer, GPT2LMHeadModel
from tokenizers import BertWordPieceTokenizer
from data_readers import filter_dataset, NextActionDataset, NextActionSchema
from models import ActionBertModel, SchemaActionBertModel
from sklearn.metrics import f1_score
from nltk.translate.bleu_score import corpus_bleu
from sklearn.preprocessing import MultiLabelBinarizer
def read_args():
parser = argparse.ArgumentParser()
parser.add_argument("--data_path", type=str)
parser.add_argument("--schema_path", type=str)
parser.add_argument("--token_vocab_path", type=str)
parser.add_argument("--output_dir", type=str, default='')
parser.add_argument("--action_output_dir", type=str, default='')
parser.add_argument("--model_name_or_path", type=str, default="bert-base-uncased")
parser.add_argument("--task", type=str, choices=["action", "generation"])
parser.add_argument("--use_schema", action="store_true")
parser.add_argument("--grad_accum", type=int, default=1)
parser.add_argument("--train_batch_size", type=int, default=64)
parser.add_argument("--max_seq_length", type=int, default=100)
parser.add_argument("--schema_max_seq_length", type=int, default=50)
parser.add_argument("--num_epochs", type=int, default=10)
parser.add_argument("--dropout", type=float, default=0.5)
parser.add_argument("--learning_rate", default=5e-5, type=float, help="The initial learning rate for Adam.")
parser.add_argument("--adam_epsilon", default=1e-8, type=float, help="Epsilon for Adam optimizer.")
parser.add_argument("--weight_decay", default=0.0, type=float, help="Weight decay if we apply some.")
parser.add_argument("--device", default=0, type=int, help="GPU device #")
parser.add_argument("--max_grad_norm", default=-1.0, type=float, help="Max gradient norm.")
parser.add_argument("--seed", type=int, default=42)
return parser.parse_args()
def evaluate(model,
eval_dataloader,
schema_dataloader,
tokenizer,
task,
device=0,
args=None):
# Get schema pooled outputs
if args.use_schema and task == "action":
with torch.no_grad():
sc_batch = next(iter(schema_dataloader))
if torch.cuda.is_available():
for key, val in sc_batch.items():
if type(sc_batch[key]) is list:
continue
sc_batch[key] = sc_batch[key].to(device)
sc_all_output, sc_pooled_output = model.bert_model(input_ids=sc_batch["input_ids"],
attention_mask=sc_batch["attention_mask"],
token_type_ids=sc_batch["token_type_ids"],
return_dict=False)
sc_action_label = sc_batch["action"]
sc_tasks = sc_batch["task"]
pred = []
true = []
sentence = []
model.eval()
for batch in tqdm(eval_dataloader, desc="Evaluating"):
with torch.no_grad():
if task == "action":
# Move to GPU
if torch.cuda.is_available():
for key, val in batch.items():
if type(batch[key]) is list:
continue
batch[key] = batch[key].to(device)
if args.use_schema:
action_logits, _ = model.predict(input_ids=batch["input_ids"],
attention_mask=batch["attention_mask"],
token_type_ids=batch["token_type_ids"],
tasks=batch["tasks"],
sc_all_output=sc_all_output,
sc_pooled_output=sc_pooled_output,
sc_tasks=sc_tasks,
sc_action_label=sc_action_label)
else:
action_logits, _ = model(input_ids=batch["input_ids"],
attention_mask=batch["attention_mask"],
token_type_ids=batch["token_type_ids"])
# Argmax to get predictions
action_preds = torch.argmax(action_logits, dim=1).cpu().tolist()
pred += action_preds
true += batch["action"].cpu().tolist()
sentence += [tokenizer.decode(e.tolist(), skip_special_tokens=False).replace(" [PAD]", "") for e in batch["input_ids"]]
# Perform evaluation
if task == "action":
acc = sum(p == t for p,t in zip(pred, true))/len(pred)
f1 = f1_score(true, pred, average='weighted')
print(acc, f1)
id_map = eval_dataloader.dataset.action_label_to_id
label_map = sorted(id_map, key=id_map.get)
print("INCORRECT ==========================================================")
for i in range(len(true)):
if pred[i] != true[i]:
print(sentence[i] + "\n", label_map[true[i]], label_map[pred[i]])
print("CORRECT ==========================================================")
for i in range(len(true)):
if pred[i] == true[i]:
print(sentence[i] + "\n", label_map[true[i]], label_map[pred[i]])
return f1, acc
def train(args, exp_setting=None):
# Set random seed
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
elif args.num_epochs == 0:
# This means we're evaluating. Don't create the directory.
pass
else:
#args.num_epochs = 0
raise Exception("Directory {} already exists".format(args.output_dir))
# Dump arguments to the checkpoint directory, to ensure reproducability.
if args.num_epochs > 0:
json.dump(args.__dict__, open(os.path.join(args.output_dir, 'args.json'), "w+"))
torch.save(args, os.path.join(args.output_dir, "run_args"))
# Configure tokenizer
token_vocab_name = os.path.basename(args.token_vocab_path).replace(".txt", "")
tokenizer = BertWordPieceTokenizer(args.token_vocab_path,
lowercase=True)
tokenizer.enable_padding(length=args.max_seq_length)
sc_tokenizer = BertWordPieceTokenizer(args.token_vocab_path,
lowercase=True)
sc_tokenizer.enable_padding(length=args.schema_max_seq_length)
# Data readers
if args.task == "action":
dataset_initializer = NextActionDataset
elif args.task == "generation":
dataset_initializer = ResponseGenerationDataset
else:
raise ValueError("Not a valid task type: {}".format(args.task))
dataset = dataset_initializer(args.data_path,
tokenizer,
args.max_seq_length,
token_vocab_name,
action_mapping_from_schemas=True,
schema_path=args.schema_path)
#dataset.examples = [e for e in dataset if 'weather' in e['tasks']]
# Get the action to id mapping
if args.task == "generation":
action_dataset = NextActionDataset(args.data_path,
tokenizer,
args.max_seq_length,
token_vocab_name,
action_mapping_from_schema=True,
schema_path=args.schema_path)
action_label_to_id = action_dataset.action_label_to_id
actions = sorted(action_label_to_id, key=action_label_to_id.get)
if exp_setting is not None:
if "domain" in exp_setting:
data_type = exp_setting.get("data_type")
train_dataset = filter_dataset(dataset,
data_type=data_type,
percentage=1.0,
domain=exp_setting.get("domain"),
exclude=True,
train=True)
test_dataset = filter_dataset(dataset,
data_type=data_type,
percentage=1.0,
domain=exp_setting.get("domain"),
exclude=False,
train=False)
elif "task" in exp_setting:
data_type = exp_setting.get("data_type")
train_dataset = filter_dataset(dataset,
data_type=data_type,
percentage=1.0,
task=exp_setting.get("task"),
exclude=True,
train=True)
test_dataset = filter_dataset(dataset,
data_type=data_type,
percentage=1.0,
task=exp_setting.get("task"),
exclude=False,
train=False)
else:
data_type = exp_setting.get("data_type")
train_dataset = filter_dataset(dataset,
data_type=data_type,
percentage=0.8,
train=True)
test_dataset = filter_dataset(dataset,
data_type=data_type,
percentage=0.2,
train=False)
# Load the schema for the next action prediction
schema = NextActionSchema(args.schema_path,
sc_tokenizer,
args.schema_max_seq_length,
dataset.action_label_to_id if args.task == "action" else action_label_to_id,
token_vocab_name)
# Data loaders
train_dataset.examples = sorted(train_dataset.examples, key=lambda i:i['tasks'])
train_dataloader = DataLoader(dataset=train_dataset,
batch_size=args.train_batch_size,
shuffle=False,
num_workers=0)
schema_train_dataloader = DataLoader(dataset=schema,
batch_size=min(len(schema), args.train_batch_size),
pin_memory=True,
shuffle=True)
schema_test_dataloader = DataLoader(dataset=schema,
batch_size=len(schema),
pin_memory=True,
shuffle=True)
test_dataloader = DataLoader(dataset=test_dataset,
batch_size=args.train_batch_size,
pin_memory=True)
print("Training set size", len(train_dataloader.dataset))
print("Testing set size", len(test_dataloader.dataset))
print("Experimental Setting", exp_setting)
# Load model
if args.task == "action":
if args.use_schema:
print(f"Just in: Num actions = {len(train_dataset.action_label_to_id)}")
model = SchemaActionBertModel(args.model_name_or_path,
dropout=args.dropout,
num_action_labels=len(train_dataset.action_label_to_id))
else:
model = ActionBertModel(args.model_name_or_path,
dropout=args.dropout,
num_action_labels=len(train_dataset.action_label_to_id))
if torch.cuda.is_available():
model.to(args.device)
else:
raise ValueError("Cannot instantiate model for task: {}".format(args.task))
if args.task == "action":
optimizer = AdamW(model.parameters(), lr=args.learning_rate, eps=args.adam_epsilon)
best_score = -1
model.tokenizer = tokenizer
for epoch in trange(args.num_epochs, desc="Epoch"):
model.train()
epoch_loss = 0
num_batches = 0
model.zero_grad()
for batch in tqdm(train_dataloader):
num_batches += 1
# Transfer to gpu
if torch.cuda.is_available():
for key, val in batch.items():
if type(batch[key]) is list:
continue
batch[key] = batch[key].to(args.device)
# Train model
if args.use_schema:
print("Using schema.")
# Get schema batch and move to GPU
sc_batch = next(iter(schema_test_dataloader))
# Filter out only the relevant actions
relevant_inds = []
batch_actions = set(batch['action'].tolist())
batch_tasks = set(batch['tasks'])
batch_action_tasks = [(batch['action'][i].item(), batch['tasks'][i]) for i in range(len(batch['action']))]
for i, action in enumerate(sc_batch['action'].tolist()):
if (action, sc_batch['task'][i]) in batch_action_tasks:
relevant_inds.append(i)
# Filter out sc batch to only relevant inds
sc_batch = {
k: [v[i] for i in relevant_inds] if type(v) is list else v[relevant_inds]
for k, v in sc_batch.items()
}
if torch.cuda.is_available():
for key, val in sc_batch.items():
if type(sc_batch[key]) is list:
continue
sc_batch[key] = sc_batch[key].to(args.device)
print("Moved data to GPU.")
else:
print("Could not move data to GPU.")
_, loss = model(input_ids=batch["input_ids"],
attention_mask=batch["attention_mask"],
token_type_ids=batch["token_type_ids"],
tasks=batch["tasks"],
action_label=batch["action"],
sc_input_ids=sc_batch["input_ids"],
sc_attention_mask=sc_batch["attention_mask"],
sc_token_type_ids=sc_batch["token_type_ids"],
sc_tasks=sc_batch["task"],
sc_action_label=sc_batch["action"])
print("Model loss computed (1).")
else:
_, loss = model(input_ids=batch["input_ids"],
attention_mask=batch["attention_mask"],
token_type_ids=batch["token_type_ids"],
action_label=batch["action"])
print("Model loss computed (2).")
if args.grad_accum > 1:
loss = loss / args.grad_accum
loss.backward()
epoch_loss += loss.item()
print("Backprop done.")
if args.grad_accum <= 1 or num_batches % args.grad_accum == 0:
if args.max_grad_norm > 0:
torch.nn.utils.clip_grad_norm_(model.parameters(), args.max_grad_norm)
optimizer.step()
print("Gradient update done.")
model.zero_grad()
print("Zero grad called.")
print("Epoch loss: {}".format(epoch_loss / num_batches))
if args.num_epochs == 0:
model.load_state_dict(torch.load(os.path.join(args.output_dir, "model.pt")))
else:
torch.save(model.state_dict(), os.path.join(args.output_dir, "model.pt"))
score = evaluate(model, test_dataloader, schema_test_dataloader, tokenizer, task=args.task, args=args)
print("Best result for {}: Score: {}".format(args.task, score))
return score
if __name__ == "__main__":
args = read_args()
print(args)
domains = ['ride', 'trip', 'plane', 'spaceship', 'meeting', 'weather', 'party', 'doctor', 'trivia', 'apartment', 'restaurant', 'hotel', 'bank']
tasks = ['hotel_service_request', 'bank_balance', 'weather', 'bank_fraud_report', 'party_rsvp', 'apartment_search', 'trivia', 'ride_book', 'apartment_schedule', 'hotel_book', 'ride_status', 'restaurant_search', 'doctor_schedule', 'doctor_followup', 'restaurant_book', 'plane_search', 'meeting_schedule', 'party_plan', 'plane_book', 'spaceship_access_codes', 'hotel_search', 'trip_directions']
# UNCOMMENT FOR STANDARD EXPERIMENTS
#exp_setting = {"data_type": "happy"}
#score = train(args, exp_setting)
#print(score)
scores = []
# Use old scores if experiment crashes.
old_scores = []
orig_dir = args.action_output_dir
orig_output_dir = args.output_dir
# ZERO-SHOT TASK TRANSFER EXPERIMENTS
for i,task in enumerate(tasks):
if task != "ride_book":
# Don't waste time training other zero-shot experiments (as we won't be using them).
continue
print("TASK", task)
exp_setting = {"task": task, "data_type": "happy"}
args.action_output_dir = orig_dir + task + "/"
args.output_dir = orig_output_dir + task + "/"
if i < len(old_scores):
scores.append(old_scores[i])
else:
scores.append(train(args, exp_setting))
print(scores)
if args.task == "generation":
print(np.mean([e[0] for e in scores]))
print(np.mean([e[1] for e in scores]))
print(np.mean([e[2] for e in scores]))
else:
print("f1", np.mean([e[0] for e in scores]))
print("acc", np.mean([e[1] for e in scores]))
# ZERO-SHOT DOMAIN TRANSFER EXPERIMENTS
#for i,task in enumerate(domains):
# print("DOMAIN", task)
# exp_setting = {"domain": task, "data_type": "happy"}
# args.action_output_dir = orig_dir + task + "/"
# args.output_dir = orig_output_dir + task + "/"
# if i < len(old_scores):
# scores.append(old_scores[i])
# else:
# scores.append(train(args, exp_setting))
# print(scores)
# if args.task == "generation":
# print(np.mean([e[0] for e in scores]))
# print(np.mean([e[1] for e in scores]))
# print(np.mean([e[2] for e in scores]))
# else:
# print("f1", np.mean([e[0] for e in scores]))
# print("acc", np.mean([e[1] for e in scores]))