This repository has been archived by the owner on Dec 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathoptions.py
258 lines (246 loc) · 7.76 KB
/
options.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
"""Script to read command line flags using ArgParser.
Author(s): Satwik Kottur
"""
from __future__ import absolute_import, division, print_function, unicode_literals
import argparse
import torch
from tools import support
def read_command_line():
"""Read and parse commandline arguments to run the program.
Returns:
parsed_args: Dictionary of parsed arguments.
"""
title = "Train assistant model for furniture genie"
parser = argparse.ArgumentParser(description=title)
# Data input settings.
parser.add_argument(
"--train_data_path", required=True, help="Path to compiled training data"
)
parser.add_argument(
"--eval_data_path", default=None, help="Path to compiled evaluation data"
)
parser.add_argument(
"--snapshot_path", default="checkpoints/", help="Path to save checkpoints"
)
parser.add_argument(
"--metainfo_path",
default="data/furniture_metainfo.json",
help="Path to file containing metainfo",
)
parser.add_argument(
"--attr_vocab_path",
default="data/attr_vocab_file.json",
help="Path to attribute vocabulary file",
)
parser.add_argument(
"--domain",
required=True,
choices=["furniture", "fashion"],
help="Domain to train the model on",
)
# Asset embedding.
parser.add_argument(
"--asset_embed_path",
default="data/furniture_asset_path.npy",
help="Path to asset embeddings",
)
# Specify encoder/decoder flags.
# Model hyperparameters.
parser.add_argument(
"--encoder",
required=True,
choices=[
"history_agnostic",
"history_aware",
"pretrained_transformer",
"hierarchical_recurrent",
"memory_network",
"tf_idf",
],
help="Encoder type to use for text",
)
parser.add_argument(
"--text_encoder",
required=True,
choices=["lstm", "transformer"],
help="Encoder type to use for text",
)
parser.add_argument(
"--word_embed_size", default=128, type=int, help="size of embedding for text"
)
parser.add_argument(
"--hidden_size",
default=128,
type=int,
help=(
"Size of hidden state in LSTM/transformer."
"Must be same as word_embed_size for transformer"
),
)
# Parameters for transformer text encoder.
parser.add_argument(
"--num_heads_transformer",
default=-1,
type=int,
help="Number of heads in the transformer",
)
parser.add_argument(
"--num_layers_transformer",
default=-1,
type=int,
help="Number of layers in the transformer",
)
parser.add_argument(
"--hidden_size_transformer",
default=2048,
type=int,
help="Hidden Size within transformer",
)
parser.add_argument(
"--num_layers", default=1, type=int, help="Number of layers in LSTM"
)
parser.add_argument(
"--use_action_attention",
dest="use_action_attention",
action="store_true",
default=False,
help="Use attention over all encoder statesfor action",
)
parser.add_argument(
"--use_action_output",
dest="use_action_output",
action="store_true",
default=False,
help="Model output of actions as decoder memory elements",
)
parser.add_argument(
"--use_multimodal_state",
dest="use_multimodal_state",
action="store_true",
default=False,
help="Use multimodal state for action prediction (fashion)",
)
parser.add_argument(
"--use_bahdanau_attention",
dest="use_bahdanau_attention",
action="store_true",
default=False,
help="Use bahdanau attention for decoder LSTM",
)
parser.add_argument(
"--skip_retrieval_evaluation",
dest="retrieval_evaluation",
action="store_false",
default=True,
help="Evaluation response generation through retrieval"
)
parser.add_argument(
"--skip_bleu_evaluation",
dest="bleu_evaluation",
action="store_false",
default=True,
help="Use beamsearch to evaluate BLEU score"
)
parser.add_argument(
"--max_encoder_len",
default=24,
type=int,
help="Maximum encoding length for sentences",
)
parser.add_argument(
"--max_history_len",
default=100,
type=int,
help="Maximum encoding length for history encoding",
)
parser.add_argument(
"--max_decoder_len",
default=26,
type=int,
help="Maximum decoding length for sentences",
)
parser.add_argument(
"--max_rounds",
default=30,
type=int,
help="Maximum number of rounds for the dialog",
)
parser.add_argument(
"--share_embeddings",
dest="share_embeddings",
action="store_true",
default=True,
help="Encoder/decoder share emebddings",
)
# Optimization hyperparameters.
parser.add_argument(
"--batch_size",
default=30,
type=int,
help="Training batch size (adjust based on GPU memory)",
)
parser.add_argument(
"--learning_rate", default=1e-3, type=float, help="Learning rate for training"
)
parser.add_argument("--dropout", default=0.2, type=float, help="Dropout")
parser.add_argument(
"--num_epochs",
default=20,
type=int,
help="Maximum number of epochs to run training",
)
parser.add_argument(
"--eval_every_epoch",
default=1,
type=int,
help="Number of epochs to evaluate every",
)
parser.add_argument(
"--save_every_epoch",
default=-1,
type=int,
help="Epochs to save the model every, -1 does not save",
)
parser.add_argument(
"--save_prudently",
dest="save_prudently",
action="store_true",
default=False,
help="Save checkpoints prudently (only best models)",
)
parser.add_argument(
"--gpu_id", type=int, default=-1, help="GPU id to use, -1 for CPU"
)
try:
parsed_args = vars(parser.parse_args())
except (IOError) as msg:
parser.error(str(msg))
# For transformers, hidden size must be same as word_embed_size.
if parsed_args["text_encoder"] == "transformer":
assert (
parsed_args["word_embed_size"] == parsed_args["hidden_size"]
), "hidden_size should be same as word_embed_size for transformer"
if not parsed_args["use_bahdanau_attention"]:
print("Bahdanau attention must be off!")
parsed_args["use_bahdanau_attention"] = False
# If action output is to be used for LSTM, bahdahnau attention must be on.
if parsed_args["use_action_output"] and parsed_args["text_encoder"] == "lstm":
assert parsed_args["use_bahdanau_attention"], (
"Bahdanau attention " "must be on for action output to be used!"
)
# For tf_idf, ignore the action_output flag.
if parsed_args["encoder"] == "tf_idf":
parsed_args["use_action_output"] = False
# Prudent save is not possible without evaluation.
if parsed_args["save_prudently"]:
assert parsed_args[
"eval_data_path"
], "Prudent save needs a non-empty eval_data_path"
# Set the cuda environment variable for the gpu to use and get context.
parsed_args["use_gpu"] = support.setup_cuda_environment(parsed_args["gpu_id"])
# Force cuda initialization
# (otherwise results in weird race conditions in PyTorch 1.4).
if parsed_args["use_gpu"]:
_ = torch.Tensor([1.0]).cuda()
support.pretty_print_dict(parsed_args)
return parsed_args