-
Notifications
You must be signed in to change notification settings - Fork 12
/
04_test_gcnn_torch.py
193 lines (163 loc) · 6.42 KB
/
04_test_gcnn_torch.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
"""
File adapted from https://github.com/ds4dm/learn2branch
"""
import os
import sys
import importlib
import argparse
import csv
import numpy as np
import time
import pickle
import pathlib
import gzip
import tensorflow as tf
import torch
import utilities
from utilities_gcnn_torch import GCNNDataset as Dataset, load_batch_gcnn as load_batch
def process(model, dataloader, top_k, optimizer=None):
"""
Executes a forward and backward pass of model over the dataset.
Parameters
----------
model : model.BaseModel
A base model, which may contain some model.PreNormLayer layers.
dataloader : torch.utils.data.DataLoader
Dataset to use for training the model.
top_k : list
list of `k` (int) to estimate for accuracy using these many candidates
optimizer : torch.optim
optimizer to use for SGD. No gradient computation takes place if its None.
Return
------
mean_loss : np.float
mean loss of model on data in dataloader
mean_kacc : np.array
computed accuracy for `top_k` candidates
"""
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, weights = map(lambda x:x.to(device), batch)
batched_states = (c, ei, ev, v, n_cs, n_vs)
batch_size = n_cs.shape[0]
weights /= batch_size # sum loss
with torch.no_grad():
_, logits = model(batched_states) # eval mode
logits = torch.unsqueeze(torch.gather(input=torch.squeeze(logits, 0), dim=0, index=cands), 0) # filter candidate variables
logits = model.pad_output(logits, n_cands) # apply padding now
loss = _loss_fn(logits, best_cands, weights)
true_scores = model.pad_output(torch.reshape(cand_scores, (1, -1)), n_cands)
true_bestscore = torch.max(true_scores, dim=-1, keepdims=True).values
true_scores = true_scores.cpu().numpy()
true_bestscore = true_bestscore.cpu().numpy()
kacc = []
for k in top_k:
pred_top_k = torch.topk(logits, k=k).indices.cpu().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.detach_().item() * 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
def _loss_fn(logits, labels, weights):
loss = torch.nn.CrossEntropyLoss(reduction='none')(logits, labels)
return torch.sum(loss * weights)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'problem',
help='MILP instance type to process.',
choices=['setcover', 'cauctions', 'facilities', 'indset'],
)
parser.add_argument(
'-g', '--gpu',
help='CUDA GPU id (-1 for CPU).',
type=int,
default=0,
)
parser.add_argument(
'--test_path',
help='if given, searches for samples in this path',
type=str,
default='',
)
args = parser.parse_args()
### HYPER PARAMETERS ###
seeds = [0,1,2]
gcnn_models = ['baseline_torch']
other_models = []
test_batch_size = 128
top_k = [1, 3, 5, 10]
num_workers = 5
problem_folders = {
'setcover': '500r_1000c_0.05d',
'cauctions': '100_500',
'facilities': '100_100_5',
'indset': '750_4',
}
problem_folder = problem_folders[args.problem]
os.makedirs("test_results", exist_ok=True)
result_file = f"test_results/{args.problem}_GCNN_test_{time.strftime('%Y%m%d-%H%M%S')}.csv"
### NUMPY / TORCH SETUP ###
if args.gpu == -1:
os.environ['CUDA_VISIBLE_DEVICES'] = ''
device = torch.device("cpu")
else:
os.environ['CUDA_VISIBLE_DEVICES'] = f'{args.gpu}'
device = torch.device(f"cuda" if torch.cuda.is_available() else "cpu")
### SET-UP DATASET ###
problem_folder = f"data/samples/{args.problem}/{problem_folders[args.problem]}/test"
if args.test_path:
problem_folder = args.test_path
test_files = list(pathlib.Path(problem_folder).glob('sample_*.pkl'))
test_files = [str(x) for x in test_files]
test_data = Dataset(test_files)
test_data = torch.utils.data.DataLoader(test_data, batch_size=test_batch_size,
shuffle = False, num_workers = num_workers, collate_fn = load_batch)
print(f"{len(test_files)} test samples")
evaluated_policies = [['gcnn', model] for model in gcnn_models] + \
[['ml-competitor', model] for model in other_models]
fieldnames = [
'problem',
'policy',
'seed',
] + [
f'acc@{k}' for k in top_k
]
with open(result_file, 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for policy_type, policy_name in evaluated_policies:
print(f"{policy_type}:{policy_name}...")
for seed in seeds:
rng = np.random.RandomState(seed)
policy = {}
policy['name'] = policy_name
policy['type'] = policy_type
if policy['type'] == 'gcnn':
# load model
sys.path.insert(0, os.path.abspath(f"models/{policy['name']}"))
import model
importlib.reload(model)
del sys.path[0]
policy['model'] = model.GCNPolicy()
policy['model'].restore_state(f"trained_models/{args.problem}/{policy['name']}/{seed}/best_params.pkl")
policy['model'].to(device)
test_loss, test_kacc = process(policy['model'], test_data, top_k)
print(f" {seed} " + " ".join([f"acc@{k}: {100*acc:4.1f}" for k, acc in zip(top_k, test_kacc)]))
writer.writerow({
**{
'problem':args.problem,
'policy': f"{policy['type']}:{policy['name']} (1.0)",
'seed': seed,
},
**{
f'acc@{k}': test_kacc[i] for i, k in enumerate(top_k)
},
})
csvfile.flush()