-
Notifications
You must be signed in to change notification settings - Fork 4
/
run_MTL.py
323 lines (271 loc) · 11.1 KB
/
run_MTL.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
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
import argparse
import numpy as np
from tqdm import trange
import tensorflow as tf
import model_utils
import hparams as hps_utils
from multitask import tasks
from utils import misc_utils
from utils import training_manager
from multitask import multitask_models
from constants import (MAIN_MODEL_INDEX,
TRAIN_LOGFILE_SUFFIX,
INFER_LOGFILE_SUFFIX,
EARLY_STOP_TOLERANCE,
AUTOMR_MAX_EVAL_BATCHES)
tf.logging.set_verbosity(tf.logging.INFO)
# ==================================================
# Command Line Arguments
# ==================================================
def get_hparams():
parser = argparse.ArgumentParser()
# Training
parser.add_argument("--tasks",
type=str, default=None)
parser.add_argument("--max_steps",
type=int, default=None)
parser.add_argument("--steps_per_eval",
type=int, default=None)
parser.add_argument("--logdir",
type=str, default=None)
parser.add_argument("--ckpt_file",
type=str, default=None)
parser.add_argument("--random_seed",
type=int, default=None)
# Inference
parser.add_argument("--infer",
action="store_true", default=False)
# -----------------------------------------
# HYPER-PARAMETERS
# Model
parser.add_argument("--embedding_dim",
type=int, default=512)
parser.add_argument("--num_units",
type=int, default=512)
parser.add_argument("--num_layers",
type=int, default=2)
parser.add_argument("--dropout_rate",
type=float, default=0.5)
parser.add_argument("--learning_rate",
type=float, default=0.001)
# MTL
parser.add_argument("--model_type",
type=str, default=None)
parser.add_argument("--mixing_ratios",
type=str, default="AutoMR")
parser.add_argument("--training_strategy",
type=str, default=None)
# AutoMR
parser.add_argument("--automr_update_rate",
type=float, default=0.3)
parser.add_argument("--automr_reward_scale",
type=float, default=1.0)
parser.add_argument("--stage",
type=int, default=None)
# Specific MTL
parser.add_argument("--is_distill",
action="store_true", default=False)
parser.add_argument("--loss_coefficient_loc",
type=float, default=None)
parser.add_argument("--loss_coefficient_scale",
type=float, default=None)
parser.add_argument("--distill_temperature",
type=float, default=1.0)
# -----------------------------------------
FLAGS, unparsed = parser.parse_known_args()
if unparsed:
raise ValueError(unparsed)
return _get_hparams(FLAGS)
def _get_hparams(FLAGS):
# just being lazy
ChosenTasks = (
[tasks.problem(task) for task in FLAGS.tasks.split("-")]
if FLAGS.tasks is not None else None)
MainTask = ChosenTasks[MAIN_MODEL_INDEX]
isAuto = (FLAGS.mixing_ratios.startswith("Auto")
if FLAGS.mixing_ratios is not None else False)
_logdir = (
FLAGS.logdir if not FLAGS.logdir.endswith("/")
else FLAGS.logdir[:-1])
train_logfile = _logdir + TRAIN_LOGFILE_SUFFIX
infer_logfile = _logdir + INFER_LOGFILE_SUFFIX
print("\t\tRunning %d" % FLAGS.stage)
if FLAGS.stage == 2:
raise ValueError(
"To run the stage-2, please follow the instructions"
"on https://scikit-optimize.github.io. Other existing "
"alternatives include: "
"`https://github.com/fmfn/BayesianOptimization` "
"`https://github.com/HIPS/Spearmint`.")
"""A set of basic hyperparameters."""
return tf.contrib.training.HParams(
# Tasks and data files
# ---------------------------------
tasks=ChosenTasks,
task_names=[t.name for t in ChosenTasks],
train_files=[t.train_data for t in ChosenTasks],
eval_files=([t.val_data for t in ChosenTasks]
if not FLAGS.infer else
[t.test_data for t in ChosenTasks]),
# Batch sizes
# ---------------------------------
# just using the main task info
train_batch_size=MainTask.train_batch_size,
eval_batch_size=MainTask.evaluate_batch_size,
# Steps
# ---------------------------------
max_steps=(
FLAGS.max_steps
if FLAGS.max_steps is not None
else MainTask.max_steps),
steps_per_eval=(
FLAGS.steps_per_eval
if FLAGS.steps_per_eval is not None
else MainTask.steps_per_eval),
# Training
# ---------------------------------
logdir=FLAGS.logdir,
manager_logdir=FLAGS.logdir,
ckpt_file=FLAGS.ckpt_file, # initialize model, or run test
numpy_seed=FLAGS.random_seed,
tensorflow_seed=FLAGS.random_seed,
train_logfile=train_logfile,
# Inference
# ---------------------------------
infer=FLAGS.infer,
infer_logfile=infer_logfile,
# Misc
# ---------------------------------
eval_model_index=0,
# Hyper-parameters
# ---------------------------------
embedding_dim=FLAGS.embedding_dim,
num_units=FLAGS.num_units,
num_layers=FLAGS.num_layers,
dropout_rate=FLAGS.dropout_rate,
learning_rate=FLAGS.learning_rate,
# Multi-Task
# ---------------------------------
training_strategy=FLAGS.training_strategy,
embedding_type=FLAGS.model_type.split("-")[0],
base_model_type=FLAGS.model_type.split("-")[1],
multitask_model_type=FLAGS.model_type.split("-")[2],
auto_model_type=(FLAGS.mixing_ratios if isAuto else None),
mixing_ratios=(
[int(r) for r in FLAGS.mixing_ratios.split("-")]
if FLAGS.mixing_ratios is not None and not isAuto else None),
# Multi-Task Hyperparams
# ---------------------------------
automr_update_rate=FLAGS.automr_update_rate,
automr_reward_scale=FLAGS.automr_reward_scale,
is_distill=FLAGS.is_distill,
loss_coefficient_loc=FLAGS.loss_coefficient_loc,
loss_coefficient_scale=FLAGS.loss_coefficient_scale,
distill_temperature=FLAGS.distill_temperature
)
def trainMTL(hparams):
# Build Models and Data
# ------------------------------------------
# with misc_utils.suppress_stdout():
train_MTL_model, val_MTL_model = model_utils.build_model(hparams)
# building training monitor
# ------------------------------------------
# early stop on the **target** task
eval_task = hparams.tasks[hparams.eval_model_index]
manager = training_manager.TrainingManager(
name=eval_task.name,
logdir=hparams.manager_logdir,
stopping_fn=eval_task.manager_stopping_fn(
tolerance=EARLY_STOP_TOLERANCE),
updating_fn=eval_task.manager_updating_fn(),
load_when_possible=False)
scores_dict = _train(
hparams=hparams,
manager=manager,
train_MTL_model=train_MTL_model,
val_MTL_model=val_MTL_model)
# log the results for easier inspectation
with open(hparams.train_logfile, "a") as f:
for tag, score in scores_dict.items():
f.write("%s: %.3f\t" % (tag, score))
f.write("\n")
print("FINISHED")
def _train(hparams, manager, train_MTL_model, val_MTL_model):
# initialize *all* data generator
# ------------------------------------------
train_MTL_model.initialize_or_restore_session(
ckpt_file=hparams.ckpt_file,
var_filter_fn=lambda name: "Adam" not in name and "clone" not in name)
train_MTL_model.initialize_data_iterator(model_idx=None)
# TRAIN
# ------------------------------------------
pbar = trange(hparams.max_steps)
for _ in pbar:
try:
_, message = train_MTL_model.train()
pbar.set_description(message)
except tf.errors.OutOfRangeError:
raise ValueError("Task Finished An Epoch, this should not happen")
# Evaluate the model
# ------------------------------------------
if train_MTL_model.global_step % hparams.steps_per_eval == 0:
with misc_utils.suppress_stdout():
ckpt = train_MTL_model.save_session()
tf.logging.info("Running Evaluation")
val_MTL_model.initialize_or_restore_session(
var_filter_fn=lambda name: "Adam" not in name)
val_MTL_model.initialize_data_iterator(
[hparams.eval_model_index])
scores_dict = val_MTL_model.evaluate(
model_idx=hparams.eval_model_index,
max_eval_batches=AUTOMR_MAX_EVAL_BATCHES)
if multitask_models.is_AutoMR(train_MTL_model):
train_MTL_model.update_TaskSelector(scores_dict["MAIN"])
# Log the best ckpt, which will be saved in a
# different directory. Note that when manager.should_update
# returns False, the manager.update will not do anything anyway
if manager.should_update({"Scores": scores_dict["MAIN"]}):
ckpt = train_MTL_model.save_best_session()
manager.update(value={"Scores": scores_dict["MAIN"]},
ckpt=ckpt, verbose=True)
manager.save()
if manager.should_stop:
print("Manager has given the order to stop")
pbar.close()
break
return manager.best_value
def infer(hparams):
# Build Models and Data
# ------------------------------------------
_, infer_model = model_utils.build_model(hparams)
manager = training_manager.TrainingManager(
name=hparams.task_names[MAIN_MODEL_INDEX],
logdir=hparams.manager_logdir)
if hparams.ckpt_file is not None:
ckpt_file = hparams.ckpt_file
print("Using Specified CKPT from %s" % ckpt_file)
else:
ckpt_file = manager.best_checkpoint
print("Using Manager CKPT from %s" % ckpt_file)
if ckpt_file is None:
raise ValueError("`ckpt_file` is None")
tf.logging.info("Running Evaluation")
infer_model.initialize_or_restore_session(
ckpt_file=ckpt_file,
var_filter_fn=lambda name: "Adam" not in name)
infer_model.initialize_data_iterator()
infer_model.inference(model_idx=MAIN_MODEL_INDEX)
def main(unused_argv):
hparams = get_hparams()
# set seed
np.random.seed(hparams.numpy_seed)
tf.set_random_seed(hparams.tensorflow_seed)
if hparams.infer:
infer(hparams)
else:
trainMTL(hparams)
if __name__ == "__main__":
tf.app.run()