-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_test_trip.py
412 lines (345 loc) · 16.5 KB
/
train_test_trip.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
import os
import json
import sys
import torch
import random
import numpy as np
import spacy
from tqdm import tqdm
from www.utils import print_dict
from collections import Counter
from www.dataset.prepro import get_tiered_data, balance_labels
from www.dataset.featurize import add_bert_features_tiered, get_tensor_dataset_tiered
from collections import Counter
from transformers import BertTokenizer, RobertaTokenizer, DebertaTokenizer, AlbertTokenizer, T5Tokenizer, GPT2Tokenizer
from transformers import BertForSequenceClassification, RobertaForSequenceClassification, DebertaForSequenceClassification, AlbertForSequenceClassification, AdamW
from transformers import BertForMultipleChoice, RobertaForMultipleChoice, AlbertForMultipleChoice, DebertaModel
from transformers import BertModel, RobertaModel, AlbertModel, DebertaModel, T5Model, T5EncoderModel, GPT2Model
from transformers import RobertaForMaskedLM
from transformers import BertConfig, RobertaConfig, DebertaConfig, AlbertConfig, T5Config, GPT2Config
from www.model.transformers_ext import DebertaForMultipleChoice
from torch.optim import Adam
from DeBERTa import deberta
from www.utils import print_dict
import pickle
import gensim
from nltk.parse.stanford import StanfordDependencyParser
from www.dataset.ann import att_to_idx, att_to_num_classes, att_types
from torch.utils.data import DataLoader, RandomSampler, SequentialSampler
from transformers import get_linear_schedule_with_warmup
from www.model.train import train_epoch_tiered
from www.model.eval import evaluate_tiered, save_results, save_preds, add_entity_attribute_labels
from www.utils import print_dict, get_model_dir
from www.model.transformers_ext import TieredModelPipeline
from www.dataset.ann import att_to_num_classes
import shutil
import pandas as pd
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix
import argparse
parser = argparse.ArgumentParser(description='File path of preprocessed data')
parser.add_argument('--drive_path', type=str, default='',help='Path for running the code.')
parser.add_argument('--pkl_file_path', type=str, default='', help='Path for preprocessed data for TRIP with dependency graphs and ConceptNet Numberbatch features')
parser.add_argument('--cn_nb_path', type=str, default='',help='Path for ConceptNet Numberbatch embedding file.')
args = parser.parse_args()
DRIVE_PATH = args.drive_path
sys.path.append(DRIVE_PATH)
# mode = 'bert' # BERT large
mode = 'roberta' # RoBERTa large
# mode = 'roberta_mnli' # RoBERTa large pre-trained on MNLI
# mode = 'deberta' # DeBERTa base for training on TRIP
# mode = 'deberta_large' # DeBERTa large for training on CE and ART
task_name = 'trip'
# task_name = 'ce'
# task_name = 'art'
debug = False
# debug = True
train_spans = False
config_batch_size = 1
config_lr = 1e-5 # Selected learning rate for best RoBERTa-based model in TRIP paper
config_epochs = 10
# config_epochs = 2
# Loss weights for (attributes, preconditions, effects, conflicts, story choices)
if task_name != 'trip':
print("We do not need a loss weighting scheme for %s dataset. Ignoring this cell." % task_name)
# loss_weights = [0.0, 0.4, 0.4, 0.1, 0.1] # "All losses"
loss_weights = [0.0, 0.4, 0.4, 0.2, 0.0] # "Omit story choice loss"
if task_name in ['trip', 'ce']:
multiple_choice = False
elif task_name == 'art':
multiple_choice = True
else:
raise ValueError("Task name should be set to 'trip', 'ce', or 'art' in the first cell of the notebook!")
if mode == 'bert':
model_name = 'bert-large-uncased'
elif mode == 'roberta':
model_name = 'roberta-large'
elif mode == 'roberta_mnli':
model_name = 'roberta-large-mnli'
elif mode == 'deberta':
model_name = 'microsoft/deberta-base'
elif mode == 'deberta_large':
model_name = 'microsoft/deberta-large'
if mode in ['bert']:
tokenizer_class = BertTokenizer
elif mode in ['roberta', 'roberta_mnli']:
tokenizer_class = RobertaTokenizer
elif mode in ['deberta', 'deberta_large']:
tokenizer_class = DebertaTokenizer
tokenizer = tokenizer_class.from_pretrained(model_name,
do_lower_case = False,
cache_dir=os.path.join(DRIVE_PATH, 'cache'))
if not multiple_choice:
if mode == 'bert':
model_class = BertForSequenceClassification
config_class = BertConfig
emb_class = BertModel
elif mode in ['roberta', 'roberta_mnli']:
model_class = RobertaForSequenceClassification
config_class = RobertaConfig
emb_class = RobertaModel
lm_class = RobertaForMaskedLM
elif mode in ['deberta', 'deberta_large']:
model_class = DebertaForSequenceClassification
config_class = DebertaConfig
emb_class = DebertaModel
else:
if mode == 'bert':
model_class = BertForMultipleChoice
config_class = BertConfig
emb_class = BertModel
elif mode in ['roberta', 'roberta_mnli']:
model_class = RobertaForMultipleChoice
config_class = RobertaConfig
emb_class = RobertaModel
lm_class = RobertaForMaskedLM
elif mode in ['deberta', 'deberta_large']:
model_class = DebertaForMultipleChoice
config_class = DebertaConfig
emb_class = DebertaModel
partitions = ['train', 'dev', 'test']
subtasks = ['cloze', 'order']
pickle_file = open(args.pkl_file_path,'rb')
while True:
try:
tiered_dataset = pickle.load(pickle_file)
except EOFError:
break
pickle_file.close()
seq_length = 16 # Max sequence length to pad to
cn_nb = gensim.models.KeyedVectors.load_word2vec_format(args.cn_nb_path, binary=False)
tiered_tensor_dataset = {}
label_entities = {}
dep_graphs = {}
graph_labels = {}
max_story_length = max([len(ex['stories'][0]['sentences']) for p in tiered_dataset for ex in tiered_dataset[p]])
for p in tiered_dataset:
tiered_tensor_dataset[p], label_entities[p], dep_graphs[p], graph_labels[p] = get_tensor_dataset_tiered(tiered_dataset[p], max_story_length, add_segment_ids=True)
subtask = 'cloze'
batch_sizes = [config_batch_size]
learning_rates = [config_lr]
epochs = config_epochs
eval_batch_size = 1
generate_learning_curve = False # Generate data for training curve figure in TRIP paper
# generate_learning_curve = True # Generate data for training curve figure in TRIP paper
num_state_labels = {}
for att in att_to_idx:
if att_types[att] == 'default':
num_state_labels[att_to_idx[att]] = 3
else:
num_state_labels[att_to_idx[att]] = att_to_num_classes[att] # Location attributes fall into this since they don't have well-define pre- and post-condition yet
# Ablation options:
# - attributes: skip attribute prediction phase
# - embeddings: DON'T input contextual embeddings to conflict detector
# - states: DON'T input states to conflict detector
# - states-labels: in states input to conflict detector, include predicted labels
# - states-logits: in states input to conflict detector, include state logits (preferred)
# - states-teacher-forcing: train conflict detector on ground truth state labels (not predictions)
# - states-attention: re-weight input to conflict detector with weights conditioned on states representation
ablation = ['attributes', 'states-logits'] # This is the default mode presented in the paper
seed_val = 22 # Save random seed for reproducibility
random.seed(seed_val)
np.random.seed(seed_val)
torch.manual_seed(seed_val)
torch.cuda.manual_seed_all(seed_val)
# We'll keep the validation data here with a constant eval batch size
dev_sampler = SequentialSampler(tiered_tensor_dataset['dev'])
dev_dataloader = DataLoader(tiered_tensor_dataset['dev'], sampler=dev_sampler, batch_size=eval_batch_size)
dev_dataset_name = subtask + '_%s_dev'
dev_ids = [ex['example_id'] for ex in tiered_dataset['dev']]
all_losses = []
param_combos = []
combo_names = []
all_val_objs = []
output_dirs = []
best_obj = 0.0
best_model = '<none>'
best_dir = ''
best_obj2 = 0.0
best_model2 = '<none>'
best_dir2 = ''
feature_size = cn_nb['heat'].shape[0]
np.random.seed(0)
print('Beginning grid search for the %s sub-task over %s parameter combination(s)!' % (subtask, str(len(batch_sizes) * len(learning_rates))))
for bs in batch_sizes:
for lr in learning_rates:
print('\nTRAINING MODEL: bs=%s, lr=%s' % (str(bs), str(lr)))
loss_values = []
obj_values = []
# Set up training dataset with new batch size
train_sampler = SequentialSampler(tiered_tensor_dataset['train'])
train_dataloader = DataLoader(tiered_tensor_dataset['train'], sampler=train_sampler, batch_size=bs)
# Set up model
config = config_class.from_pretrained(model_name,
cache_dir=os.path.join(DRIVE_PATH, 'cache'))
emb = emb_class.from_pretrained(model_name,
config=config,
cache_dir=os.path.join(DRIVE_PATH, 'cache'))
if torch.cuda.is_available():
emb.cuda()
device = emb.device
max_story_length = max([len(ex['stories'][0]['sentences']) for p in tiered_dataset for ex in tiered_dataset[p]])
model = TieredModelPipeline(emb, max_story_length, len(att_to_num_classes), num_state_labels,
config_class, model_name, device, feature_size,
ablation=ablation, loss_weights=loss_weights).to(device)
# Set up optimizer
optimizer = AdamW(model.parameters(), lr=lr)
total_steps = len(train_dataloader) * epochs
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=0, num_training_steps = total_steps)
train_lc_data = []
val_lc_data = []
for epoch in tqdm(range(epochs)):
# Train the model for one epoch
print('[%s] Beginning epoch...' % str(epoch))
epoch_loss, _ = train_epoch_tiered(model, optimizer, train_dataloader, device, seg_mode=False,
build_learning_curves=generate_learning_curve, val_dataloader=dev_dataloader,
train_lc_data=train_lc_data, val_lc_data=val_lc_data,dep_graphs=dep_graphs,label_entities=label_entities,graph_labels=graph_labels,cn_nb=cn_nb)
# Save loss
loss_values.append(epoch_loss)
# Validate on dev set
validation_results = evaluate_tiered(model, dev_dataloader, device, [(accuracy_score, 'accuracy'), (f1_score, 'f1')], seg_mode=False, return_explanations=True,dep_graphs=dep_graphs['dev'],label_entities=label_entities['dev'],graph_labels=graph_labels['dev'],cn_nb=cn_nb)
metr_attr, all_pred_atts, all_atts, \
metr_prec, all_pred_prec, all_prec, \
metr_eff, all_pred_eff, all_eff, \
metr_conflicts, all_pred_conflicts, all_conflicts, \
metr_stories, all_pred_stories, all_stories, explanations = validation_results[:16]
explanations = add_entity_attribute_labels(explanations, tiered_dataset['dev'], list(att_to_num_classes.keys()))
print('[%s] Validation results:' % str(epoch))
print('[%s] Preconditions:' % str(epoch))
print_dict(metr_prec)
print('[%s] Effects:' % str(epoch))
print_dict(metr_eff)
print('[%s] Conflicts:' % str(epoch))
print_dict(metr_conflicts)
print('[%s] Stories:' % str(epoch))
print_dict(metr_stories)
# Save accuracy - want to maximize verifiability of tiered predictions
ver = metr_stories['verifiability']
acc = metr_stories['accuracy']
obj_values.append(ver)
# Save model checkpoint
print('[%s] Saving model checkpoint...' % str(epoch))
model_param_str = get_model_dir(model_name.replace('/', '-'), subtask, bs, lr, epoch) + '_' + '-'.join([str(lw) for lw in loss_weights]) + '_tiered_pipeline_lc'
if train_spans:
model_param_str += 'spans'
if len(model.ablation) > 0:
model_param_str += '_ablate_'
model_param_str += '_'.join(model.ablation)
output_dir = os.path.join(DRIVE_PATH, 'saved_models_complex', model_param_str)
output_dirs.append(output_dir)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
save_results(metr_attr, output_dir, dev_dataset_name % 'attributes')
save_results(metr_prec, output_dir, dev_dataset_name % 'preconditions')
save_results(metr_eff, output_dir, dev_dataset_name % 'effects')
save_results(metr_conflicts, output_dir, dev_dataset_name % 'conflicts')
save_results(metr_stories, output_dir, dev_dataset_name % 'stories')
save_results(explanations, output_dir, dev_dataset_name % 'explanations')
# Just save story preds
save_preds(dev_ids, all_stories, all_pred_stories, output_dir, dev_dataset_name % 'stories')
emb = emb.module if hasattr(emb, 'module') else emb
emb.save_pretrained(output_dir)
torch.save(model, os.path.join(output_dir, 'classifiers.pth'))
tokenizer.save_vocabulary(output_dir)
if ver > best_obj:
best_obj = ver
best_model = model_param_str
best_dir = output_dir
if acc > best_obj2:
best_obj2 = acc
best_model2 = model_param_str
best_dir2 = output_dir
for od in output_dirs:
if od != best_dir and od != best_dir2 and os.path.exists(od):
shutil.rmtree(od)
print('[%s] Finished epoch.' % str(epoch))
all_losses.append(loss_values)
all_val_objs.append(obj_values)
param_combos.append((bs, lr))
combo_names.append('bs=%s, lr=%s' % (str(bs), str(lr)))
print('Finished grid search! :)')
print('Best validation *verifiability* %s from model %s.' % (str(best_obj), best_model))
print('Best validation *accuracy* %s from model %s.' % (str(best_obj2), best_model2))
if generate_learning_curve:
print('Saving learning curve data...')
train_lc_data = [subrecord for record in train_lc_data for subrecord in record] # flatten
val_lc_data = [subrecord for record in val_lc_data for subrecord in record] # flatten
train_lc_data = pd.DataFrame(train_lc_data)
print(os.path.join(best_dir if best_dir != '<none>' else best_dir2, 'learning_curve_data_train.csv'))
train_lc_data.to_csv(os.path.join(best_dir if best_dir != '' else best_dir2, 'learning_curve_data_train.csv'), index=False)
val_lc_data = pd.DataFrame(val_lc_data)
val_lc_data.to_csv(os.path.join(best_dir if best_dir != '' else best_dir2, 'learning_curve_data_val.csv'), index=False)
print('Learning curve data saved. %s rows saved for training, %s rows saved for validation.' % (str(len(train_lc_data.index)), str(len(val_lc_data.index))))
# import shutil
# Delete non-best model checkpoints
for od in output_dirs:
if od != best_dir and od != best_dir2 and os.path.exists(od):
shutil.rmtree(od)
metrics = [(accuracy_score, 'accuracy'), (precision_score, 'precision'), (recall_score, 'recall'), (f1_score, 'f1')]
for layer in model.precondition_classifiers:
layer.eval()
for layer in model.effect_classifiers:
layer.eval()
print('Testing model ')
# May alter this depending on which partition(s) you want to run inference on
for p in tiered_dataset:
if p != 'test':
continue
p_dataset = tiered_dataset[p]
p_tensor_dataset = tiered_tensor_dataset[p]
p_sampler = SequentialSampler(p_tensor_dataset)
p_dataloader = DataLoader(p_tensor_dataset, sampler=p_sampler, batch_size=1)
dev_dataset_name = subtask + '_%s_' + p
p_ids = [ex['example_id'] for ex in tiered_dataset[p]]
# Get preds and metrics on this partition
metr_attr, all_pred_atts, all_atts, \
metr_prec, all_pred_prec, all_prec, \
metr_eff, all_pred_eff, all_eff, \
metr_conflicts, all_pred_conflicts, all_conflicts, \
metr_stories, all_pred_stories, all_stories, explanations = evaluate_tiered(model, p_dataloader, device, [(accuracy_score, 'accuracy'), (f1_score, 'f1')], seg_mode=False, return_explanations=True,dep_graphs=dep_graphs[p],label_entities=label_entities[p],graph_labels=graph_labels[p],cn_nb=cn_nb)
explanations = add_entity_attribute_labels(explanations, tiered_dataset[p], list(att_to_num_classes.keys()))
print('\nPARTITION: %s' % p)
print('Stories:')
print_dict(metr_stories)
print('Conflicts:')
print_dict(metr_conflicts)
print('Preconditions:')
print_dict(metr_prec)
print('Effects:')
print_dict(metr_eff)
partitions = ['dev', 'test']
for p in partitions:
consistent_preds = 0
verifiable_preds = 0
total = 0
for expl in explanations:
if expl['valid_explanation']:
verifiable_preds += 1
if expl['story_pred'] == expl['story_label']:
if len(expl['conflict_pred']) == len(expl['conflict_label']) and expl['conflict_pred'][0] == expl['conflict_label'][0] and expl['conflict_pred'][1] == expl['conflict_label'][1]:
expl['consistent'] = True
consistent_preds += 1
else:
expl['consistent'] = False
total += 1
float(consistent_preds) / total
print('consistency in {}:{}'.format(p,float(consistent_preds) / total))