forked from sled-group/Verifiable-Coherent-NLU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
303 lines (258 loc) · 11.3 KB
/
main.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
import os
import pickle
import torch
import transformers
from argparse import ArgumentParser
from sklearn.metrics import accuracy_score, f1_score
from transformers import get_linear_schedule_with_warmup
from www.model.transformers_ext import TieredModelPipeline
from www.model.eval import evaluate_tiered, save_results, save_preds, add_entity_attribute_labels
from www.model.train import train_epoch_tiered
from www.utils import print_dict, get_model_dir
from www.dataset.ann import att_to_idx, att_to_num_classes, att_types
from src.dataloading import get_dataloaders
from src.preprocessing import data_setup, get_baseline, get_tensor_dataset
from src.utils import get_components
def main(args):
# Print run configuration
print("====================")
print("JOB CONFIGURATION")
print(f"Model: {args.model}")
print(f"Objective: {args.objective}")
print(f"Loss weights: {args.loss_weights}")
print(f"gamma: {args.gamma}")
print(f"alpha: {args.alpha}")
print(f"lambda_const: {args.lambda_const}")
print(f"p_th: {args.p_th}")
print(f"Batch size: {args.batch_size}")
print(f"Eval batch size: {args.eval_batch_size}")
print(f"Test batch size: {args.test_batch_size}")
print(f"Epochs: {args.num_epochs}")
print(f"Learning rate: {args.learning_rate}")
print("====================")
# Get model-related components (LM and tokenizer)
model_name, model_class, config_class, emb_class, tokenizer, lm_class = get_components(args.model, args.cache_dir)
# Preprocess data
print('Preprocessing data.')
cloze_dataset_2s, order_dataset_2s = data_setup()
# print('here')
tiered_dataset = get_baseline(cloze_dataset_2s, tokenizer)
# print('here now')
tiered_tensor_dataset = get_tensor_dataset(tiered_dataset)
# Create dataloaders for train, val, and test datasets
print('Getting dataloaders.')
train_dataloader, dev_dataloader, test_dataloader = get_dataloaders(args, tiered_tensor_dataset)
dev_dataset_name = args.subtask + '_%s_dev'
dev_ids = [ex['example_id'] for ex in tiered_dataset['dev']]
# Set number of state variables
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]
# Set up model
config = config_class.from_pretrained(
model_name,
cache_dir=args.cache_dir
)
# Set up embedding
emb = emb_class.from_pretrained(
model_name,
config=config,
cache_dir=args.cache_dir
)
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]])
# Initialize model
print('Initializing model.')
model = TieredModelPipeline(
emb,
max_story_length,
len(att_to_num_classes),
num_state_labels,
config_class,
model_name,
device,
ablation=args.ablation,
objective=args.objective,
loss_weights=args.loss_weights,
gamma=args.gamma,
alpha=args.alpha,
lambda_const=args.lambda_const,
p_th=args.p_th
).to(device)
# Initialize optimizer and scheduler
print('Initializing optimizer and scheduler.')
optimizer = torch.optim.AdamW(model.parameters(), lr=args.learning_rate)
scheduler = get_linear_schedule_with_warmup(
optimizer,
num_warmup_steps=0,
num_training_steps=len(train_dataloader) * args.num_epochs
)
# Initialize variables to track
train_lc_data = []
val_lc_data = []
loss_values = []
obj_values = []
# Train model
print('Training model')
for epoch in range(args.num_epochs):
train_loss, _ = train_epoch_tiered(
model,
optimizer,
train_dataloader,
device,
epoch=epoch,
seg_mode=False,
build_learning_curves=args.generate_learning_curve,
val_dataloader=dev_dataloader,
train_lc_data=train_lc_data,
val_lc_data=val_lc_data,
grad_surgery=args.grad_surgery
)
loss_values.append(train_loss)
# Validate on dev set
validation_results = evaluate_tiered(
model,
dev_dataloader,
device,
[(accuracy_score, 'accuracy'), (f1_score, 'f1')],
epoch,
seg_mode=False,
return_explanations=True
)
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 results
print(f"Epoch ({epoch + 1}/{args.num_epochs} -- train_loss: {train_loss}, val_loss: {0}")
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_dir = get_model_dir(
model_name.replace('/', '-'),
args.subtask,
args.batch_size,
args.learning_rate,
epoch
)
model_param_str = model_dir + '_' + '-'.join([str(lw) for lw in args.loss_weights]) + '_tiered_pipeline_lc'
if args.train_spans:
model_param_str += 'spans'
if len(model.ablation) > 0:
model_param_str += '_ablate_'
model_param_str += '_'.join(model.ablation)
# Set up output directory
output_dir = os.path.join(args.output_dir, 'saved_models', model_param_str)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Save validation results
# 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)
# Save gamma history (if gamma weighted update)
if args.objective == 'gamma':
with open(os.path.join(output_dir, 'gamma_history.pkl'), 'wb') as f:
pickle.dump(model.gamma_history, f)
# Test model (#TODO: implement testing)
print("Testing model")
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,
test_dataloader,
device,
[(accuracy_score, 'accuracy'), (f1_score, 'f1')],
epoch,
seg_mode=False,
return_explanations=True
)
explanations = add_entity_attribute_labels(explanations, tiered_dataset['test'], list(att_to_num_classes.keys()))
test_dataset_name = args.subtask + '_%s_test'
# save_results(metr_attr, output_dir, test_dataset_name % 'attributes')
# save_results(metr_prec, output_dir, test_dataset_name % 'preconditions')
# save_results(metr_eff, output_dir, test_dataset_name % 'effects')
# save_results(metr_conflicts, output_dir, test_dataset_name % 'conflicts')
# save_results(metr_stories, output_dir, test_dataset_name % 'stories')
# save_results(explanations, output_dir, test_dataset_name % 'explanations')
print('Stories:')
print_dict(metr_stories)
print('Conflicts:')
print_dict(metr_conflicts)
print('Preconditions:')
print_dict(metr_prec)
print('Effects:')
print_dict(metr_eff)
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
print('Found %s consistent preds (versus %s verifiable) out of %s total' % (str(consistent_preds), str(verifiable_preds), str(total)))
print('Consistency: %s' % str(float(consistent_preds) / total))
if __name__ == "__main__":
parser = ArgumentParser(description="Train and test model on TRIP.")
transformers.logging.set_verbosity_error()
# Model
parser.add_argument("--dataset", type=str, default="trip")
parser.add_argument("--model", type=str, default="roberta")
parser.add_argument("--ablation", type=list, default=["attributes", "states-logits"])
parser.add_argument("--subtask", type=str, default="cloze", choices=["cloze", "order"])
parser.add_argument("--train_spans", type=bool, default=False)
# Objective-related hyperparameters
parser.add_argument("--objective", type=str, choices=["default", "sigmoid", "gamma"], default="default")
parser.add_argument("--grad-surgery", type=bool, default=False)
parser.add_argument("--loss_weights", type=list, default=[0.0, 0.4, 0.4, 0.2, 0.0])
parser.add_argument("--gamma", type=float, default=0.1)
parser.add_argument("--alpha", type=float, default=0.9)
parser.add_argument("--lambda_const", type=float, nargs=4, default=[1.0, 1.0, 1.0, 1.0])
parser.add_argument("--p_th", type=float, nargs=4, default=[0.0, 0.0, 2.0, 5.0])
# Hyperparameters
parser.add_argument("--batch_size", type=int, default=1)
parser.add_argument("--eval_batch_size", type=int, default=16)
parser.add_argument("--test_batch_size", type=int, default=8)
parser.add_argument("--num_epochs", type=int, default=10)
parser.add_argument("--learning_rate", type=float, default=1e-5)
# Logging
parser.add_argument("--output_dir", type=str, default="./output")
parser.add_argument("--cache_dir", type=str, default="./cache")
parser.add_argument("--generate_learning_curve", type=bool, default=True)
args = parser.parse_args()
main(args)