-
Notifications
You must be signed in to change notification settings - Fork 103
/
03_train_gcnn.py
286 lines (237 loc) · 9.97 KB
/
03_train_gcnn.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
import os
import importlib
import argparse
import sys
import pathlib
import pickle
import numpy as np
from time import strftime
from shutil import copyfile
import gzip
import tensorflow as tf
import tensorflow.contrib.eager as tfe
import utilities
from utilities import log
from utilities_tf import load_batch_gcnn
def load_batch_tf(x):
return tf.py_func(
load_batch_gcnn,
[x],
[tf.float32, tf.int32, tf.float32, tf.float32, tf.int32, tf.int32, tf.int32, tf.int32, tf.int32, tf.float32])
def pretrain(model, dataloader):
"""
Pre-normalizes a model (i.e., PreNormLayer layers) over the given samples.
Parameters
----------
model : model.BaseModel
A base model, which may contain some model.PreNormLayer layers.
dataloader : tf.data.Dataset
Dataset to use for pre-training the model.
Return
------
number of PreNormLayer layers processed.
"""
model.pre_train_init()
i = 0
while True:
for batch in dataloader:
c, ei, ev, v, n_cs, n_vs, n_cands, cands, best_cands, cand_scores = batch
batched_states = (c, ei, ev, v, n_cs, n_vs)
if not model.pre_train(batched_states, tf.convert_to_tensor(True)):
break
res = model.pre_train_next()
if res is None:
break
else:
layer, name = res
i += 1
return i
def process(model, dataloader, top_k, optimizer=None):
mean_loss = 0
mean_kacc = np.zeros(len(top_k))
n_samples_processed = 0
for batch in dataloader:
c, ei, ev, v, n_cs, n_vs, n_cands, cands, best_cands, cand_scores = batch
batched_states = (c, ei, ev, v, tf.reduce_sum(n_cs, keepdims=True), tf.reduce_sum(n_vs, keepdims=True)) # prevent padding
batch_size = len(n_cs.numpy())
if optimizer:
with tf.GradientTape() as tape:
logits = model(batched_states, tf.convert_to_tensor(True)) # training mode
logits = tf.expand_dims(tf.gather(tf.squeeze(logits, 0), cands), 0) # filter candidate variables
logits = model.pad_output(logits, n_cands.numpy()) # apply padding now
loss = tf.losses.sparse_softmax_cross_entropy(labels=best_cands, logits=logits)
grads = tape.gradient(target=loss, sources=model.variables)
optimizer.apply_gradients(zip(grads, model.variables))
else:
logits = model(batched_states, tf.convert_to_tensor(False)) # eval mode
logits = tf.expand_dims(tf.gather(tf.squeeze(logits, 0), cands), 0) # filter candidate variables
logits = model.pad_output(logits, n_cands.numpy()) # apply padding now
loss = tf.losses.sparse_softmax_cross_entropy(labels=best_cands, logits=logits)
true_scores = model.pad_output(tf.reshape(cand_scores, (1, -1)), n_cands)
true_bestscore = tf.reduce_max(true_scores, axis=-1, keepdims=True)
true_scores = true_scores.numpy()
true_bestscore = true_bestscore.numpy()
kacc = []
for k in top_k:
pred_top_k = tf.nn.top_k(logits, k=k)[1].numpy()
pred_top_k_true_scores = np.take_along_axis(true_scores, pred_top_k, axis=1)
kacc.append(np.mean(np.any(pred_top_k_true_scores == true_bestscore, axis=1)))
kacc = np.asarray(kacc)
mean_loss += loss.numpy() * batch_size
mean_kacc += kacc * batch_size
n_samples_processed += batch_size
mean_loss /= n_samples_processed
mean_kacc /= n_samples_processed
return mean_loss, mean_kacc
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'problem',
help='MILP instance type to process.',
choices=['setcover', 'cauctions', 'facilities', 'indset'],
)
parser.add_argument(
'-m', '--model',
help='GCNN model to be trained.',
type=str,
default='baseline',
)
parser.add_argument(
'-s', '--seed',
help='Random generator seed.',
type=utilities.valid_seed,
default=0,
)
parser.add_argument(
'-g', '--gpu',
help='CUDA GPU id (-1 for CPU).',
type=int,
default=0,
)
args = parser.parse_args()
### HYPER PARAMETERS ###
max_epochs = 1000
epoch_size = 312
batch_size = 32
pretrain_batch_size = 128
valid_batch_size = 128
lr = 0.001
patience = 10
early_stopping = 20
top_k = [1, 3, 5, 10]
train_ncands_limit = np.inf
valid_ncands_limit = np.inf
problem_folders = {
'setcover': 'setcover/500r_1000c_0.05d',
'cauctions': 'cauctions/100_500',
'facilities': 'facilities/100_100_5',
'indset': 'indset/500_4',
}
problem_folder = problem_folders[args.problem]
running_dir = f"trained_models/{args.problem}/{args.model}/{args.seed}"
os.makedirs(running_dir)
### LOG ###
logfile = os.path.join(running_dir, 'log.txt')
log(f"max_epochs: {max_epochs}", logfile)
log(f"epoch_size: {epoch_size}", logfile)
log(f"batch_size: {batch_size}", logfile)
log(f"pretrain_batch_size: {pretrain_batch_size}", logfile)
log(f"valid_batch_size : {valid_batch_size }", logfile)
log(f"lr: {lr}", logfile)
log(f"patience : {patience }", logfile)
log(f"early_stopping : {early_stopping }", logfile)
log(f"top_k: {top_k}", logfile)
log(f"problem: {args.problem}", logfile)
log(f"gpu: {args.gpu}", logfile)
log(f"seed {args.seed}", logfile)
### NUMPY / TENSORFLOW SETUP ###
if args.gpu == -1:
os.environ['CUDA_VISIBLE_DEVICES'] = ''
else:
os.environ['CUDA_VISIBLE_DEVICES'] = f'{args.gpu}'
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
tf.enable_eager_execution(config)
tf.executing_eagerly()
rng = np.random.RandomState(args.seed)
tf.set_random_seed(rng.randint(np.iinfo(int).max))
### SET-UP DATASET ###
train_files = list(pathlib.Path(f'data/samples/{problem_folder}/train').glob('sample_*.pkl'))
valid_files = list(pathlib.Path(f'data/samples/{problem_folder}/valid').glob('sample_*.pkl'))
def take_subset(sample_files, cands_limit):
nsamples = 0
ncands = 0
for filename in sample_files:
with gzip.open(filename, 'rb') as file:
sample = pickle.load(file)
_, _, _, cands, _ = sample['data']
ncands += len(cands)
nsamples += 1
if ncands >= cands_limit:
log(f" dataset size limit reached ({cands_limit} candidate variables)", logfile)
break
return sample_files[:nsamples]
if train_ncands_limit < np.inf:
train_files = take_subset(rng.permutation(train_files), train_ncands_limit)
log(f"{len(train_files)} training samples", logfile)
if valid_ncands_limit < np.inf:
valid_files = take_subset(valid_files, valid_ncands_limit)
log(f"{len(valid_files)} validation samples", logfile)
train_files = [str(x) for x in train_files]
valid_files = [str(x) for x in valid_files]
valid_data = tf.data.Dataset.from_tensor_slices(valid_files)
valid_data = valid_data.batch(valid_batch_size)
valid_data = valid_data.map(load_batch_tf)
valid_data = valid_data.prefetch(1)
pretrain_files = [f for i, f in enumerate(train_files) if i % 10 == 0]
pretrain_data = tf.data.Dataset.from_tensor_slices(pretrain_files)
pretrain_data = pretrain_data.batch(pretrain_batch_size)
pretrain_data = pretrain_data.map(load_batch_tf)
pretrain_data = pretrain_data.prefetch(1)
### MODEL LOADING ###
sys.path.insert(0, os.path.abspath(f'models/{args.model}'))
import model
importlib.reload(model)
model = model.GCNPolicy()
del sys.path[0]
### TRAINING LOOP ###
optimizer = tf.train.AdamOptimizer(learning_rate=lambda: lr) # dynamic LR trick
best_loss = np.inf
for epoch in range(max_epochs + 1):
log(f"EPOCH {epoch}...", logfile)
epoch_loss_avg = tfe.metrics.Mean()
epoch_accuracy = tfe.metrics.Accuracy()
# TRAIN
if epoch == 0:
n = pretrain(model=model, dataloader=pretrain_data)
log(f"PRETRAINED {n} LAYERS", logfile)
# model compilation
model.call = tfe.defun(model.call, input_signature=model.input_signature)
else:
# bugfix: tensorflow's shuffle() seems broken...
epoch_train_files = rng.choice(train_files, epoch_size * batch_size, replace=True)
train_data = tf.data.Dataset.from_tensor_slices(epoch_train_files)
train_data = train_data.batch(batch_size)
train_data = train_data.map(load_batch_tf)
train_data = train_data.prefetch(1)
train_loss, train_kacc = process(model, train_data, top_k, optimizer)
log(f"TRAIN LOSS: {train_loss:0.3f} " + "".join([f" acc@{k}: {acc:0.3f}" for k, acc in zip(top_k, train_kacc)]), logfile)
# TEST
valid_loss, valid_kacc = process(model, valid_data, top_k, None)
log(f"VALID LOSS: {valid_loss:0.3f} " + "".join([f" acc@{k}: {acc:0.3f}" for k, acc in zip(top_k, valid_kacc)]), logfile)
if valid_loss < best_loss:
plateau_count = 0
best_loss = valid_loss
model.save_state(os.path.join(running_dir, 'best_params.pkl'))
log(f" best model so far", logfile)
else:
plateau_count += 1
if plateau_count % early_stopping == 0:
log(f" {plateau_count} epochs without improvement, early stopping", logfile)
break
if plateau_count % patience == 0:
lr *= 0.2
log(f" {plateau_count} epochs without improvement, decreasing learning rate to {lr}", logfile)
model.restore_state(os.path.join(running_dir, 'best_params.pkl'))
valid_loss, valid_kacc = process(model, valid_data, top_k, None)
log(f"BEST VALID LOSS: {valid_loss:0.3f} " + "".join([f" acc@{k}: {acc:0.3f}" for k, acc in zip(top_k, valid_kacc)]), logfile)