-
Notifications
You must be signed in to change notification settings - Fork 18
/
corechain_with_sltr.py
839 lines (663 loc) · 41.1 KB
/
corechain_with_sltr.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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
'''
create loss function and training data and other necessary utilities
TODO:
> Add visaulization so as to understand the interplay of train vs Validation vs test accuracy (not really going to do it)
> Add logs
- Need to discuss it before implementing.
'''
from __future__ import print_function
# Torch imports
import torch
import torch.nn as nn
from torch import optim
from torch.utils.data import DataLoader
# Local imports
import data_loader as dl
import auxiliary as aux
import network as net
from configs import config_loader as cl
from utils import query_graph_to_sparql as sparql_constructor
from utils import dbpedia_interface as db_interface
from utils import embeddings_interface
from utils import natural_language_utilities as nlutils
import network_rdftype as net_rdftype
import network_intent as net_intent
import onefile as one
import lriters
from utils.goodies import *
# Other libs
import numpy as np
import argparse
import json
import time
import os
import sys
import pickle
from pprint import pprint
from progressbar import ProgressBar
if sys.version_info[0] == 3: import configparser as ConfigParser
else: import ConfigParser
#setting up device,model name and loss types.
device = torch.device("cpu")
training_model = 'bilstm_dot'
_dataset = 'lcquad'
pointwise = False
_train_over_validation = False
#Loading relations file.
COMMON_DATA_DIR = 'data/data/common'
_dataset_specific_data_dir = 'data/data/%(dataset)s/' % {'dataset': _dataset}
_inv_relations = aux.load_inverse_relation(COMMON_DATA_DIR)
_word_to_id = aux.load_word_list(COMMON_DATA_DIR)
#onefile stuff
dbp = db_interface.DBPedia(_verbose=True, caching=True)
vocabularize_relation = lambda path: embeddings_interface.vocabularize(nlutils.tokenize(dbp.get_label(path))).tolist()
sparql_constructor.init(embeddings_interface)
# gloveid_to_embeddingid , embeddingid_to_gloveid, word_to_gloveid, gloveid_to_word = aux.load_embeddingid_gloveid()
def load_data(data,parameter_dict,pointwise,schema='default',shuffle = False):
# Loading training data
td = dl.TrainingDataGenerator(data,
parameter_dict['max_length'],
parameter_dict['_neg_paths_per_epoch_train'], parameter_dict['batch_size']
, parameter_dict['total_negative_samples'], pointwise=pointwise,schema=schema)
return DataLoader(td, shuffle=shuffle)
def curatail_padding(data,parameter_dict):
'''
Since schema is already implicitly defined/present in the parameter_dict['rel1_pad']
'''
data['test_neg_paths'] = data['test_neg_paths'][:, :, :parameter_dict['rel_pad']]
data['test_pos_paths'] = data['test_pos_paths'][:, :parameter_dict['rel_pad']]
if parameter_dict['schema'] == 'reldet':
data['test_neg_paths_rel1_rd'] = data['test_neg_paths_rel1_rd'][:, :, :parameter_dict['rel1_pad']]
data['test_neg_paths_rel2_rd'] = data['test_neg_paths_rel2_rd'][:, :, :parameter_dict['rel1_pad']]
data['test_pos_paths_rel1_rd'] = data['test_pos_paths_rel1_rd'][:, :parameter_dict['rel1_pad']]
data['test_pos_paths_rel2_rd'] = data['test_pos_paths_rel2_rd'][:, :parameter_dict['rel1_pad']]
elif parameter_dict['schema'] == 'slotptr':
data['test_neg_paths_rel1_sp'] = data['test_neg_paths_rel1_sp'][:, :, :parameter_dict['rel1_pad']]
data['test_neg_paths_rel2_sp'] = data['test_neg_paths_rel2_sp'][:, :, :parameter_dict['rel1_pad']]
data['test_pos_paths_rel1_sp'] = data['test_pos_paths_rel1_sp'][:, :parameter_dict['rel1_pad']]
data['test_pos_paths_rel2_sp'] = data['test_pos_paths_rel2_sp'][:, :parameter_dict['rel1_pad']]
data['valid_neg_paths'] = data['valid_neg_paths'][:, :, :parameter_dict['rel_pad']]
data['valid_pos_paths'] = data['valid_pos_paths'][:, :parameter_dict['rel_pad']]
if parameter_dict['schema'] == 'reldet':
data['valid_neg_paths_rel1_rd'] = data['valid_neg_paths_rel1_rd'][:, :, :parameter_dict['rel1_pad']]
data['valid_neg_paths_rel2_rd'] = data['valid_neg_paths_rel2_rd'][:, :, :parameter_dict['rel1_pad']]
data['valid_pos_paths_rel1_rd'] = data['valid_pos_paths_rel1_rd'][:, :parameter_dict['rel1_pad']]
data['valid_pos_paths_rel2_rd'] = data['valid_pos_paths_rel2_rd'][:, :parameter_dict['rel1_pad']]
elif parameter_dict['schema'] == 'slotptr':
data['valid_neg_paths_rel1_sp'] = data['valid_neg_paths_rel1_sp'][:, :, :parameter_dict['rel1_pad']]
data['valid_neg_paths_rel2_sp'] = data['valid_neg_paths_rel2_sp'][:, :, :parameter_dict['rel1_pad']]
data['valid_pos_paths_rel1_sp'] = data['valid_pos_paths_rel1_sp'][:, :parameter_dict['rel1_pad']]
data['valid_pos_paths_rel2_sp'] = data['valid_pos_paths_rel2_sp'][:, :parameter_dict['rel1_pad']]
return data
def training_loop(training_model, parameter_dict,modeler,train_loader,
optimizer,loss_func, data, dataset, device, test_every, validate_every , pointwise = False, problem='core_chain',lrschedule=None,curtail_padding_rel=True):
model_save_location = aux.save_location(problem, training_model, dataset)
aux_save_information = {
'epoch' : 0,
'test_accuracy':0.0,
'validation_accuracy':0.0,
'parameter_dict':parameter_dict
}
train_loss = []
valid_accuracy = []
test_accuracy = []
best_validation_accuracy = 0
best_test_accuracy = 0
if parameter_dict['schema'] == 'reldet':
parameter_dict['rel1_pad'] = parameter_dict['relrd_pad']
elif parameter_dict['schema'] == 'slotptr':
parameter_dict['rel1_pad'] = parameter_dict['relsp_pad']
###############
# Training Loop
###############
#Makes test data of appropriate shape
print("the dataset is ", dataset)
if curtail_padding_rel and dataset == 'lcquad':
data = curatail_padding(data, parameter_dict)
try:
for epoch in range(parameter_dict['epochs']):
# Epoch start print
print("Epoch: ", epoch, "/", parameter_dict['epochs'])
# Bookkeeping variables
epoch_loss = []
epoch_time = time.time()
# Loop for one batch
# tqdm_loop = tqdm(enumerate(train_loader))
for i_batch, sample_batched in enumerate(train_loader):
# Bookkeeping and data preparation
batch_time = time.time()
if lr_schedule:
update_lr(optimizer, lrschedule.get())
if not pointwise:
ques_batch = torch.tensor(np.reshape(sample_batched[0][0], (-1, parameter_dict['max_length'])),
dtype=torch.long, device=device)
pos_batch = torch.tensor(np.reshape(sample_batched[0][1], (-1, parameter_dict['max_length'])),
dtype=torch.long, device=device)
neg_batch = torch.tensor(np.reshape(sample_batched[0][2], (-1, parameter_dict['max_length'])),
dtype=torch.long, device=device)
data['dummy_y'] = torch.ones(ques_batch.shape[0], device=device)
if parameter_dict['schema'] != 'default':
pos_rel1_batch = torch.tensor(np.reshape(sample_batched[0][3], (-1, parameter_dict['max_length'])),
dtype=torch.long, device=device)
pos_rel2_batch = torch.tensor(np.reshape(sample_batched[0][4], (-1, parameter_dict['max_length'])),
dtype=torch.long, device=device)
neg_rel1_batch = torch.tensor(np.reshape(sample_batched[0][5], (-1, parameter_dict['max_length'])),
dtype=torch.long, device=device)
neg_rel2_batch = torch.tensor(np.reshape(sample_batched[0][6], (-1, parameter_dict['max_length'])),
dtype=torch.long, device=device)
data_batch = {
'ques_batch': ques_batch,
'pos_batch': pos_batch[:,:parameter_dict['rel_pad']],
'neg_batch': neg_batch[:,:parameter_dict['rel_pad']],
'y_label': data['dummy_y'],
'pos_rel1_batch': pos_rel1_batch[:,:parameter_dict['rel1_pad']],
'pos_rel2_batch':pos_rel2_batch[:,:parameter_dict['rel1_pad']],
'neg_rel1_batch':neg_rel1_batch[:,:parameter_dict['rel1_pad']],
'neg_rel2_batch' : neg_rel2_batch[:,:parameter_dict['rel1_pad']]
}
else:
data_batch = {
'ques_batch': ques_batch,
'pos_batch': pos_batch[:,:parameter_dict['rel_pad']],
'neg_batch': neg_batch[:,:parameter_dict['rel_pad']],
'y_label': data['dummy_y']}
else:
ques_batch = torch.tensor(np.reshape(sample_batched[0][0], (-1, parameter_dict['max_length'])),
dtype=torch.long, device=device)
path_batch = torch.tensor(np.reshape(sample_batched[0][1], (-1, parameter_dict['max_length'])),
dtype=torch.long, device=device)
y = torch.tensor(sample_batched[1],dtype = torch.float,device=device).view(-1)
if parameter_dict['schema'] != 'default':
path_rel1_batch = torch.tensor(np.reshape(sample_batched[0][2], (-1, parameter_dict['max_length'])),
dtype=torch.long, device=device)
path_rel2_batch = torch.tensor(np.reshape(sample_batched[0][3], (-1, parameter_dict['max_length'])),
dtype=torch.long, device=device)
data_batch = {
'ques_batch': ques_batch,
'path_batch': path_batch[:,:parameter_dict['rel_pad']],
'y_label': y,
'path_rel1_batch': path_rel1_batch[:,:parameter_dict['rel1_pad']],
'path_rel2_batch': path_rel2_batch[:,:parameter_dict['rel1_pad']]
}
else:
data_batch = {
'ques_batch': ques_batch,
'path_batch': path_batch[:,:parameter_dict['rel_pad']],
'y_label': y
}
loss = modeler.train(data=data_batch,
optimizer=optimizer,
loss_fn=loss_func,
device=device)
# Bookkeep the training loss
epoch_loss.append(loss.item())
# tqdm_loop.desc("#"+str(i_batch)+"\tLoss:" + str(loss.item())[:min(5, len(str(loss.item())))])
print("Batch:\t%d" % i_batch, "/%d\t: " % (parameter_dict['batch_size']),
"%s" % (time.time() - batch_time),
"\t%s" % (time.time() - epoch_time),
"\t%s" % (str(loss.item())),
end=None if i_batch + 1 == int(int(i_batch) / parameter_dict['batch_size']) else "\n")
# EPOCH LEVEL
# Track training loss
train_loss.append(epoch_loss)
# test_every = False
if test_every:
# Run on test set
if epoch%test_every == 0:
if parameter_dict['schema'] != 'default':
if parameter_dict['schema'] == 'slotptr':
test_accuracy.append(aux.validation_accuracy(data['test_questions'], data['test_pos_paths'],
data['test_neg_paths'],modeler, device, data['test_pos_paths_rel1_sp'],data['test_pos_paths_rel2_sp'],
data['test_neg_paths_rel1_sp'],data['test_neg_paths_rel2_sp']))
else:
test_accuracy.append(aux.validation_accuracy(data['test_questions'], data['test_pos_paths'],
data['test_neg_paths'], modeler, device,
data['test_pos_paths_rel1_rd'],
data['test_pos_paths_rel2_rd'],
data['test_neg_paths_rel1_rd'],
data['test_neg_paths_rel2_rd']))
else:
test_accuracy.append(aux.validation_accuracy(data['test_questions'], data['test_pos_paths'],
data['test_neg_paths'], modeler, device))
if test_accuracy[-1] >= best_test_accuracy:
best_test_accuracy = test_accuracy[-1]
aux_save_information['test_accuracy'] = best_test_accuracy
else:
test_accuracy.append(0)
best_test_accuracy = 0
# Run on validation set
if validate_every:
if epoch%validate_every == 0:
if parameter_dict['schema'] != 'default':
if parameter_dict['schema'] == 'slotptr':
valid_accuracy.append(aux.validation_accuracy(data['valid_questions'], data['valid_pos_paths'],
data['valid_neg_paths'], modeler, device, data['valid_pos_paths_rel1_sp'],data['valid_pos_paths_rel2_sp'],
data['valid_neg_paths_rel1_sp'],data['valid_neg_paths_rel2_sp']))
else:
valid_accuracy.append(aux.validation_accuracy(data['valid_questions'][:-1], data['valid_pos_paths'][:-1],
data['valid_neg_paths'][:-1], modeler, device,
data['valid_pos_paths_rel1_rd'][:-1],
data['valid_pos_paths_rel2_rd'][:-1],
data['valid_neg_paths_rel1_rd'][:-1],
data['valid_neg_paths_rel2_rd'][:-1]))
else:
valid_accuracy.append(aux.validation_accuracy(data['valid_questions'], data['valid_pos_paths'],
data['valid_neg_paths'], modeler, device))
if valid_accuracy[-1] > best_validation_accuracy:
print("MODEL WEIGHTS RIGHT NOW: ", modeler.get_parameter_sum())
best_validation_accuracy = valid_accuracy[-1]
aux_save_information['epoch'] = epoch
aux_save_information['validation_accuracy'] = best_validation_accuracy
aux.save_model(model_save_location, modeler, model_name='model.torch'
, epochs=epoch, optimizer=optimizer, accuracy=best_validation_accuracy, aux_save_information=aux_save_information)
# Resample new negative paths per epoch and shuffle all data
train_loader.dataset.shuffle()
# Epoch level prints
print("Time: %s\t" % (time.time() - epoch_time),
"Loss: %s\t" % (sum(epoch_loss)),
"Valdacc: %s\t" % (valid_accuracy[-1]),
"Testacc: %s\n" % (test_accuracy[-1]),
"BestValidAcc: %s\n" % (best_validation_accuracy),
"BestTestAcc: %s\n" % (best_test_accuracy))
return train_loss, modeler, valid_accuracy, test_accuracy, model_save_location
except KeyboardInterrupt:
print('-' * 89)
return train_loss, modeler, valid_accuracy, test_accuracy, model_save_location
def evaluate(device,pointwise,dataset,training_model,training_model_number,finetune=False):
# Reading and setting up config parser
config = ConfigParser.ConfigParser()
config.readfp(open('configs/macros.cfg'))
# setting up device,model name and loss types.
device = device
training_model = training_model
_dataset = dataset
pointwise = pointwise
# 19 is performing the best
training_model_number = training_model_number
_debug = False
# Loading relations file.
COMMON_DATA_DIR = 'data/data/common'
INTENTS = ['count', 'ask', 'list']
RDFTYPES = ['x', 'uri', 'none']
_dataset_specific_data_dir = 'data/data/%(dataset)s/' % {'dataset': _dataset}
# glove_id_sf_to_glove_id_rel = dl.create_relation_lookup_table(COMMON_DATA_DIR)
# Model specific paramters
# #Model specific paramters
if pointwise:
training_config = 'pointwise'
else:
training_config = 'pairwise'
parameter_dict = cl.runtime_parameters(dataset=_dataset, training_model=training_model,
training_config=training_config, config_file='configs/macros.cfg')
if training_model == 'cnn_dot':
parameter_dict['output_dim'] = int(config.get(_dataset, 'output_dim'))
# Update parameters
parameter_dict['_dataset_specific_data_dir'] = _dataset_specific_data_dir
parameter_dict['_model_dir'] = './data/models/'
parameter_dict['corechainmodel'] = training_model
parameter_dict['corechainmodelnumber'] = str(training_model_number)
parameter_dict['intentmodel'] = 'bilstm_dense'
parameter_dict['intentmodelnumber'] = '4'
parameter_dict['rdftypemodel'] = 'bilstm_dense'
parameter_dict['rdftypemodelnumber'] = '3'
parameter_dict['rdfclassmodel'] = 'bilstm_dot'
parameter_dict['rdfclassmodelnumber'] = '4'
parameter_dict['dataset'] = _dataset
TEMP = aux.data_loading_parameters(_dataset, parameter_dict, runtime=True)
_dataset_specific_data_dir, _model_specific_data_dir, _file, \
_max_sequence_length, _neg_paths_per_epoch_train, \
_neg_paths_per_epoch_validation, _training_split, _validation_split, _index = TEMP
_data, _vectors = dl.create_dataset_runtime(file=_file, _dataset=_dataset,
_dataset_specific_data_dir=_dataset_specific_data_dir,
split_point=.80)
parameter_dict['vectors'] = _vectors
# For interpretability's sake
# word_to_gloveid, gloveid_to_word = aux.load_embeddingid_gloveid()
"""
Different counters and metrics to store accuracy of diff modules
Core chain accuracy counter counts the number of time the core chain predicated is same as
positive path. This also includes for ask query.
The counter might confuse the property and the ontology.
Similar functionality with rdf_type and intent
**word vector accuracy counter**:
Counts the number of times just using word2vec similarity,
the best path came the most similar.
This will only work if CANDIDATE_SPACE is not none.
"""
'''
c_flag is true if the core_chain was correctly predicted.
same is the case for i_flag and r_flag, rt_flag (correct candidate for rdf type)
'''
c_flag, i_flag, r_flag, rt_flag = False, False, False, False
'''
Stores tuple of (fmeasure,precision,recall)
'''
results = []
Logging = parameter_dict.copy()
Logging['runtime'] = []
quesans = one.QuestionAnswering(parameter_dict, pointwise, _word_to_id, device, _dataset, False)
# Some logs which run during runtime, not after.
core_chain_acc_log = []
core_chain_mrr_log = []
startindex = 0
for index, data in enumerate(_data[startindex:]):
print (index)
if index == 16 or index == 25:
continue
print(data.keys())
index += startindex
log, metrics = one.answer_question(qa=quesans,
index=index,
data=data,
relations=_inv_relations,
parameter_dict=parameter_dict)
# log, metrics = answer_question(qa=None,
# index=None,
# data=None,
# gloveid_to_embeddingid=None,
# embeddingid_to_gloveid=None,
# relations=None,
# parameter_dict=None)
sparql = one.create_sparql(log=log,
data=data,
embeddings_interface=embeddings_interface,
relations=_inv_relations)
# sparql = ""
# metrics = eval(data, log, metrics)
# Update logs
Logging['runtime'].append({'log': log, 'metrics': metrics,
'pred_sparql': sparql, 'true_sparql': data['parsed-data']['node']['sparql_query']})
# Update metrics
core_chain_acc_log.append(metrics['core_chain_accuracy_counter'])
core_chain_mrr_log.append(metrics['core_chain_mrr_counter'])
# Make shit interpretable
# question = aux.id_to_word(log['question'], gloveid_to_word, remove_pad=True)
# true_path = aux.id_to_word(log['true_path'], gloveid_to_word, remove_pad=True)
# pred_path = aux.id_to_word(log['pred_path'], gloveid_to_word, remove_pad=True)
print("#%s" % index, "\t\bAcc: ", np.mean(core_chain_acc_log))
# print("\t\bQues: ", question)
# print("\t\bTPath: ", true_path, "\n\t\bPPath: ", pred_path)
# print("\t\bTIntent: ", log['true_intent'])
# print("\t\bPIntent: ", log['pred_intent'])
# print("\t\bPRdftype: ", log['true_rdf_type'])
# print("\t\bTRdftype: ", log['pred_rdf_type'])
# print("\t\bPRdfclass: ", log['true_rdf_class'])
# print("\t\bTRdfclass: ", log['pred_rdf_class'])
# print("")
# pprint(log)
# print("")
pprint(metrics)
# print("\n",sparql)
print("\n################################\n")
Logging = one.evaluate(Logging,dbp)
if not finetune:
model_path = os.path.join(parameter_dict['_model_dir'], 'core_chain')
model_path = os.path.join(model_path, parameter_dict['corechainmodel'])
model_path = os.path.join(model_path, parameter_dict['dataset'])
model_path = os.path.join(model_path, parameter_dict['corechainmodelnumber'])
pickle.dump(Logging,open(model_path+'/result.pickle','wb+'))
else:
return np.mean(core_chain_acc_log)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-dataset', action='store', dest='dataset',
help='dataset includes lcquad,qald',default = 'lcquad')
parser.add_argument('-model', action='store', dest='model',
help='name of the model to use',default='cnn_dot')
parser.add_argument('-pointwise', action='store', dest='pointwise',
help='to use pointwise training procedure make it true',default=True)
parser.add_argument('-train_valid', action='store', dest='train_over_validation',
help='train over validation', default=False)
parser.add_argument('-device', action='store', dest='device',
help='cuda for gpu else cpu', default='cpu')
parser.add_argument('-finetune', action='store', dest='finetune',
help='train over validation', default=False)
parser.add_argument('-bidirectional', action='store', dest='bidirectional',
help='train over validation', default=True)
parser.add_argument('-evals', action='store', dest='evals',
help='train over validation', default=True)
parser.add_argument('-ilr', action='store', dest='ilr',
help='init lr', default=0.001)
args = parser.parse_args()
# setting up device,model namenpp and loss types.
device = torch.device(args.device)
training_model = args.model
_dataset = args.dataset
pointwise = aux.to_bool(args.pointwise)
_train_over_validation = aux.to_bool(args.train_over_validation)
finetune = aux.to_bool(args.finetune)
bidirectional = aux.to_bool(args.bidirectional)
evals = aux.to_bool(args.evals)
ilr = float(args.ilr)
# #Model specific paramters
if pointwise:
training_config = 'pointwise'
else:
training_config = 'pairwise'
parameter_dict = cl.corechain_parameters(dataset=_dataset,training_model=training_model,
training_config=training_config,config_file='configs/macros.cfg')
if _dataset == 'lcquad':
test_every = parameter_dict['test_every']
else:
test_every = False
validate_every = parameter_dict['validate_every']
data = aux.load_data(_dataset=_dataset, _train_over_validation = _train_over_validation,
_parameter_dict=parameter_dict, _relations = _inv_relations, _pointwise=pointwise, _device=device,k=-1)
if training_model == 'reldet':
schema = 'reldet'
elif training_model == 'slotptr' or training_model == 'slotptr_common_encoder' or training_model == 'slotptrortho'\
or training_model == 'ulmfit_slotptr':
schema = 'slotptr'
elif training_model == 'bilstm_dot_multiencoder':
schema = 'default'
else:
schema = 'default'
train_loader = load_data(data, parameter_dict, pointwise, schema=schema)
# if training_model == 'bilstm_dot_ulmfit':
# _, data['vectors'] = vocab_master.load_ulmfit()
parameter_dict['vectors'] = data['vectors']
parameter_dict['schema'] = schema
if not bidirectional:
parameter_dict['bidirectional'] = False
if training_model == 'bilstm_dot':
if not finetune:
print("********", parameter_dict['bidirectional'])
modeler = net.BiLstmDot( _parameter_dict = parameter_dict,_word_to_id=_word_to_id,
_device=device,_pointwise=pointwise, _debug=False)
optimizer = optim.Adam(list(filter(lambda p: p.requires_grad, modeler.encoder.parameters())))
else:
print("^^^^^^^^^^^^^^^^^^finetuning^^^^^^^^^^^^^^^^")
if pointwise:
model_path = 'data/models/core_chain/bilstm_dot_pointwise/lcquad/19/model.torch'
else:
model_path = 'data/models/core_chain/bilstm_dot/lcquad/13/model.torch'
modeler = net.BiLstmDot(_parameter_dict=parameter_dict, _word_to_id=_word_to_id,
_device=device, _pointwise=pointwise, _debug=False)
modeler.load_from(model_path)
optimizer = optim.Adam(list(filter(lambda p: p.requires_grad, modeler.encoder.parameters())),lr=ilr)
if training_model == 'bilstm_dense':
modeler = net.BiLstmDense( _parameter_dict = parameter_dict,_word_to_id=_word_to_id,
_device=device,_pointwise=pointwise, _debug=False)
optimizer = optim.Adam(list(filter(lambda p: p.requires_grad, modeler.encoder.parameters()))+
list(filter(lambda p: p.requires_grad, modeler.dense.parameters())))
if training_model == 'bilstm_densedot':
modeler = net.BiLstmDenseDot( _parameter_dict = parameter_dict,_word_to_id=_word_to_id,
_device=device,_pointwise=pointwise, _debug=False)
optimizer = optim.Adam(list(filter(lambda p: p.requires_grad, modeler.encoder.parameters())) +
list(filter(lambda p: p.requires_grad, modeler.dense.parameters())))
if training_model == 'cnn_dot':
modeler = net.CNNDot( _parameter_dict = parameter_dict,_word_to_id=_word_to_id,
_device=device,_pointwise=pointwise, _debug=False)
optimizer = optim.Adam(list(filter(lambda p: p.requires_grad, modeler.encoder.parameters())))
if training_model == 'decomposable_attention':
if not finetune:
modeler = net.DecomposableAttention(_parameter_dict=parameter_dict, _word_to_id=_word_to_id,
_device=device, _pointwise=pointwise, _debug=False)
optimizer = optim.Adam(list(filter(lambda p: p.requires_grad, modeler.encoder.parameters())) +
list(filter(lambda p: p.requires_grad, modeler.scorer.parameters())))
else:
if not pointwise:
model_path = 'data/models/core_chain/decomposable_attention/lcquad/71/model.torch'
else:
model_path = 'data/models/core_chain/decomposable_attention_pointwise/lcquad/8/model.torch'
modeler = net.DecomposableAttention(_parameter_dict=parameter_dict, _word_to_id=_word_to_id,
_device=device, _pointwise=pointwise, _debug=False)
modeler.load_from(model_path)
optimizer = optim.Adam(list(filter(lambda p: p.requires_grad, modeler.encoder.parameters())) +
list(filter(lambda p: p.requires_grad, modeler.scorer.parameters())),lr=0.0001)
if training_model == 'reldet':
modeler = net.RelDetection(_parameter_dict=parameter_dict, _word_to_id=_word_to_id,
_device=device, _pointwise=pointwise, _debug=False)
optimizer = optim.Adam(list(filter(lambda p: p.requires_grad, modeler.encoder.parameters())))
# if False:
# # if training_model == 'slotptr':00
# modeler = net.SlotPointerModel(_parameter_dict=parameter_dict, _word_to_id=_word_to_id,
# _device=device, _pointwise=pointwise, _debug=False)
# optimizer = optim.Adam(list(filter(lambda p: p.requires_grad, modeler.encoder_q.parameters())) +
# list(filter(lambda p: p.requires_grad, modeler.encoder_p.parameters())) +
# list(filter(lambda p: p.requires_grad, modeler.comparer.parameters())))
if training_model == 'slotptr':
if not finetune:
modeler = net.QelosSlotPointerModel(_parameter_dict=parameter_dict, _word_to_id=_word_to_id,
_device=device, _pointwise=pointwise, _debug=False)
optimizer = optim.Adam(list(filter(lambda p: p.requires_grad, modeler.encoder_q.parameters())) +
list(filter(lambda p: p.requires_grad, modeler.encoder_p.parameters())), weight_decay=0.0001)
else:
print("^^^^^^^^^^^^finetuning^^^^^^^")
if pointwise:
model_path = 'data/models/core_chain/slotptr_pointwise/lcquad/8/model.torch'
else:
model_path = 'data/models/core_chain/slotptr/transfer-b/0/model.torch'
modeler = net.QelosSlotPointerModel(_parameter_dict=parameter_dict, _word_to_id=_word_to_id,
_device=device, _pointwise=pointwise, _debug=False)
optimizer = optim.Adam(list(filter(lambda p: p.requires_grad, modeler.encoder_q.parameters())) +
list(filter(lambda p: p.requires_grad, modeler.encoder_p.parameters())),
weight_decay=0.0001,lr=ilr)
modeler.load_from(model_path)
if training_model == 'slotptrortho':
if not finetune:
modeler = net.QelosSlotPointerModelOrthogonal(_parameter_dict=parameter_dict, _word_to_id=_word_to_id,
_device=device, _pointwise=pointwise, _debug=False)
optimizer = optim.Adam(list(filter(lambda p: p.requires_grad, modeler.encoder_q.parameters())) +
list(filter(lambda p: p.requires_grad, modeler.encoder_p.parameters())), weight_decay=0.0001)
else:
model_path = 'data/models/core_chain/slotptr/lcquad/94/model.torch'
modeler = net.QelosSlotPointerModelOrthogonal(_parameter_dict=parameter_dict, _word_to_id=_word_to_id,
_device=device, _pointwise=pointwise, _debug=False)
optimizer = optim.Adam(list(filter(lambda p: p.requires_grad, modeler.encoder_q.parameters())) +
list(filter(lambda p: p.requires_grad, modeler.encoder_p.parameters())),
weight_decay=0.0001,lr=0.0001)
modeler.load_from(model_path)
if training_model == 'bilstm_dot_skip':
modeler = net.BiLstmDot_skip( _parameter_dict = parameter_dict,_word_to_id=_word_to_id,
_device=device,_pointwise=pointwise, _debug=False)
optimizer = optim.Adam(list(filter(lambda p: p.requires_grad, modeler.encoder.parameters())))
if training_model == 'bilstm_dot_multiencoder':
print(schema)
modeler = net.BiLstmDot_multiencoder( _parameter_dict = parameter_dict,_word_to_id=_word_to_id,
_device=device,_pointwise=pointwise, _debug=False)
optimizer = optim.Adam(list(filter(lambda p: p.requires_grad, modeler.encoder_p.parameters())) +
list(filter(lambda p: p.requires_grad, modeler.encoder_q.parameters())))
if training_model == 'bilstm_dot_ulmfit':
modeler = net.BiLstmDot_ulmfit( _parameter_dict = parameter_dict,_word_to_id=_word_to_id,
_device=device,_pointwise=pointwise, _debug=False)
modeler.freeze_layer(modeler.encoder)
modeler.unfreeze_layer(modeler.encoder.rnns[-1])
# +
# list(filter(lambda p: p.requires_grad, modeler.linear.parameters()))
optimizer = optim.Adam(list(filter(lambda p: p.requires_grad, modeler.encoder.parameters())),lr=0.001)
if training_model == 'slotptr_common_encoder':
print("*************",parameter_dict['bidirectional'])
if not finetune:
modeler = net.QelosSlotPointerModel_common_encoder(_parameter_dict=parameter_dict, _word_to_id=_word_to_id,
_device=device, _pointwise=pointwise, _debug=False)
optimizer = optim.Adam(list(filter(lambda p: p.requires_grad, modeler.encoder_q.parameters())) +
list(filter(lambda p: p.requires_grad, modeler.encoder_p.parameters())), weight_decay=0.0001)
else:
print("^^^^^^^^^^^^finetuning^^^^^^^")
if pointwise:
model_path = 'data/models/core_chain/slotptr_common_encoder_pointwise/lcquad/1/model.torch'
else:
model_path = 'data/models/core_chain/slotptr_common_encoder/lcquad/5/model.torch'
modeler = net.QelosSlotPointerModel_common_encoder(_parameter_dict=parameter_dict, _word_to_id=_word_to_id,
_device=device, _pointwise=pointwise, _debug=False)
optimizer = optim.Adam(list(filter(lambda p: p.requires_grad, modeler.encoder_q.parameters())) +
list(filter(lambda p: p.requires_grad, modeler.encoder_p.parameters())),
weight_decay=0.0001)
modeler.load_from(model_path)
if training_model == 'ulmfit_slotptr':
modeler = net.ULMFITQelosSlotPointerModel( _parameter_dict = parameter_dict,_word_to_id=_word_to_id,
_device=device,_pointwise=pointwise, _debug=False)
optimizer = optim.Adam(list(filter(lambda p: p.requires_grad, modeler.encoder_q.parameters())) +
list(filter(lambda p: p.requires_grad, modeler.encoder_p.parameters())), weight_decay=0.0001, lr=0.01)
if not pointwise:
loss_func = nn.MarginRankingLoss(margin=1,size_average=False)
else:
loss_func = nn.BCEWithLogitsLoss()
training_model += '_pointwise'
for iterations, sample_batched in enumerate(train_loader):
pass
parameter_dict['epochs'] = 100
# lr_args = {'iterations': (iterations + 1) * parameter_dict['epochs']}
# lr_args = {'iterations': (iterations + 1) * parameter_dict['epochs'],'cycles':2}
lr_args = {'iterations': (iterations + 1) * parameter_dict['epochs'], 'cut_frac': 0.1, 'ratio': 32}
# lr_schedule = lriters.LearningRateScheduler(optimizer, lr_args, lriters.CosineAnnealingLR)
# lr_schedule = lriters.LearningRateScheduler(optimizer, lr_args, lriters.ConstantLR)
lr_schedule = lriters.LearningRateScheduler(optimizer, lr_args, lriters.SlantedTriangularLR)
train_loss, modeler, valid_accuracy, test_accuracy,model_save_location = training_loop(training_model = training_model,
parameter_dict = parameter_dict,
modeler = modeler,
train_loader = train_loader,
optimizer=optimizer,
loss_func=loss_func,
data=data,
dataset=parameter_dict['dataset'],
device=device,
test_every=test_every,
validate_every=validate_every,
pointwise=pointwise,
lrschedule = lr_schedule,
problem='core_chain')
print(valid_accuracy)
print(test_accuracy)
print("validation accuracy is , ", max(valid_accuracy))
print("maximum test accuracy is , ", max(test_accuracy))
print("correct test accuracy i.e test accuracy where validation is highest is ", test_accuracy[valid_accuracy.index(max(valid_accuracy))])
print("model saved at, " , model_save_location )
msl = model_save_location.split('/')
print(f"model save locaton info {msl}")
json.dump(train_loss,open(f"{model_save_location}/loss.json",'w+'))
if evals:
evaluate(device=device,
pointwise=pointwise,
dataset=msl[-2],
training_model=msl[-3],
training_model_number=msl[-1])
# rsync -avz --progress corechain.py qrowdgpu+titan:/shared/home/GauravMaheshwari/new_kranti/KrantikariQA/
# rsync -avz --progress auxiliary.py qrowdgpu+titan:/shared/home/GauravMaheshwari/new_kranti/KrantikariQA/
# rsync -avz --progress network.py qrowdgpu+titan:/shared/home/GauravMaheshwari/new_kranti/KrantikariQA/
# rsync -avz --progress components.py qrowdgpu+titan:/shared/home/GauravMaheshwari/new_kranti/KrantikariQA/
# rsync -avz --progress data_loader.py qrowdgpu+titan:/shared/home/GauravMaheshwari/new_kranti/KrantikariQA/
# rsync -avz --progress corechain.py priyansh@sda-srv04:/data/priyansh/new_kranti
# rsync -avz --progress auxiliary.py priyansh@sda-srv04:/data/priyansh/new_kranti
# rsync -avz --progress components.py priyansh@sda-srv04:/data/priansh/new_kranti
# rsync -avz --progress network.py priyansh@sda-srv04:/data/priyansh/new_kranti
# [0.16, 0.19, 0.24, 0.24, 0.28, 0.3, 0.25, 0.27, 0.29, 0.27, 0.26, 0.28, 0.27, 0.3, 0.32, 0.29, 0.31, 0.3, 0.31,
# 0.28, 0.31, 0.32, 0.29, 0.31, 0.32, 0.34, 0.32, 0.31, 0.32, 0.33, 0.33, 0.32, 0.33, 0.32, 0.32, 0.33, 0.32, 0.33,
# 0.32, 0.37, 0.35, 0.34, 0.33, 0.34, 0.34, 0.36, 0.39]
# [0.2, 0.235, 0.25, 0.29, 0.3, 0.325, 0.28, 0.315, 0.29, 0.325, 0.305, 0.33, 0.35, 0.315, 0.335, 0.315, 0.35, 0.345,
# 0.325, 0.365, 0.335, 0.35, 0.36, 0.385, 0.37, 0.355, 0.35, 0.375, 0.345, 0.345, 0.36, 0.385, 0.4, 0.375, 0.38,
# 0.35, 0.395, 0.385, 0.38, 0.39, 0.365, 0.4, 0.395, 0.39, 0.39, 0.4, 0.4]
# validation
# accuracy is, 0.39
# maximum
# test
# accuracy is, 0.4
# correct
# test
# accuracy
# i.e
# test
# accuracy
# where
# validation is highest is 0.4