-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathargs.py
296 lines (220 loc) · 10.3 KB
/
args.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
import argparse
# TODO: make args more comprehensible, reduce unnecessary ones and make some default to each action
def add_rnn_args(parser):
group = parser.add_argument_group('RNN Model')
group.add_argument('--hidden-size', action='store', type=int, default=1000,
help='Specify the hidden size of the model')
group.add_argument('-d', '--dropout', action='store', type=float, default=0.2,
help='Specify the dropout rate of the model')
group.add_argument('--num-layers', action='store', type=int, default=4,
help='Specify the number of GRU/LSTM layers of the model')
group.add_argument('--more-decoder-layers', action='store', type=int, default=0,
help='Specify the number more layers for decoder')
group.add_argument('--num-directions', action='store', type=int, default=1, choices=[1, 2],
help='Specify whether or not use bidirectional encoder')
group.add_argument('--rnn-type', action='store', type=str, default='GRU', choices=['GRU', 'LSTM'],
help='Specify to use GRU or LSTM')
group.add_argument('--init-rnn', action='store_true',
help='Initialize RNN to have uniform parameter between -0.1 and 0.1')
return group
def add_train_args(parser):
group = parser.add_argument_group('Training')
group.add_argument('-e', '--num-epochs', action='store', type=int, default=2,
help='Specify the number of epochs to train')
group.add_argument('--learning-rate', action='store', type=float, default=0.01,
help='Specify the learning rate')
group.add_argument('--weight-decay', action='store', type=float, default=1e-5,
help='Specify the weight decay')
group.add_argument('--eps', action='store', type=float, default=1e-6,
help='Specify eps for Adam optimizer')
group.add_argument(
'--label-smoothing',
type=float,
default=0.1,
help='The amount of label smoothing'
)
group.add_argument('--print-every', action='store', type=int, default=40,
help='Specify the number of batches to report loss')
group.add_argument('--num-evaluate', action='store', type=int, default=10,
help='Number of sentences to evaluate during training')
group.add_argument('--train-size', action='store', type=int, default=None,
help='Specify the size of data to train. If specify a small number,'
'can try to make the model converge before training on larger data.')
group.add_argument('--minibatch-size', action='store', type=int, default=128,
help='Specify the size of minibatch')
group.add_argument('--optimizer', action='store', type=str, default="Adam",
choices=["SGD", "Adadelta", "Adagrad", "RMSprop", "Adam"],
help='Specify which optimizer to use')
group.add_argument('--lr-decay', action='store', type=float, default=1,
help='Multiplicative factor of learning rate decay.')
group.add_argument('--lr-milestone', action='store', type=int, default=20,
help='Decay the learning rate at which epoch')
group.add_argument('--lr-scheduler-type', action='store', type=str, default="ExponentialLR",
choices=["ExponentialLR", "MultiStepLR", "ReduceLROnPlateau", "LambdaLR"],
help='Specify which type of lr scheduler to use')
group.add_argument('--teacher-forcing-ratio', action='store', type=float, default=0.5,
help='Teacher forcing ratio during training')
parser.add_argument('--clip', type=float, default=2.,
help='Gradient clipping')
group.add_argument(
'--eval-when-train',
action='store_false',
help='Whether or not evaluate when training'
)
group.add_argument(
'--new-lr-scheduler',
action='store_true',
help='Use a new lr scheduler'
)
group.add_argument(
'--accumulate-steps',
type=int,
default=1,
help='How many batches of data to accumulate gradients over'
)
group.add_argument(
'--reverse',
action='store_true',
help='Reverse the direction of translation. If true, translate from English; if false, translate to English'
)
return group
def add_evaluate_args(parser):
group = parser.add_argument_group('Evaluate')
group.add_argument('--evaluate-path', action='store', type=str, default="translated.txt",
help='Specify a path to store the evaluated sentences')
group.add_argument(
'--search-method',
type=str,
default='greedy',
choices=['greedy', 'beam'],
help='Use greedy search or beam search to evaluate.'
)
group.add_argument(
'--length-penalty',
type=float,
default=0.6,
help='Divides the hypothesis log probabilities in beam search by length^<length penalty>.'
)
group.add_argument(
'--beam-width',
default=4,
type=int,
help='Default beam width for beam search decoder.'
)
group.add_argument(
'--average-checkpoints',
action='store_true',
help='Average instead of using a single checkpoint'
)
group.add_argument(
'--start-epoch',
default=0,
type=int,
help='The epoch to start with when averaging checkpoints'
)
group.add_argument(
'--end-epoch',
default=1,
type=int,
help='The checkpoint to end with when averaging checkpoints'
)
group.add_argument(
'--beam-search-all',
action='store_true',
help='Search optimal of all combinations in the span instead of search sequentially.'
)
group.add_argument(
'--detokenize',
action='store_false',
help='False if do not want to detokenize the predictions'
)
return group
def add_data_args(parser):
group = parser.add_argument_group('Data')
group.add_argument('--max-length', action='store', type=int, default=51,
help='Specify the max length of a sentence')
group.add_argument('--span-size', action='store', type=int, default=3,
help='Specify the span size of the model')
group.add_argument('--seed', action='store', type=int, default=None,
help='Set seed for random scheduler')
group.add_argument('--shuffle', action='store_false',
help='Shuffle the dataloader')
group.add_argument('--trim', action='store_true',
help='Trim the dataset to max length')
group.add_argument('--dataset', action='store', type=str, default="WMT",
help='Specify which data to use')
group.add_argument(
'--batch-method',
type=str,
default='token',
choices=['token', 'example', 'random_batch'],
help='By which method to sample batches'
)
group.add_argument(
'--batch-size-buffer',
type=int,
default=0,
help='By how many tokens to reduce the batch size on the GPU of the optimizer'
)
group.add_argument(
'--drop-last',
action='store_false',
help='Whether or not to drop the last minibatch. If it is training, and using multiple GPU, then drop.'
'If it is evaluating, and using one GPU, then do not drop.'
)
group.add_argument(
'--filter',
action='store_false',
help='Whether or not filter data by length'
)
group.add_argument(
'-p',
'--preprocess-directory',
type=str,
default='/mnt/nfs/work1/miyyer/wyou/iwslt',
help='Location for the preprocessed data'
)
group.add_argument(
'--preprocess-buffer-size',
type=int,
default=12500,
help='Number of lines to preprocess at once'
)
return group
def add_cuda_args(parser):
group = parser.add_argument_group('CUDA')
group.add_argument(
'--profile-cuda-memory',
default=False,
const='cuda.prof',
nargs='?',
type=str,
help='Whether to profile CUDA memory.'
)
return group
def get_cl_args():
"""Get the command line arguments using argparse."""
arg_parser = argparse.ArgumentParser(prog="RNN-NMT-Syntax", description='Train machine translation model with RNN + Syntax')
arg_parser.add_argument('--experiment-path', action='store', type=str, default='experiments/exptest/',
help='Specify the path to store the experiment')
arg_parser.add_argument('-s', '--save-path', action='store', type=str, default='checkpoint',
help='Specify the path of checkpoint to save the stored model')
arg_parser.add_argument('--save-loss-every', action='store', type=int, default=10,
help='Save loss every x steps')
arg_parser.add_argument('--save-checkpoint-every', action='store', type=int, default=50,
help='Save checkpoint every x steps')
arg_parser.add_argument('-b', '--best-model', action='store', type=str, default='model_best.pth.tar',
help='Specify the path of checkpoint to save the best stored model')
arg_parser.add_argument('-r', '--restore', action='store', type=str, default=None,
help='Specify the path of checkpoint to load the stored model')
arg_parser.add_argument('--track', action='store_true',
help='Track this run in experiment')
arg_parser.add_argument('--mode', action='store', type=str, default="train", choices=["train", "evaluate", "evaluate_train", "test"],
help='Specify train or evaluate, if evaluate, need to load a model')
groups = {}
groups['rnn'] = add_rnn_args(arg_parser)
groups['data'] = add_data_args(arg_parser)
groups['train'] = add_train_args(arg_parser)
groups['evaluate'] = add_evaluate_args(arg_parser)
groups['cuda'] = add_cuda_args(arg_parser)
return arg_parser.parse_args()