-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
593 lines (517 loc) · 24.9 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
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
import argparse
from Utils.fix_seed import fix_seed
import torch
import logging
from Data.dataset import RGTDataset, SeqDataset, SingleGraphDataset, TreeDataset
from Model.model import RGT, RelativeTransformer, AbsoluteTransformer, BiLSTM, GAT, GCN, TreeLSTM
from Utils.const import UP_SCHEMA_NUM, UP_TYPE_NUM, DOWN_TYPE_NUM, TYPE_NUM, RGT_VOCAB_PATH, RGT_MODEL_PATH, SEQ_VOCAB_PATH, RELATIVE_MODEL_PATH, TRANSFORMER_MODEL_PATH, BILSTM_MODEL_PATH, SINGLE_GRAPH_VOCAB_PATH, GAT_MODEL_PATH, GCN_MODEL_PATH, TREE_VOCAB_PATH, TREE_MODEL_PATH
import os
from torch.utils.data import DataLoader
from Data.utils import get_RGT_batch_data, get_seq_batch_data, get_single_graph_batch_data, get_tree_batch_data
from Utils.metric import get_metric
def parse():
parser = argparse.ArgumentParser()
parser.add_argument("--seed", type=int)
parser.add_argument("--data", type=str)
parser.add_argument("--up_embed_dim", type=int)
parser.add_argument("--down_embed_dim", type=int)
parser.add_argument("--up_max_depth", type=int)
parser.add_argument("--down_max_dist", type=int)
parser.add_argument("--up_d_model", type=int)
parser.add_argument("--down_d_model", type=int)
parser.add_argument("--up_d_ff", type=int)
parser.add_argument("--down_d_ff", type=int)
parser.add_argument("--up_head_num", type=int)
parser.add_argument("--down_head_num", type=int)
parser.add_argument("--up_layer_num", type=int)
parser.add_argument("--down_layer_num", type=int)
parser.add_argument("--hid_size", type=int)
parser.add_argument("--dropout", type=float)
parser.add_argument("--max_oov_num", type=int)
parser.add_argument("--copy", type=int)
parser.add_argument("--rel_share", type=int)
parser.add_argument("--k_v_share", type=int)
parser.add_argument("--mode", type=str)
parser.add_argument("--cross_atten", type=str)
parser.add_argument("--up_rel", type=str, nargs='+', default=[])
parser.add_argument("--down_rel", type=str, nargs='+', default=[])
parser.add_argument("--gpu", type=int)
parser.add_argument("--lr", type=float)
parser.add_argument("--epoch", type=int)
parser.add_argument("--train_batch_size", type=int)
parser.add_argument("--eval_batch_size", type=int)
parser.add_argument("--train_step", type=int)
parser.add_argument("--eval_step", type=int)
parser.add_argument("--schedule_step", type=int)
parser.add_argument("--log", type=str)
parser.add_argument("--gamma", type=float, default=0.8)
parser.add_argument("--prefix", type=str)
parser.add_argument("--model", type=str)
parser.add_argument("--min_freq", type=int, default=1)
parser.add_argument("--output", type=str)
parser.add_argument("--absolute_pos", type=int, default=1)
return parser.parse_args()
def train(model, batch, label, optimizer, Loss, vocab_size, unk_idx, MODEL):
out = None
if MODEL == "RGT":
up_x, up_type_x, down_x, down_type_x, up_depth, up_schema, down_dist, down_lca, q_x, AOA_mask, AOD_mask, copy_mask, src2trg_map = batch
model.train()
out = model(up_x, up_type_x, down_x, down_type_x, up_depth, up_schema,
down_dist, down_lca, q_x, AOA_mask, AOD_mask, copy_mask,
src2trg_map)
elif MODEL in ["Relative-Transformer", "Transformer", "BiLSTM"]:
nodes, questions, rela_dist, copy_mask, src2trg_map = batch
if MODEL == "Relative-Transformer":
out = model(nodes, rela_dist, questions, copy_mask, src2trg_map)
elif MODEL in ["Transformer", "BiLSTM"]:
out = model(nodes, questions, copy_mask, src2trg_map)
elif MODEL in ["GAT", "GCN"]:
nodes, types, questions, graphs, copy_mask, src2trg_map = batch
out = model(nodes, types, graphs, questions, copy_mask, src2trg_map)
else:
nodes, types, node_order, adjacency_list, edge_order, q_x, copy_mask, src2trg_map = batch
out = model(nodes, types, node_order, adjacency_list, edge_order, q_x,
copy_mask, src2trg_map)
out_dim = out.size(-1)
out = out.reshape(-1, out_dim)
trg = label.reshape(-1)
out = torch.log(out + 1e-15)
loss = Loss(out, trg)
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 1)
optimizer.step()
return loss.item()
def eval(model,
dataset,
up_vocab,
down_vocab,
args,
device,
MODEL,
best_bleu=100,
max_decode=50,
write=False):
model.eval()
dataloader = DataLoader(dataset, args.eval_batch_size)
total_preds = []
for batch_data in dataloader:
preds = []
if MODEL == "RGT":
batch, label = get_RGT_batch_data(batch_data, up_vocab.pad_idx,
down_vocab.pad_idx, device,
args.down_max_dist,
down_vocab.size,
down_vocab.unk_idx)
up_x, up_type_x, down_x, down_type_x, up_depth, up_schema, down_dist, down_lca, q_x, AOA_mask, AOD_mask, copy_mask, src2trg_map = batch
up_nodes, down_nodes, hidden, up_mask, down_mask = model.encode(
up_x, up_type_x, down_x, down_type_x, up_depth, up_schema,
down_dist, down_lca, AOA_mask, AOD_mask)
inputs = q_x[:, 0].view(-1, 1)
for i in range(max_decode):
inputs = model.down_nodes_embed(inputs)
cur_out, hidden = model.decode(inputs, up_nodes, down_nodes,
hidden, up_mask, down_mask,
copy_mask, src2trg_map)
next_input = cur_out.argmax(dim=-1)
preds.append(next_input)
next_input[next_input >= down_vocab.size] = down_vocab.unk_idx
inputs = next_input
elif MODEL in [
"Relative-Transformer", "Transformer", "BiLSTM", "GAT", "GCN"
]:
if MODEL in ["Relative-Transformer", "Transformer", "BiLSTM"]:
batch, label = get_seq_batch_data(batch_data,
down_vocab.pad_idx, device,
down_vocab.size,
down_vocab.unk_idx,
args.down_max_dist)
nodes, questions, rela_dist, copy_mask, src2trg_map = batch
if MODEL == "Relative-Transformer":
nodes, hidden, mask = model.encode(nodes, rela_dist)
elif MODEL in ["Transformer", "BiLSTM"]:
nodes, hidden, mask = model.encode(nodes)
else:
# TODO
pass
elif MODEL in ["GAT", "GCN"]:
batch, label = get_single_graph_batch_data(
batch_data, down_vocab.pad_idx, device, down_vocab.size,
down_vocab.unk_idx)
nodes, types, questions, graphs, copy_mask, src2trg_map = batch
nodes, hidden, mask = model.encode(nodes, types, graphs)
inputs = questions[:, 0].view(-1, 1)
for i in range(max_decode):
cur_out, hidden = model.decode(inputs, nodes, hidden, mask,
copy_mask, src2trg_map)
next_input = cur_out.argmax(dim=-1)
preds.append(next_input)
next_input[next_input >= down_vocab.size] = down_vocab.unk_idx
inputs = next_input
else:
batch, label = get_tree_batch_data(batch_data, device)
nodes, types, node_order, adjacency_list, edge_order, questions, copy_mask, src2trg_map = batch
nodes, hidden, mask = model.encode(nodes, types, node_order,
adjacency_list, edge_order)
inputs = questions[:, 0].view(-1, 1)
for i in range(max_decode):
cur_out, hidden = model.decode(inputs, nodes, hidden, mask,
copy_mask, src2trg_map)
next_input = cur_out.argmax(dim=-1)
preds.append(next_input)
next_input[next_input >= down_vocab.size] = down_vocab.unk_idx
inputs = next_input
preds = torch.cat(preds, dim=1)
total_preds += preds.tolist()
bleu, preds, refs = get_metric(total_preds, dataset.origin_questions,
down_vocab, True, dataset.val_map_list,
dataset.idx2tok_map_list)
if write or bleu > best_bleu:
with open(args.output, 'w') as f:
for idx, pred in enumerate(preds):
f.write(f"Pre: {pred}\n\n")
f.write(f"Ref: {refs[idx]}\n")
f.write(f"{'-' * 60}\n")
return bleu
def run(args):
fix_seed(args.seed)
device = torch.device(f"cuda:{args.gpu}")
up_vocab, down_vocab, vocab = None, None, None
train_set, dev_set = None, None
model = None
DATA = None
train_data_files = []
train_table_file = ''
dev_data_files = []
dev_table_file = ''
# build vocabulary and load data
if args.data == "spider":
DATA = "spider"
train_data_files = [
"./Dataset/spider/train_spider.json",
"./Dataset/spider/train_others.json"
]
# train_data_files = ['./Dataset/spider/test.json']
train_table_file = "./Dataset/spider/tables.json"
dev_table_file = train_table_file
dev_data_files = ["./Dataset/spider/dev.json"]
elif args.data == "wikisql":
DATA = "wikisql"
train_data_files = ["./Dataset/wikisql/train.jsonl"]
train_table_file = "./Dataset/wikisql/train.tables.jsonl"
dev_data_files = ["./Dataset/wikisql/dev.jsonl"]
dev_table_file = "./Dataset/wikisql/dev.tables.jsonl"
test_data_files = ["./Dataset/wikisql/test.jsonl"]
test_table_file = "./Dataset/wikisql/test.tables.jsonl"
else:
raise NotImplementedError("Not supported dataset.")
if args.model == "RGT":
train_set = RGTDataset(train_data_files,
train_table_file,
data=DATA,
min_freq=args.min_freq,
max_depth=args.up_max_depth)
dev_set = RGTDataset(dev_data_files,
dev_table_file,
data=DATA,
down_vocab=train_set.down_vocab,
up_vocab=train_set.up_vocab,
max_depth=args.up_max_depth)
if DATA == "wikisql":
test_set = RGTDataset(test_data_files,
test_table_file,
data=DATA,
down_vocab=train_set.down_vocab,
up_vocab=train_set.up_vocab,
max_depth=args.up_max_depth)
up_vocab = train_set.up_vocab
down_vocab = train_set.down_vocab
rgt_vocab_path = os.path.join(RGT_VOCAB_PATH, DATA)
if not os.path.exists(rgt_vocab_path):
os.makedirs(rgt_vocab_path)
up_vocab.save(os.path.join(rgt_vocab_path, "up.vocab"))
down_vocab.save(os.path.join(rgt_vocab_path, "down.vocab"))
elif args.model in ["Relative-Transformer", "Transformer", "BiLSTM"]:
train_set = SeqDataset(train_data_files,
train_table_file,
data=DATA,
min_freq=args.min_freq)
dev_set = SeqDataset(dev_data_files,
dev_table_file,
data=DATA,
vocab=train_set.vocab)
if DATA == "wikisql":
test_set = SeqDataset(test_data_files,
test_table_file,
data=DATA,
vocab=train_set.vocab)
vocab = train_set.vocab
seq_vocab_path = os.path.join(SEQ_VOCAB_PATH, DATA)
if not os.path.exists(seq_vocab_path):
os.makedirs(seq_vocab_path)
vocab.save(os.path.join(seq_vocab_path, "seq.vocab"))
elif args.model in ["GAT", "GCN"]:
train_set = SingleGraphDataset(train_data_files,
train_table_file,
data=DATA,
min_freq=args.min_freq)
dev_set = SingleGraphDataset(dev_data_files,
dev_table_file,
data=DATA,
vocab=train_set.vocab)
if DATA == "wikisql":
test_set = SingleGraphDataset(test_data_files,
test_table_file,
data=DATA,
vocab=train_set.vocab)
vocab = train_set.vocab
single_graph_vocab_path = os.path.join(SINGLE_GRAPH_VOCAB_PATH, DATA)
if not os.path.exists(single_graph_vocab_path):
os.makedirs(single_graph_vocab_path)
vocab.save(os.path.join(single_graph_vocab_path, "SingleGraph.vocab"))
elif args.model == "TreeLSTM":
train_set = TreeDataset(train_data_files,
train_table_file,
data=DATA,
min_freq=args.min_freq)
dev_set = TreeDataset(dev_data_files,
dev_table_file,
data=DATA,
vocab=train_set.vocab)
if DATA == "wikisql":
test_set = TreeDataset(test_data_files,
test_table_file,
data=DATA,
vocab=train_set.vocab)
vocab = train_set.vocab
tree_vocab_path = os.path.join(TREE_VOCAB_PATH, DATA)
if not os.path.exists(tree_vocab_path):
os.makedirs(tree_vocab_path)
vocab.save(os.path.join(tree_vocab_path, "tree.vocab"))
else:
raise ValueError("Not supported model.")
# build model
if args.model == "RGT":
model = RGT(args.up_embed_dim, args.down_embed_dim, up_vocab.size,
down_vocab.size, UP_TYPE_NUM, DOWN_TYPE_NUM, UP_SCHEMA_NUM,
args.up_max_depth, args.down_max_dist, args.up_d_model,
args.down_d_model, args.up_d_ff, args.down_d_ff,
args.up_head_num, args.down_head_num, args.up_layer_num,
args.down_layer_num, args.hid_size, args.dropout,
up_vocab.pad_idx, down_vocab.pad_idx, args.max_oov_num,
args.copy, args.rel_share, args.k_v_share,
args.mode, args.cross_atten, set(args.up_rel),
set(args.down_rel), DATA)
elif args.model == "Relative-Transformer":
model = RelativeTransformer(args.down_embed_dim, vocab.size,
args.down_d_model, args.down_d_ff,
args.down_head_num, args.down_layer_num,
args.hid_size, args.dropout, vocab.pad_idx,
args.down_max_dist, args.max_oov_num,
args.copy, args.rel_share, args.k_v_share)
elif args.model == "Transformer":
model = AbsoluteTransformer(args.down_embed_dim,
vocab.size,
args.down_d_model,
args.down_d_ff,
args.down_head_num,
args.down_layer_num,
args.hid_size,
args.dropout,
vocab.pad_idx,
max_oov_num=args.max_oov_num,
copy=args.copy,
pos=args.absolute_pos)
elif args.model == "BiLSTM":
model = BiLSTM(args.down_embed_dim, vocab.size, args.hid_size,
vocab.pad_idx, args.dropout, args.max_oov_num,
args.copy)
elif args.model == "GAT":
model = GAT(args.down_embed_dim, TYPE_NUM, vocab.size,
args.down_d_model, args.down_d_ff, args.down_head_num,
args.down_layer_num, args.hid_size, vocab.pad_idx,
args.dropout, args.max_oov_num, args.copy)
elif args.model == "GCN":
model = GCN(args.down_embed_dim, vocab.size, TYPE_NUM, args.hid_size,
args.down_layer_num, vocab.pad_idx, args.dropout,
args.max_oov_num, args.copy)
else:
model = TreeLSTM(args.down_embed_dim, vocab.size, TYPE_NUM,
args.hid_size, args.dropout, vocab.pad_idx,
args.max_oov_num, args.copy)
args.train_batch_size = 1
args.eval_batch_size = 1
model.to(device)
# optimizer
lr = args.lr
optimizer = torch.optim.Adam(model.parameters(), lr)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
step_size=args.schedule_step,
gamma=args.gamma)
# loss function
Loss = None
if args.model == "RGT":
Loss = torch.nn.NLLLoss(ignore_index=down_vocab.pad_idx)
elif args.model in [
"Relative-Transformer", "Transformer", "BiLSTM", "GAT", "GCN",
"TreeLSTM"
]:
Loss = torch.nn.NLLLoss(ignore_index=vocab.pad_idx)
else:
raise ValueError("Not supported model.")
# data loader
train_data_loader = DataLoader(train_set,
batch_size=args.train_batch_size,
shuffle=True)
# if test_set is not None:
# test_data_loader = DataLoader(test_set,
# batch_size=args.eval_batch_size)
# train and evaluate
MODEL = None
if args.model == "RGT":
MODEL = RGT_MODEL_PATH
elif args.model == "Relative-Transformer":
MODEL = RELATIVE_MODEL_PATH
elif args.model == "Transformer":
MODEL = TRANSFORMER_MODEL_PATH
elif args.model == "BiLSTM":
MODEL = BILSTM_MODEL_PATH
elif args.model == "GAT":
MODEL = GAT_MODEL_PATH
elif args.model == "GCN":
MODEL = GCN_MODEL_PATH
else:
MODEL = TREE_MODEL_PATH
MODEL = os.path.join(MODEL, DATA)
if not os.path.exists(MODEL):
os.makedirs(MODEL)
model_file = f"{args.prefix}.pt"
model_path = os.path.join(MODEL, model_file)
best_bleu = 0
batch_step = 0
test_bleu = 0
for epoch in range(args.epoch):
for batch_data in train_data_loader:
batch, label = None, None
if args.model == "RGT":
batch, label = get_RGT_batch_data(batch_data, up_vocab.pad_idx,
down_vocab.pad_idx, device,
args.down_max_dist,
down_vocab.size,
down_vocab.unk_idx)
elif args.model in [
"Relative-Transformer", "Transformer", "BiLSTM"
]:
batch, label = get_seq_batch_data(batch_data, vocab.pad_idx,
device, vocab.size,
vocab.unk_idx,
args.down_max_dist)
elif args.model in ["GAT", "GCN"]:
batch, label = get_single_graph_batch_data(
batch_data, vocab.pad_idx, device, vocab.size,
vocab.unk_idx)
else:
batch, label = get_tree_batch_data(batch_data, device)
train_loss = 0
if args.model == "RGT":
train_loss = train(model, batch, label, optimizer, Loss,
down_vocab.size, down_vocab.unk_idx,
args.model)
elif args.model in [
"Relative-Transformer", "Transformer", "BiLSTM", "GAT",
"GCN", "TreeLSTM"
]:
train_loss = train(model, batch, label, optimizer, Loss,
vocab.size, vocab.unk_idx, args.model)
else:
raise ValueError("Not supported model.")
if batch_step and not batch_step % args.train_step:
logging.info(
f"epoch {epoch}, batch {batch_step}: [training loss-> {round(train_loss, 3)}]"
)
if batch_step and not batch_step % args.eval_step:
train_bleu, dev_bleu = 0, 0
if args.model == "RGT":
# train_bleu = eval(model, train_set, up_vocab, down_vocab,
# args, device, args.model)
# logging.info(
# f"epoch {epoch}, batch {batch_step}: [training bleu-> {round(train_bleu, 4)}]"
# )
dev_bleu = eval(model,
dev_set,
up_vocab,
down_vocab,
args,
device,
args.model,
best_bleu=best_bleu)
logging.info(
f"epoch {epoch}, batch {batch_step}: [dev bleu-> {round(dev_bleu, 4)}]"
)
elif args.model in [
"Relative-Transformer", "Transformer", "BiLSTM", "GAT",
"GCN", "TreeLSTM"
]:
# train_bleu = eval(model, train_set, None, vocab, args,
# device, args.model)
# logging.info(
# f"epoch {epoch}, batch {batch_step}: [training bleu-> {round(train_bleu, 4)}]"
# )
dev_bleu = eval(model,
dev_set,
None,
vocab,
args,
device,
args.model,
best_bleu=best_bleu)
logging.info(
f"epoch {epoch}, batch {batch_step}: [dev bleu-> {round(dev_bleu, 4)}]"
)
else:
raise ValueError("Not supported model.")
if dev_bleu > best_bleu:
best_bleu = dev_bleu
torch.save({
"args": args,
"model": model.state_dict()
}, model_path)
if DATA == "wikisql":
if args.model == "RGT":
test_bleu = eval(model,
test_set,
up_vocab,
down_vocab,
args,
device,
args.model,
write=True)
else:
test_bleu = eval(model,
test_set,
None,
vocab,
args,
device,
args.model,
write=True)
logging.info(
f"epoch {epoch}, batch {batch_step}: [test bleu-> {round(test_bleu, 4)}]"
)
batch_step += 1
scheduler.step()
logging.info(f"best dev bleu: {round(best_bleu, 4)}")
if DATA == "wikisql":
logging.info(f"test bleu: {round(test_bleu, 4)}")
if __name__ == "__main__":
args = parse()
LOG_DIR = os.path.dirname(args.log)
if not os.path.exists(LOG_DIR):
os.makedirs(LOG_DIR)
OUTPUT_DIR = os.path.dirname(args.output)
if not os.path.exists(OUTPUT_DIR):
os.makedirs(OUTPUT_DIR)
logging.basicConfig(format='%(asctime)s - %(levelname)s: %(message)s',
level=logging.DEBUG,
filename=args.log,
filemode='w')
run(args)