-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactive_learning.py
319 lines (253 loc) · 11.4 KB
/
active_learning.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
import argparse
import os
from model.al_manager import get_al_manager
from utils.graphs import active_learning_graph
from utils.pickle import save
DEFAULT_BATCH_SIZE = 32
DEFAULT_PERFORM_SHUFFLE = True
DEFAULT_NUM_EPOCHS = 10000
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
def bool_arguments(value):
return True if int(value) == 1 else False
def create_argument_parser():
parser = argparse.ArgumentParser()
parser.add_argument('-tr',
'--train-file',
type=str,
help='The location of the train file',
required=True)
parser.add_argument('-ts',
'--test-file',
type=str,
help='The location of the test file',
required=True)
parser.add_argument('-sm',
'--saved-model-folder',
type=str,
help='Location to search/save models. The model name variable will be used for searching') # noqa
parser.add_argument('-nt',
'--num-train',
type=int,
help='Number of training examples')
parser.add_argument('-nte',
'--num-test',
type=int,
help='Number of test examples')
parser.add_argument('-uv',
'--use-validation',
type=bool_arguments,
help='If the model should provide accuracy measurements using validation set') # noqa
parser.add_argument('-gd',
'--graphs-dir',
type=str,
help='The location of the graphs dir')
parser.add_argument('-mn',
'--model-name',
type=str,
help='The model name that will be used to save tensorboard information')
parser.add_argument('-td',
'--tensorboard-dir',
type=str,
help='Directory to save tensorboard information')
parser.add_argument('-ef',
'--embedding-file',
type=str,
help='The path of the embedding file')
parser.add_argument('-ekl',
'--embedding-pickle',
type=str,
help='The path of embedding matrix pickle file')
parser.add_argument('-lr',
'--learning-rate',
type=float,
help='The learning rate to use during training')
parser.add_argument('-bs',
'--batch-size',
type=int,
default=DEFAULT_BATCH_SIZE,
help='The batch size used for stochastic gradient descent')
parser.add_argument('-np',
'--num-epochs',
type=int,
default=DEFAULT_NUM_EPOCHS,
help='Number of epochs to train the model')
parser.add_argument('-ps',
'--perform-shuffle',
type=bool_arguments,
default=DEFAULT_PERFORM_SHUFFLE,
help='If the dataset should be shuffled before using it')
parser.add_argument('-es',
'--embed-size',
type=int,
help='The embedding size of the embedding matrix')
parser.add_argument('-nu',
'--num-units',
type=int,
help='The number of hidden units in the Recurrent layer')
parser.add_argument('-nc',
'--num-classes',
type=int,
help='The number of classification classes')
parser.add_argument('-lid',
'--recurrent-input-dropout',
type=float,
help='Dropout value for inputs in the network')
parser.add_argument('-lod',
'--recurrent-output-dropout',
type=float,
help='Dropout value for Recurrent output')
parser.add_argument('-lsd',
'--recurrent-state-dropout',
type=float,
help='Dropout value for recurrent state (variational dropout)')
parser.add_argument('-ed',
'--embedding-dropout',
type=float,
help='Dropout value for embedding layer')
parser.add_argument('-cp',
'--clip-gradients',
type=bool_arguments,
help='If gradient clipping should be performed')
parser.add_argument('-mxn',
'--max-norm',
type=int,
help='The max norm to clip the gradients, if --clip-gradients=True')
parser.add_argument('-wd',
'--weight-decay',
type=float,
help='Weight Decay value for L2 regularizer')
parser.add_argument('-bw',
'--bucket-width',
type=int,
help='The width use to define a bucket id for a given movie review')
parser.add_argument('-nb',
'--num-buckets',
type=int,
help='The maximum number of buckets allowed')
parser.add_argument('-ut',
'--use-test',
type=bool_arguments,
help='Define if the model should check accuracy on test dataset')
parser.add_argument('-sg',
'--save-graph',
type=bool_arguments,
help='Define if an accuracy graph should be saved')
parser.add_argument('-alt',
'--active-learning-type',
type=str,
help='Define the type of active learning technique to use')
parser.add_argument('-um',
'--uncertainty-metric',
type=str,
help='Type of uncertainty metric to use')
parser.add_argument('-nr',
'--num-rounds',
type=int,
help='Number of Active Learning cycles to run')
parser.add_argument('-sz',
'--sample-size',
type=int,
help='The size of samples to evaluate in the unlabeled dataset')
parser.add_argument('-nq',
'--num-queries',
type=int,
help='The number of data to be extracted from the unlabeled sample')
parser.add_argument('-nfp',
'--num-passes',
type=int,
help='Number of forward passes for Monte Carlo Dropout')
parser.add_argument('-its',
'--initial-training-size',
type=int,
help='Initial size of the training set')
parser.add_argument('-ml',
'--max-len',
type=int,
help='The maximum sentece size allowed')
parser.add_argument('-sgp',
'--save-graph-path',
type=str,
help='Save graph path')
parser.add_argument('-sdf',
'--save-data-folder',
type=str,
help='Location to save the data generated by the script')
parser.add_argument('-tdna',
'--train-data-name',
type=str,
help='Name to save the train data array file')
parser.add_argument('-tdn',
'--test-acc-name',
type=str,
help='Name to save the test accuracy array file')
return parser
def run_active_learning(**user_args):
active_learning_params = {
'train_file': user_args['train_file'],
'test_file': user_args['test_file'],
'uncertainty_metric': user_args['uncertainty_metric'],
'num_rounds': user_args['num_rounds'],
'sample_size': user_args['sample_size'],
'num_queries': user_args['num_queries'],
'num_passes': user_args['num_passes'],
'max_len': user_args['max_len'],
'train_initial_size': user_args['initial_training_size']
}
model_params = {
'embedding_file': user_args['embedding_file'],
'embed_size': user_args['embed_size'],
'embedding_pickle': user_args['embedding_pickle'],
'saved_model_folder': user_args['saved_model_folder'],
'should_save': False,
'perform_shuffle': user_args['perform_shuffle'],
'model_name': user_args['model_name'],
'tensorboard_dir': user_args['tensorboard_dir'],
'graphs_dir': user_args['graphs_dir'],
'save_graph': user_args['save_graph'],
'learning_rate': user_args['learning_rate'],
'batch_size': user_args['batch_size'],
'num_epochs': user_args['num_epochs'],
'num_classes': user_args['num_classes'],
'num_train': user_args['num_train'],
'num_test': user_args['num_test'],
'use_test': user_args['use_test'],
'use_validation': user_args['use_validation'],
'use_mc_dropout': False,
'num_units': user_args['num_units'],
'recurrent_input_dropout': user_args['recurrent_input_dropout'],
'recurrent_output_dropout': user_args['recurrent_output_dropout'],
'recurrent_state_dropout': user_args['recurrent_state_dropout'],
'embedding_dropout': user_args['embedding_dropout'],
'weight_decay': user_args['weight_decay'],
'clip_gradients': user_args['clip_gradients'],
'max_norm': user_args['max_norm'],
'max_len': user_args['max_len'],
'bucket_width': user_args['bucket_width'],
'num_buckets': user_args['num_buckets']
}
al_type = user_args['active_learning_type']
al_technique = get_al_manager(al_type)
al_model_manager = al_technique(
model_params, active_learning_params, verbose=False)
uncertainty_metric = user_args['uncertainty_metric']
uncertainty_metric = uncertainty_metric.replace('_', ' ').title()
print('Running {} Active Learning with {} metric'.format(
al_type.title(), uncertainty_metric))
train_data, test_accuracies = al_model_manager.run_cycle()
save_graph_path = user_args['save_graph_path']
active_learning_graph(train_data, test_accuracies, save_graph_path)
save_data_folder = user_args['save_data_folder']
if not os.path.exists(save_data_folder):
os.makedirs(save_data_folder)
train_data_name = user_args['train_data_name']
save_data_train = os.path.join(save_data_folder, train_data_name)
save(train_data, save_data_train)
test_acc_name = user_args['test_acc_name']
save_data_test = os.path.join(save_data_folder, test_acc_name)
save(test_accuracies, save_data_test)
def main():
parser = create_argument_parser()
user_args = vars(parser.parse_args())
return run_active_learning(**user_args)
if __name__ == '__main__':
main()