-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtrain_classifier.py
216 lines (181 loc) · 8.98 KB
/
train_classifier.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
# 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.classifier import Classifier
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
import time
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=15,
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=5e-5,
type=float,
help="The initial learning rate for Adam.")
parser.add_argument("--num_train_epochs",
default=25.0,
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', #Store_true: false
help="Whether not to use CUDA when available")
parser.add_argument('--gradient_accumulation_steps',
type=int,
default=1,
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('--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 = []
label_lists = []
intent_train_examples = []
intent_dev_examples = []
intent_oos_dev_examples = []
for i in range(T):
tasks = sampled_tasks[i]
label_lists.append([])
intent_train_examples.append([])
intent_dev_examples.append([InputExample(e.text, None, e.label) for e in dev_examples])
intent_oos_dev_examples.append([InputExample(e.text, None, None) for e in oos_dev_examples])
for task in tasks:
label = task['task']
examples = task['examples']
label_lists[-1].append(label)
for j in range(len(examples)):
intent_train_examples[-1].append(InputExample(examples[j], None, label))
if args.output_dir is not None:
folder_name = '{}/{}-shot-{}/'.format(args.output_dir, N, args.bert_model)
if not os.path.exists(folder_name):
os.makedirs(folder_name)
file_name = 'batch_{}---epoch_{}---lr_{}'.format(args.train_batch_size, args.num_train_epochs, args.learning_rate)
file_name = '{}__oos-threshold'.format(file_name)
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
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):
model = Classifier(path = save_model_path,
label_list = label_lists[j],
args = args)
else:
model = Classifier(path = None,
label_list = label_lists[j],
args = args)
model.train(intent_train_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)
in_domain_preds = model.evaluate(intent_dev_examples[j])
oos_preds = model.evaluate(intent_oos_dev_examples[j])
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 __name__ == '__main__':
main()