forked from strangetom/ingredient-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
356 lines (341 loc) · 10.7 KB
/
train.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
#!/usr/bin/env python3
import argparse
import json
import os
from random import randint
from train import (
check_label_consistency,
feature_search,
grid_search,
train_multiple,
train_single,
)
class ParseJsonArg(argparse.Action):
"""Custom argparse.Action to parse JSON argument into dict."""
def __init__(self, option_strings, dest, **kwargs):
super().__init__(option_strings, dest, **kwargs)
def __call__(self, parser, namespace, values, option_strings):
setattr(namespace, self.dest, json.loads(values))
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Train a CRF model to parse label token from recipe \
ingredient sentences."
)
subparsers = parser.add_subparsers(dest="command", help="Training commands")
train_parser = subparsers.add_parser("train", help="Train CRF model.")
train_parser.add_argument(
"--database",
help="Path to database of training data",
type=str,
dest="database",
required=True,
)
train_parser.add_argument(
"--database-table",
help="Name of table in database containing training data",
type=str,
dest="table",
default="en",
)
train_parser.add_argument(
"--datasets",
help="Datasets to use in training and evaluating the model",
dest="datasets",
nargs="*",
default=["bbc", "cookstr", "nyt"],
)
train_parser.add_argument(
"--split",
default=0.20,
type=float,
help="Fraction of data to be used for testing",
)
train_parser.add_argument(
"--save-model",
default="ingredient_parser/en/model.en.crfsuite",
help="Path to save model to",
)
train_parser.add_argument(
"--seed",
default=None,
type=int,
help="Seed value used for train/test split.",
)
train_parser.add_argument(
"--html",
action="store_true",
help="Output a markdown file containing detailed results.",
)
train_parser.add_argument(
"--detailed",
action="store_true",
help="Output a file containing detailed results about accuracy.",
)
train_parser.add_argument(
"--confusion",
action="store_true",
help="Plot confusion matrix of token labels.",
)
multiple_parser_help = "Average CRF performance across multiple training cycles."
multiple_parser = subparsers.add_parser("multiple", help=multiple_parser_help)
multiple_parser.add_argument(
"--database",
help="Path to database of training data",
type=str,
dest="database",
required=True,
)
multiple_parser.add_argument(
"--database-table",
help="Name of table in database containing training data",
type=str,
dest="table",
default="en",
)
multiple_parser.add_argument(
"--datasets",
help="Datasets to use in training and evaluating the model",
dest="datasets",
nargs="*",
default=["bbc", "cookstr", "nyt"],
)
multiple_parser.add_argument(
"--split",
default=0.20,
type=float,
help="Fraction of data to be used for testing",
)
multiple_parser.add_argument(
"--save-model",
default="ingredient_parser/en/model.en.crfsuite",
help="Path to save model to",
)
multiple_parser.add_argument(
"--html",
action="store_true",
help="Output a markdown file containing detailed results.",
)
multiple_parser.add_argument(
"--detailed",
action="store_true",
help="Output a file containing detailed results about accuracy.",
)
multiple_parser.add_argument(
"--confusion",
action="store_true",
help="Plot confusion matrix of token labels.",
)
multiple_parser.add_argument(
"-r",
"--runs",
default=10,
type=int,
help="Number of times to run the training and evaluation of the model.",
)
multiple_parser.add_argument(
"-p",
"--processes",
default=os.cpu_count() - 1,
type=int,
help="Number of processes to spawn. Default to number of cpu cores.",
)
gridsearch_parser_help = (
"Grid search over all combinations of model hyperparameters."
)
gridsearch_parser = subparsers.add_parser("gridsearch", help=multiple_parser_help)
gridsearch_parser.add_argument(
"--database",
help="Path to database of training data",
type=str,
dest="database",
required=True,
)
gridsearch_parser.add_argument(
"--database-table",
help="Name of table in database containing training data",
type=str,
dest="table",
default="en",
)
gridsearch_parser.add_argument(
"--datasets",
help="Datasets to use in training and evaluating the model",
dest="datasets",
nargs="*",
default=["bbc", "cookstr", "nyt"],
)
gridsearch_parser.add_argument(
"--split",
default=0.20,
type=float,
help="Fraction of data to be used for testing",
)
gridsearch_parser.add_argument(
"--save-model",
default="ingredient_parser/en/model.en.crfsuite",
help="Path to save model to",
)
gridsearch_parser.add_argument(
"--keep-models",
action="store_true",
default=False,
help="Keep models after evaluation instead of deleting.",
)
gridsearch_parser.add_argument(
"-p",
"--processes",
default=os.cpu_count() - 1,
type=int,
help="Number of processes to spawn. Default to number of cpu cores.",
)
gridsearch_parser.add_argument(
"--seed",
default=randint(0, 1_000_000_000),
type=int,
help="Seed value used for train/test split.",
)
gridsearch_parser.add_argument(
"--algos",
default=["lbfgs"],
choices=["lbfgs", "ap", "l2sgd", "pa", "arow"],
nargs="+",
help="CRF training algorithms to use.",
)
gridsearch_parser.add_argument(
"--lbfgs-params",
help="""LBFGS algorithm parameters as JSON.
The values for each parameter should be a list.
Any parameters not given will take their default value.""",
action=ParseJsonArg,
)
gridsearch_parser.add_argument(
"--ap-params",
help="""AP algorithm parameters as JSON.
The values for each parameter should be a list.
Any parameters not given will take their default value.""",
action=ParseJsonArg,
)
gridsearch_parser.add_argument(
"--l2sgd-params",
help="""L2GSD algorithm parameters as JSON.
The values for each parameter should be a list.
Any parameters not given will take their default value.""",
action=ParseJsonArg,
)
gridsearch_parser.add_argument(
"--pa-params",
help="""PA algorithm parameters as JSON.
The values for each parameter should be a list.
Any parameters not given will take their default value.""",
action=ParseJsonArg,
)
gridsearch_parser.add_argument(
"--arow-params",
help="""AROW algorithm parameters as JSON.
The values for each parameter should be a list.
Any parameters not given will take their default value.""",
action=ParseJsonArg,
)
gridsearch_parser.add_argument(
"--global-params",
help="""Global algorithm parameters, applicable to all algorithms, as JSON.
The values for each parameter should be a list.
Any parameters not given will take their default value.""",
action=ParseJsonArg,
default=dict(),
)
featuresearch_parser_help = "Grid search over all sets of model features."
featuresearch_parser = subparsers.add_parser(
"featuresearch", help=featuresearch_parser_help
)
featuresearch_parser.add_argument(
"--database",
help="Path to database of training data",
type=str,
dest="database",
required=True,
)
featuresearch_parser.add_argument(
"--database-table",
help="Name of table in database containing training data",
type=str,
dest="table",
default="en",
)
featuresearch_parser.add_argument(
"--datasets",
help="Datasets to use in training and evaluating the model",
dest="datasets",
nargs="*",
default=["bbc", "cookstr", "nyt"],
)
featuresearch_parser.add_argument(
"--split",
default=0.20,
type=float,
help="Fraction of data to be used for testing",
)
featuresearch_parser.add_argument(
"--save-model",
default="ingredient_parser/en/model.en.crfsuite",
help="Path to save model to",
)
featuresearch_parser.add_argument(
"--keep-models",
action="store_true",
default=False,
help="Keep models after evaluation instead of deleting.",
)
featuresearch_parser.add_argument(
"-p",
"--processes",
default=os.cpu_count() - 1,
type=int,
help="Number of processes to spawn. Default to number of cpu cores.",
)
featuresearch_parser.add_argument(
"--seed",
default=randint(0, 1_000_000_000),
type=int,
help="Seed value used for train/test split.",
)
utility_help = "Utilities to aid cleaning training data."
utility_parser = subparsers.add_parser("utility", help=utility_help)
utility_parser.add_argument(
"utility",
choices=["consistency"],
help="Cleaning utility to execute",
)
utility_parser.add_argument(
"--database",
help="Path to database of training data",
type=str,
dest="database",
required=True,
)
utility_parser.add_argument(
"--database-table",
help="Name of table in database containing training data",
type=str,
dest="table",
default="en",
)
utility_parser.add_argument(
"--datasets",
help="Datasets to use in training and evaluating the model",
dest="datasets",
nargs="*",
default=["bbc", "cookstr", "nyt"],
)
args = parser.parse_args()
if args.command == "train":
train_single(args)
elif args.command == "multiple":
train_multiple(args)
elif args.command == "gridsearch":
grid_search(args)
elif args.command == "featuresearch":
feature_search(args)
elif args.command == "utility":
if args.utility == "consistency":
check_label_consistency(args)