-
Notifications
You must be signed in to change notification settings - Fork 27
/
budget_allocation.py
198 lines (160 loc) · 6.08 KB
/
budget_allocation.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
import numpy as np
import torch
from coverage import optimize_coverage_multilinear, CoverageInstanceMultilinear, dgrad_coverage, hessian_coverage
import pickle
from functools import partial
from submodular import ContinuousOptimizer
import torch.nn as nn
import random
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--layers', type=int, default=2)
args = parser.parse_args()
num_layers = args.layers
def load_instance(n, i, num_targets):
with open('new_budget_instances/yahoo_' + str(n) + '_' + str(i), 'rb') as f:
Pfull, wfull = pickle.load(f, encoding='bytes')
P = np.zeros((num_items, num_targets), dtype=np.float32)
for i in range(num_targets):
for j in Pfull[i]:
P[j, i] = Pfull[i][j]
P = torch.from_numpy(P).float()
return P
num_items = 100
num_targets = 500
num_iters = 40
use_hessian = True
test_pct = 0.2
num_instances = 1000
total_instances = 1000
instances_load = random.sample(range(total_instances), num_instances)
Ps = [load_instance(num_items, i, num_targets) for i in instances_load]
kvals = [5, 10, 20]
opt_vals = {}
mse_vals = {}
algs = ['diffopt', 'twostage', 'opt', 'random']
for alg in algs:
opt_vals[alg] = np.zeros((30, len(kvals)))
mse_vals[alg] = np.zeros((30, len(kvals)))
activation = 'relu'
intermediate_size = 200
def make_fc():
if num_layers > 1:
if activation == 'relu':
activation_fn = nn.ReLU
elif activation == 'sigmoid':
activation_fn = nn.Sigmoid
else:
raise Exception('Invalid activation function: ' + str(activation))
net_layers = [nn.Linear(num_features, intermediate_size), activation_fn()]
for hidden in range(num_layers-2):
net_layers.append(nn.Linear(intermediate_size, intermediate_size))
net_layers.append(activation_fn())
net_layers.append(nn.Linear(intermediate_size, num_targets))
net_layers.append(nn.ReLU())
return nn.Sequential(*net_layers)
else:
return nn.Sequential(nn.Linear(num_features, num_targets), nn.ReLU())
idx = 0
for idx in range(30):
print(idx)
test = random.sample(range(num_instances), int(test_pct*num_instances))
train = [i for i in range(num_instances) if i not in test]
w = np.ones(num_targets, dtype=np.float32)
num_features = int(num_targets)
true_transform = nn.Sequential(
nn.Linear(num_targets, num_targets),
nn.ReLU(),
nn.Linear(num_targets, num_targets),
nn.ReLU(),
nn.Linear(num_targets, num_features),
nn.ReLU(),
)
data = [torch.from_numpy(true_transform(P).detach().numpy()).float() for P in Ps]
f_true = [CoverageInstanceMultilinear(P, w, True) for P in Ps]
net = make_fc()
net_two_stage = make_fc()
loss_fn = nn.MSELoss()
learning_rate = 1e-3
optimizer = torch.optim.Adam(net_two_stage.parameters(), lr=learning_rate)
def get_test_mse(net):
loss = 0
for i in test:
pred = net(data[i])
loss += loss_fn(pred, Ps[i])
return loss/len(test)
def get_train_mse(net):
loss = 0
for i in train:
pred = net(data[i])
loss += loss_fn(pred, Ps[i])
return loss/len(train)
def get_test_mse_random():
loss = 0
train_sum = 0
for i in train:
train_sum += Ps[0].sum()
train_sum /= len(train)
for i in test:
pred = torch.rand(num_items, num_targets).float()
pred *= train_sum/pred.sum()
loss += loss_fn(pred, Ps[i])
return loss/len(test)
print('train two stage')
for t in range(4001):
i = random.choice(train)
pred = net_two_stage(data[i])
loss = loss_fn(pred, Ps[i])
optimizer.zero_grad()
loss.backward()
optimizer.step()
mse_vals['twostage'][idx, 0] = get_test_mse(net_two_stage).item()
for kidx, k in enumerate(kvals):
optfunc = partial(optimize_coverage_multilinear, w = w, k=k, c = 0.95)
dgrad = partial(dgrad_coverage, w = w)
if use_hessian:
hessian = partial(hessian_coverage, w = w)
else:
hessian = None
opt = ContinuousOptimizer(optfunc, dgrad, hessian, 0.95)
opt.verbose = False
def eval_opt(net, instances):
val = 0.
for i in instances:
pred = net(data[i])
x = opt(pred)
val += f_true[i](x)
return val/len(instances)
def get_opt(instances):
val = 0.
for i in instances:
x = opt(Ps[i])
val += f_true[i](x)
return val/len(instances)
def get_rand(instances):
val = 0
for _ in range(100):
for i in instances:
x = np.zeros(num_items)
x[random.sample(range(num_items), k)] = 1
x = torch.from_numpy(x).float()
val += f_true[i](x)
return val/(100*len(instances))
opt_vals['opt'][idx, kidx] = get_opt(test).item()
opt_vals['twostage'][idx, kidx] = eval_opt(net_two_stage, test).item()
opt_vals['random'][idx, kidx] = get_rand(test).item()
optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate)
for t in range(num_iters):
i = random.choice(train)
pred = net(data[i])
x = opt(pred)
loss = -f_true[i](x)
optimizer.zero_grad()
loss.backward()
optimizer.step()
opt_vals['diffopt'][idx, kidx] = eval_opt(net, test).item()
mse_vals['diffopt'][idx, kidx] = get_test_mse(net).item()
for alg in algs:
print(alg, opt_vals[alg][idx, kidx])
pickle.dump(opt_vals, open('evaluation_synthetic_full_{}_opt.pickle'.format(num_layers), 'wb'))
pickle.dump(mse_vals, open('evaluation_synthetic_full_{}_mse.pickle'.format(num_layers), 'wb'))