-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtrain_dnnc.py
328 lines (265 loc) · 13.3 KB
/
train_dnnc.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
# Copyright 2020, Salesforce.com, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import argparse
from tqdm import tqdm
import random
import os
import json
from collections import defaultdict
from models.dnnc import DNNC
from models.dnnc import ENTAILMENT, NON_ENTAILMENT
from models.utils import InputExample
from models.utils import load_intent_datasets, load_intent_examples, sample, print_results
from models.utils import calc_oos_precision, calc_in_acc, calc_oos_recall, calc_oos_f1
from models.utils import THRESHOLDS
from intent_predictor import DnncIntentPredictor
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--seed",
default=42,
type=int,
help="Random seed")
parser.add_argument("--bert_model",
default='roberta-base',
type=str,
help="BERT model")
parser.add_argument("--train_batch_size",
default=370,
type=int,
help="Total batch size for training.")
parser.add_argument("--eval_batch_size",
default=8,
type=int,
help="Total batch size for eval.")
parser.add_argument("--learning_rate",
default=1e-5,
type=float,
help="The initial learning rate for Adam.")
parser.add_argument("--num_train_epochs",
default=7,
type=float,
help="Total number of training epochs to perform.")
parser.add_argument("--warmup_proportion",
default=0.1,
type=float,
help="Proportion of training to perform linear learning rate warmup for. "
"E.g., 0.1 = 10%% of training.")
parser.add_argument("--adam_epsilon", default=1e-8, type=float, help="Epsilon for Adam optimizer.")
parser.add_argument("--no_cuda",
action='store_true',
help="Whether not to use CUDA when available")
parser.add_argument('--gradient_accumulation_steps',
type=int,
default=2,
help="Number of updates steps to accumulate before performing a backward/update pass.")
parser.add_argument('--max_grad_norm', help='gradient clipping for Max gradient norm.', required=False, default=1.0,
type=float)
parser.add_argument('--label_smoothing',
type = float,
default = 0.1,
help = 'Coefficient for label smoothing (default: 0.1, if 0.0, no label smoothing)')
parser.add_argument('--max_seq_length',
type = int,
default = 128,
help = 'Maximum number of paraphrases for each sentence')
parser.add_argument("--do_lower_case",
action='store_true',
help="Whether to lowercase input string")
# Special params
parser.add_argument('--train_file_path',
type = str,
default = None,
help = 'Training data path')
parser.add_argument('--dev_file_path',
type = str,
default = None,
help = 'Validation data path')
parser.add_argument('--oos_dev_file_path',
type = str,
default = None,
help = 'Out-of-Scope validation data path')
parser.add_argument('--output_dir',
type = str,
default = None,
help = 'Output file path')
parser.add_argument('--save_model_path',
type = str,
default = '',
help = 'path to save the model checkpoints')
parser.add_argument('--bert_nli_path',
type = str,
default = '',
help = 'The bert checkpoints which are fine-tuned with NLI datasets')
parser.add_argument("--scratch",
action='store_true',
help="Whether to start from the original BERT")
parser.add_argument('--over_sampling',
type = int,
default = 0,
help = 'Over-sampling positive examples as there are more negative examples')
parser.add_argument('--few_shot_num',
type = int,
default = 5,
help = 'Number of training examples for each class')
parser.add_argument('--num_trials',
type = int,
default = 10,
help = 'Number of trials to see robustness')
parser.add_argument("--do_predict",
action='store_true',
help="do_predict the model")
parser.add_argument("--do_final_test",
action='store_true',
help="do_predict the model")
args = parser.parse_args()
random.seed(args.seed)
N = args.few_shot_num
T = args.num_trials
train_file_path = args.train_file_path
dev_file_path = args.dev_file_path
train_examples, dev_examples = load_intent_datasets(train_file_path, dev_file_path, args.do_lower_case)
sampled_tasks = [sample(N, train_examples) for i in range(T)]
if args.oos_dev_file_path is not None:
oos_dev_examples = load_intent_examples(args.oos_dev_file_path, args.do_lower_case)
else:
oos_dev_examples = []
nli_train_examples = []
nli_dev_examples = []
for i in range(T):
if args.do_predict:
nli_train_examples.append([])
nli_dev_examples.append([])
continue
tasks = sampled_tasks[i]
all_entailment_examples = []
all_non_entailment_examples = []
# entailement
for task in tasks:
examples = task['examples']
for j in range(len(examples)):
for k in range(len(examples)):
if k <= j:
continue
all_entailment_examples.append(InputExample(examples[j], examples[k], ENTAILMENT))
all_entailment_examples.append(InputExample(examples[k], examples[j], ENTAILMENT))
# non entailment
for task_1 in range(len(tasks)):
for task_2 in range(len(tasks)):
if task_2 <= task_1:
continue
examples_1 = tasks[task_1]['examples']
examples_2 = tasks[task_2]['examples']
for j in range(len(examples_1)):
for k in range(len(examples_2)):
all_non_entailment_examples.append(InputExample(examples_1[j], examples_2[k], NON_ENTAILMENT))
all_non_entailment_examples.append(InputExample(examples_2[k], examples_1[j], NON_ENTAILMENT))
nli_train_examples.append(all_entailment_examples + all_non_entailment_examples)
nli_dev_examples.append(all_entailment_examples[:100] + all_non_entailment_examples[:100]) # sanity check for over-fitting
for j in range(args.over_sampling):
nli_train_examples[-1] += all_entailment_examples
if args.output_dir is not None:
if args.scratch:
folder_name = '{}/{}-shot-{}_nli__Scratch/'.format(args.output_dir, N, args.bert_model)
else:
folder_name = '{}/{}-shot-{}_nli__Based_on_nli_fine_tuned_model/'.format(args.output_dir, N, args.bert_model)
if not os.path.exists(folder_name):
os.makedirs(folder_name)
file_name = 'batch_{}---epoch_{}---lr_{}---trials_{}'.format(args.train_batch_size,
args.num_train_epochs,
args.learning_rate, args.num_trials)
file_name = '{}__oos-threshold'.format(file_name)
if args.scratch:
file_name = '{}__scratch'.format(file_name)
else:
file_name = '{}__based_on_nli_fine_tuned_model'.format(file_name)
if args.over_sampling:
file_name = file_name + '--over_sampling'
if args.do_final_test:
file_name = file_name + '_TEST.txt'
else:
file_name = file_name + '.txt'
f = open(folder_name+file_name, 'w')
else:
f = None
if args.scratch:
BERT_NLI_PATH = None
else:
BERT_NLI_PATH = args.bert_nli_path
assert os.path.exists(BERT_NLI_PATH)
if args.save_model_path and args.do_predict:
stats_lists_preds = defaultdict(list)
for j in range(T):
save_model_path = '{}_{}'.format(folder_name+args.save_model_path, j+1)
if os.path.exists(save_model_path):
assert args.do_predict
else:
assert not args.do_predict
if args.save_model_path and os.path.exists(save_model_path):
if args.do_predict:
trial_stats_preds = defaultdict(list)
model = DNNC(path = save_model_path,
args = args)
else:
model = DNNC(path = BERT_NLI_PATH,
args = args)
model.train(nli_train_examples[j], nli_dev_examples[j])
if args.save_model_path:
if not os.path.exists(save_model_path):
os.mkdir(save_model_path)
model.save(save_model_path)
intent_predictor = DnncIntentPredictor(model, sampled_tasks[j])
in_domain_preds = []
oos_preds = []
for e in tqdm(dev_examples, desc = 'Intent examples'):
pred, conf, matched_example = intent_predictor.predict_intent(e.text)
in_domain_preds.append((conf, pred))
if args.save_model_path and args.do_predict:
if not trial_stats_preds[e.label]:
trial_stats_preds[e.label] = []
single_pred = {}
single_pred['gold_example'] = e.text
single_pred['match_example'] = matched_example
single_pred['gold_label'] = e.label
single_pred['pred_label'] = pred
single_pred['conf'] = conf
trial_stats_preds[e.label].append(single_pred)
for e in tqdm(oos_dev_examples, desc = 'OOS examples'):
pred, conf, matched_example = intent_predictor.predict_intent(e.text)
oos_preds.append((conf, pred))
if args.save_model_path and args.do_predict:
if not trial_stats_preds[e.label]:
trial_stats_preds[e.label] = []
single_pred = {}
single_pred['gold_example'] = e.text
single_pred['match_example'] = matched_example
single_pred['gold_label'] = e.label
single_pred['pred_label'] = pred
single_pred['conf'] = conf
trial_stats_preds[e.label].append(single_pred)
if args.save_model_path and args.do_predict:
stats_lists_preds[j] = trial_stats_preds
in_acc = calc_in_acc(dev_examples, in_domain_preds, THRESHOLDS)
oos_recall = calc_oos_recall(oos_preds, THRESHOLDS)
oos_prec = calc_oos_precision(in_domain_preds, oos_preds, THRESHOLDS)
oos_f1 = calc_oos_f1(oos_recall, oos_prec)
print_results(THRESHOLDS, in_acc, oos_recall, oos_prec, oos_f1)
if f is not None:
for i in range(len(in_acc)):
f.write('{},{},{},{} '.format(in_acc[i], oos_recall[i], oos_prec[i], oos_f1[i]))
f.write('\n')
if f is not None:
f.close()
if args.save_model_path and args.do_predict:
if args.do_final_test:
save_file = folder_name + "dev_examples_predictions_TEST.json"
else:
save_file = folder_name+"dev_examples_predictions.json"
with open(save_file, "w") as outfile:
json.dump(stats_lists_preds, outfile, indent=4)
if __name__ == '__main__':
main()