forked from lyeskhalil/CORL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.py
346 lines (289 loc) · 10 KB
/
pipeline.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
import os
import subprocess
# Refer to opts.py for details about the flags
# graph/dataset flags
model_type = "inv-ff-hist"
problem = "adwords"
graph_family = "triangular"
weight_distribution = "triangular"
weight_distribution_param = "0.1 0.4" # seperate by a space
graph_family_parameters = "-1"
u_size = 10
v_size = 100
dataset_size = 1
val_size = 1
eval_size = 2000
# add this to the flags for adwords only.
# capacity_params='0 1'
extention = "/{}_{}_{}_{}_{}by{}".format(
problem,
graph_family,
weight_distribution,
weight_distribution_param,
u_size,
v_size,
).replace(" ", "")
train_dataset = "dataset/train" + extention
val_dataset = "dataset/val" + extention
eval_dataset = "dataset/eval" + extention
save_eval_data = True
# model flags
batch_size = 100
eval_batch_size = 100
embedding_dim = 30 # 60
n_heads = 1 # 3
n_epochs = 20
checkpoint_epochs = 0
eval_baselines = "greedy"
if problem == "e-obm":
eval_baselines += " greedy-rt greedy-t"
if problem == "adwords":
eval_baselines += " msvv"
lr_model = 0.006
lr_decay = 0.97
beta_decay = 0.8
ent_rate = 0.0006
n_encode_layers = 1
baseline = "exponential"
# directory io flags
output_dir = "saved_models"
log_dir = "logs_dataset"
# model evaluation flags
eval_models = "ff ff-hist inv-ff inv-ff-hist"
eval_output = "figures"
# this is a single checkpoint. Example: outputs_dataset/e-obm_20/run_20201226T171156/epoch-4.pt
load_path = None
test_transfer = False
def get_latest_model(
m_type,
u_size,
v_size,
problem,
graph_family,
weight_dist,
w_dist_param,
g_fams,
eval_models,
):
if m_type not in eval_models:
return "None"
m, v = w_dist_param.split(" ")
models = ""
if graph_family == "gmission-perm":
graph_family = "gmission"
for g_fam_param in g_fams.split(" "):
dir = f"outputs/output_{problem}_{graph_family}_{u_size}by{v_size}_p={g_fam_param}_{graph_family}_m={m}_v={v}_a=3"
list_of_files = sorted(
os.listdir(dir + f"/{m_type}"), key=lambda s: int(s[4:12] + s[13:])
)
if models != "":
models += " " + dir + f"/{m_type}/{list_of_files[-1]}/best-model.pt"
else:
models += dir + f"/{m_type}/{list_of_files[-1]}/best-model.pt"
return models
arg = [
u_size,
v_size,
problem,
graph_family,
weight_distribution,
weight_distribution_param,
graph_family_parameters,
eval_models.split(" "),
]
attention_models = get_latest_model("attention", *arg)
ff_supervised_models = get_latest_model("ff-supervised", *arg)
gnn_hist_models = get_latest_model("gnn-hist", *arg)
gnn_models = get_latest_model("gnn", *arg)
gnn_simp_hist_models = get_latest_model("gnn-simp-hist", *arg)
inv_ff_models = get_latest_model("inv-ff", *arg)
inv_ff_hist_models = get_latest_model("inv-ff-hist", *arg)
ff_models = get_latest_model("ff", *arg)
ff_hist_models = get_latest_model("ff-hist", *arg)
eval_set = graph_family_parameters
def make_dir():
if not os.path.exists(output_dir):
os.makedirs(output_dir)
if not os.path.exists(eval_output):
os.makedirs(eval_output)
if not os.path.exists("data"):
os.makedirs("data")
if not os.path.exists("data/train"):
os.makedirs("data/train")
if not os.path.exists("data/val"):
os.makedirs("data/val")
if not os.path.exists("data/eval"):
os.makedirs("data/eval")
def generate_data():
for n in graph_family_parameters.split(" "):
# the naming convention here should not be changed!
train_dir = train_dataset + "/parameter_{}".format(n)
val_dir = val_dataset + "/parameter_{}".format(n)
eval_dir = eval_dataset + "/parameter_{}".format(n)
generate_train = """python data/generate_data.py --problem {} --dataset_size {} --dataset_folder {} \
--u_size {} --v_size {} --graph_family {} --weight_distribution {} \
--weight_distribution_param {} --graph_family_parameter {} """.format(
problem,
dataset_size,
train_dir,
u_size,
v_size,
graph_family,
weight_distribution,
weight_distribution_param,
n,
)
generate_val = """python data/generate_data.py --problem {} --dataset_size {} --dataset_folder {} \
--u_size {} --v_size {} --graph_family {} --weight_distribution {} \
--weight_distribution_param {} --graph_family_parameter {} --seed 20000""".format(
problem,
val_size,
val_dir,
u_size,
v_size,
graph_family,
weight_distribution,
weight_distribution_param,
n,
)
generate_eval = """python data/generate_data.py --problem {} --dataset_size {} --dataset_folder {} \
--u_size {} --v_size {} --graph_family {} --weight_distribution {} \
--weight_distribution_param {} --graph_family_parameter {} --seed 40000""".format(
problem,
eval_size,
eval_dir,
u_size,
v_size,
graph_family,
weight_distribution,
weight_distribution_param,
n,
)
# print(generate_train)
# os.system(generate_train)
subprocess.run(generate_train, shell=True)
# print(generate_val)
# os.system(generate_val)
subprocess.run(generate_val, shell=True)
# print(generate_eval)
# os.system(generate_eval)
subprocess.run(generate_eval, shell=True)
def train_model():
for n in graph_family_parameters.split(" "):
# the naming convention here should not be changed!
train_dir = train_dataset + "/parameter_{}".format(n)
val_dir = val_dataset + "/parameter_{}".format(n)
save_dir = output_dir + extention + "/parameter_{}".format(n)
train = """python run.py --encoder mpnn --model {} --problem {} --batch_size {} --embedding_dim {} --n_heads {} --u_size {} --v_size {} --n_epochs {} \
--train_dataset {} --val_dataset {} --dataset_size {} --val_size {} --checkpoint_epochs {} --baseline {} \
--lr_model {} --lr_decay {} --output_dir {} --log_dir {} --n_encode_layers {} --save_dir {} --graph_family_parameter {} --exp_beta {} --ent_rate {}""".format(
model_type,
problem,
batch_size,
embedding_dim,
n_heads,
u_size,
v_size,
n_epochs,
train_dir,
val_dir,
dataset_size,
val_size,
checkpoint_epochs,
baseline,
lr_model,
lr_decay,
output_dir,
log_dir,
n_encode_layers,
save_dir,
n,
beta_decay,
ent_rate,
)
# print(train)
subprocess.run(train, shell=True)
def tune_model():
for n in graph_family_parameters.split(" "):
# the naming convention here should not be changed!
train_dir = train_dataset + "/parameter_{}".format(n)
val_dir = val_dataset + "/parameter_{}".format(n)
save_dir = output_dir + extention + "/parameter_{}".format(n)
train = """python run.py --tune_baseline --graph_family {} --encoder mpnn --model {} --problem {} --batch_size {} --embedding_dim {} --n_heads {} --u_size {} --v_size {} --n_epochs {} \
--train_dataset {} --val_dataset {} --dataset_size {} --val_size {} --checkpoint_epochs {} --baseline {} \
--lr_model {} --lr_decay {} --output_dir {} --log_dir {} --n_encode_layers {} --save_dir {} --graph_family_parameter {} --exp_beta {} --ent_rate {}""".format(
graph_family,
model_type,
problem,
batch_size,
embedding_dim,
n_heads,
u_size,
v_size,
n_epochs,
train_dir,
val_dir,
dataset_size,
val_size,
checkpoint_epochs,
baseline,
lr_model,
lr_decay,
output_dir,
log_dir,
n_encode_layers,
save_dir,
n,
beta_decay,
ent_rate,
)
# print(train)
subprocess.run(train, shell=True)
def evaluate_model():
evaluate = """python eval.py --problem {} --graph_family {} --embedding_dim {} --load_path {} --ff_models {} --attention_models {} --inv_ff_models {} --ff_hist_models {} \
--inv_ff_hist_models {} --gnn_hist_models {} --gnn_models {} --gnn_simp_hist_models {} --ff_supervised_models {} --eval_baselines {} \
--baseline {} --eval_models {} --eval_dataset {} --u_size {} --v_size {} --eval_set {} --eval_size {} --eval_batch_size {} \
--n_encode_layers {} --n_heads {} --output_dir {} --dataset_size {} --batch_size {} --encoder mpnn --weight_distribution {} --weight_distribution_param {}""".format(
problem,
graph_family,
embedding_dim,
load_path,
ff_models,
attention_models,
inv_ff_models,
ff_hist_models,
inv_ff_hist_models,
gnn_hist_models,
gnn_models,
gnn_simp_hist_models,
ff_supervised_models,
eval_baselines,
baseline,
eval_models,
eval_dataset,
u_size,
v_size,
eval_set,
eval_size,
eval_batch_size,
n_encode_layers,
n_heads,
output_dir,
eval_size,
eval_batch_size,
weight_distribution,
weight_distribution_param,
)
if test_transfer:
evaluate += " --test_transfer"
elif save_eval_data:
evaluate += " --save_eval_data"
# print(evaluate)
subprocess.run(evaluate, shell=True)
if __name__ == "__main__":
# make the directories if they do not exist
make_dir()
# generate_data()
# train_model()
# tune_model()
evaluate_model()