-
Notifications
You must be signed in to change notification settings - Fork 20
/
opts.py
192 lines (173 loc) · 6.46 KB
/
opts.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
"""Module for setting the configuration of the experiment.
This class contains functions to parse the input options set by the user.
It also contains logging, outputs, and model saving information.
Copyright (c) 2021, Shruti Rijhwani
All rights reserved.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree.
"""
import argparse
import logging
import os
class SetConfig:
def __init__(self, sys_args):
self.args = self.parse_config(sys_args)
def parse_config(self, sys_args):
parser = argparse.ArgumentParser()
# model params
parser.add_argument(
"--single",
action="store_true",
help="Enables single-source model. Default is multisource.",
)
parser.add_argument(
"--beam_size",
type=int,
default=4,
help="Beam size for generating outputs with beam search.",
)
parser.add_argument(
"--pointer_gen",
action="store_true",
help="Enables copy mechanism (recommended).",
)
parser.add_argument(
"--coverage",
action="store_true",
help="Enables coverage mechanism and coverage loss (recommended).",
)
parser.add_argument(
"--diag_loss",
type=int,
default=-1,
help="Enables diagonal attention loss (recommended).",
)
parser.add_argument(
"--load_model",
default=None,
help="Saved model to load before training or testing.",
)
parser.add_argument(
"--vocab_folder",
help="Location of stored character vocabularies to use with the model.",
)
# experiment params
parser.add_argument(
"--pretrain_only",
action="store_true",
help="Enables pretraining only. Useful when using the same pretrained model for many experiments.",
)
parser.add_argument(
"--train_only",
action="store_true",
help="Enables training only. Useful when setting up experiments with different combinations of datasets and/or pretrained models.",
)
parser.add_argument(
"--testing",
action="store_true",
help="Enables testing with a trained model.",
)
parser.add_argument("--model_name", help="Model name for saving to disk.")
# pretrain params
parser.add_argument(
"--pretrain_src1",
help="Source 1 for pretraining (endangered language first pass OCR).",
)
parser.add_argument(
"--pretrain_src2",
help="Source 2 for pretraining (translation first pass OCR). Do not use for single-source model.",
)
parser.add_argument(
"--pretrain_tgt", help="Denoised target outputs for pretraining."
)
parser.add_argument(
"--pretrain_enc",
action="store_true",
help="Enables pretraining the encoders.",
)
parser.add_argument(
"--pretrain_dec",
action="store_true",
help="Enables pretraining the decoders.",
)
parser.add_argument(
"--pretrain_s2s",
action="store_true",
help="Enables pretraining the seq2seq model.",
)
parser.add_argument(
"--pretrain_epochs",
type=int,
default=10,
help="Number of epochs for pretraining.",
)
# parallel files for training -- one sentence or paragraph per line
parser.add_argument(
"--train_src1",
help="Source 1 for training (endangered language first pass OCR).",
)
parser.add_argument(
"--train_src2",
help="Source 2 for training (translation first pass OCR). Do not use for single-source model.",
)
parser.add_argument(
"--train_tgt", help="Manually corrected target transcriptions for training."
)
parser.add_argument("--dev_src1", help="Development set source 1.")
parser.add_argument(
"--dev_src2",
help="Development set source 2. Do not use for single-source model.",
)
parser.add_argument(
"--dev_tgt",
help="Development set manually corrected target transcriptions.",
)
# test params
parser.add_argument("--test_src1", help="Test input source 1.")
parser.add_argument(
"--test_src2",
help="Test input source 2. Do not use for single-source model.",
)
parser.add_argument(
"--test_tgt",
help="Optional target transcriptions for testing. Use only when targets are available and CER/WER metrics are desired.",
)
# dynet params
parser.add_argument(
"--dynet-autobatch", help="Enables automatic minibatching with dynet."
)
parser.add_argument("--dynet-mem", help="Memory available to dynet.")
parser.add_argument(
"--dynet-gpu", action="store_true", help="Enables GPU use with dynet."
)
# experiment output folder
parser.add_argument(
"--output_folder",
help="Output folder for experiment logs, output files, and saved models.",
)
args = parser.parse_args(sys_args)
output_folder = args.output_folder
if args.pretrain_only:
filename = args.model_name
log_filename = output_folder + "pretrain_logs/" + filename + ".log"
self.model_name = output_folder + "pretrain_models/" + filename
self.output_name = None
elif args.testing:
log_filename = None
self.output_name = (
output_folder + "outputs/" + args.load_model.split("/")[-1]
)
self.model_name = None
else:
filename = args.model_name
log_filename = output_folder + "train_logs/" + filename + ".log"
self.model_name = output_folder + "models/" + filename
self.output_name = output_folder + "debug_outputs/" + filename
if log_filename:
logging.basicConfig(
filename=log_filename,
level=logging.INFO,
format="%(message)s",
filemode="w",
)
return args