-
Notifications
You must be signed in to change notification settings - Fork 1
/
problem.py
370 lines (281 loc) · 11.5 KB
/
problem.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import torch
from torch import nn
import numpy as np
from torch.nn import functional as F
import torchvision.datasets as datasets
from torch.utils.data import DataLoader, RandomSampler, Subset
import torchvision.transforms as transforms
device = 'cpu' #'cuda' if torch.cuda.is_available() else 'cpu'
def init_weights(m):
# initialize weights of the model m
if isinstance(m, nn.Linear) or isinstance(m, nn.Conv2d):
torch.nn.init.xavier_uniform_(m.weight)
m.bias.data.fill_(0.01)
class Variable(nn.Module):
"""A wrapper to turn a tensor of parameters into a module for optimization."""
def __init__(self, data: torch.Tensor):
"""Create Variable holding `data` tensor."""
super().__init__()
self.x = nn.Parameter(data)
# define mlp_problem, but as a class
class MLPProblemClass:
def __init__(self,
num_vars=2,
num_gaussians=4,
num_samples=25,):
# generate list of random covariance matrices
trils = []
for _ in range(num_gaussians):
mat = torch.rand(num_vars, num_vars)
mat = mat + 2 * torch.diag_embed(torch.absolute(torch.diag(mat)))
tril = torch.tril(mat)
trils.append(tril)
# Create gaussian distributions with random mean and covariance
gaussians = [
torch.distributions.multivariate_normal.MultivariateNormal(
loc=torch.randn(num_vars),
#covariance_matrix=cov[i]
#covariance_matrix=torch.eye(num_vars) * torch.rand(1),
scale_tril=trils[i],
)
for i in range(num_gaussians)
]
# Randomly assign each of the four gaussians a 0-1 label
# Do again if all four gaussians have the same label (don't want that)
gaussian_labels = np.zeros((num_gaussians,))
while (gaussian_labels == 0).all() or (gaussian_labels == 1).all():
gaussian_labels = torch.randint(0, 2, size=(num_gaussians,))
# Generate a dataset of 100 points with 25 points drawn from each gaussian
# Label of the datapoint is the same as the label of the gaussian it came from
x = torch.cat([g.sample((num_samples,)) for g in gaussians])
y = torch.cat([torch.full((num_samples,), float(label)) for label in gaussian_labels])
perm = torch.randperm(len(x))
x = x[perm]
y = y[perm]
self.model0 = nn.Sequential(
nn.Linear(num_vars, 2), nn.ReLU(), nn.Linear(2, 1), nn.Sigmoid()
)
self.model0.device = device
print('print device is : ',self.model.device)
self.model0.apply(init_weights)
self.dataset = (x, y)
def obj_function(self, model):
x, y = self.dataset
y_hat = model(x).view(-1)
weight_norm = model[0].weight.norm() + model[2].weight.norm()
return F.binary_cross_entropy(y_hat, y) + 5e-4 / 2 * weight_norm
# define rosenbrock problem as a class
class RosenbrockProblem:
def __init__(self,
x0=None,
num_vars=2,
):
if x0 is None:
x0 = torch.tensor([-1.5 if i % 2 == 0 else 1.5 for i in range(num_vars)], dtype=torch.float32, requires_grad=True)
else :
x0 = torch.tensor(x0, dtype=torch.float32, requires_grad=True)
self.model0 = Variable(x0)
#def function_def(self, x):
# return torch.sum(100.0 * (x[1:] - x[:-1] ** 2.0) ** 2.0 + (1 - x[:-1]) ** 2.0)
def function_def(self, x, y):
return (1-x)**2 + 100*(y-x**2)**2
def obj_function(self, model):
x = model.x
return self.function_def(x[0],x[1])
class SquareProblemClass:
def __init__(self,
x0=0,
scale=1,
center=1
):
x0 = torch.tensor([x0], dtype=torch.float32, requires_grad=True)
self.model0 = Variable(x0)
self.scale = scale
self.center = center
def function_def(self, x):
return self.scale*(x-self.center)**2
def obj_function(self, model):
x = model.x
return self.function_def(x[0])
class NoisyHillsProblem:
def __init__(self,
x0=[0,0],
scale=1,
center=1
):
x0 = torch.tensor(x0, dtype=torch.float32, requires_grad=True)
self.model0 = Variable(x0)
self.scale = scale
self.center = center
def function_def(self, x, y):
return -1 * torch.sin(x * x) * torch.cos(3 * y * y) * torch.exp(-(x * y) * (x * y)) - torch.exp(-(x + y) * (x + y))
def obj_function(self, model):
x = model.x
return self.function_def(x[0],x[1])
class RastriginProblem():
def __init__(self,
x0=[0,0],
scale=1,
center=1
):
x0 = torch.tensor(x0, dtype=torch.float32, requires_grad=True)
self.model0 = Variable(x0)
self.scale = scale
self.center = center
def function_def(self, x, y):
return 20 + x**2 - 10*torch.cos(2*np.pi*x) + y**2 - 10*torch.cos(2*np.pi*y)
def obj_function(self, model):
x = model.x
return self.function_def(x[0],x[1])
class AckleyProblem():
def __init__(self,
x0=[0,0],
scale=1,
center=1
):
x0 = torch.tensor(x0, dtype=torch.float32, requires_grad=True)
self.model0 = Variable(x0)
self.scale = scale
self.center = center
def function_def(self, x, y):
return -20*torch.exp(-0.2*torch.sqrt(0.5*(x**2 + y**2))) - torch.exp(0.5*(torch.cos(2*torch.pi*x) + torch.cos(2*torch.pi*y))) + np.exp(1) + 20
def obj_function(self, model):
x = model.x
return self.function_def(x[0],x[1])
class GaussianHillsProblem:
def __init__(self,
x0=[0,0],
scale=1,
center=1
):
x0 = torch.tensor(x0, dtype=torch.float32, requires_grad=True)
self.model0 = Variable(x0)
self.scale = scale
self.center = center
def fd(self, x, y, x_mean, y_mean, x_sig, y_sig):
normalizing = 1 / (2 * torch.pi * x_sig * y_sig)
x_exp = (-1 * (x - x_mean)**2) / (2 * (x_sig)**2)
y_exp = (-1 * (y - y_mean)**2) / (2 * (y_sig)**2)
return normalizing * torch.exp(x_exp + y_exp)
def function_def(self, x, y):
z = -1 * self.fd(x, y, x_mean=-0.5, y_mean=-0.8, x_sig=0.35, y_sig=0.35)
# three steep gaussian trenches
z -= self.fd(x, y, x_mean=1.0, y_mean=-0.5, x_sig=0.1, y_sig=0.5)
z -= self.fd(x, y, x_mean=-1.0, y_mean=0.5, x_sig=0.2, y_sig=0.2)
z -= self.fd(x, y, x_mean=-0.5, y_mean=-0.8, x_sig=0.2, y_sig=0.2)
return z
def obj_function(self, model):
x = model.x
return self.function_def(x[0],x[1])
class NormProblem:
def __init__(self,
x0=[0,0],
scale=1,
center=1
):
x0 = torch.tensor(x0, dtype=torch.float32, requires_grad=True)
self.model0 = Variable(x0)
self.scale = scale
self.center = center
def function_def(self, x, y):
return torch.sqrt(x**2 + (1.1*y)**2)
def obj_function(self, model):
x = model.x
return self.function_def(x[0],x[1])
class YNormProblem:
def __init__(self,
x0=[0,0],
scale=1,
center=1
):
x0 = torch.tensor(x0, dtype=torch.float32, requires_grad=True)
self.model0 = Variable(x0)
self.scale = scale
self.center = center
def function_def(self, x, y):
return torch.sqrt(y**2) * 10
def obj_function(self, model):
x = model.x
return self.function_def(x[0],x[1])
class MNISTProblemClass:
def __init__(self,
classes,
batch_size=5,
num_classes_selected=2,
weights_flag=False
):
# maximum is 45
self.batch_size = batch_size
self.num_classes_selected = num_classes_selected
self.weights_flag = weights_flag
self.num_classes = 10
# classes that define the problem
self.classes = classes
# tranform data, resize to make it smaller
transform = transforms.Compose([transforms.ToTensor(), transforms.Resize(10),
transforms.Normalize((0.5,), (0.5,))
])
mnist_trainset = datasets.MNIST(root='./data', train=True, download=True, transform=transform)
# Selecting the classes chosen from train dataset
idx = (mnist_trainset.targets == self.classes[0]) | (mnist_trainset.targets == self.classes[1])
mnist_trainset.targets = mnist_trainset.targets[idx]
mnist_trainset.data = mnist_trainset.data[idx]
# assign classes to 0 or 1 label - for proper computing of loss
for i in range(0, len(mnist_trainset.targets)):
if mnist_trainset.targets[i] == self.classes[0]:
mnist_trainset.targets[i] = 0
else:
mnist_trainset.targets[i] = 1
# set dataset
self.dataset = mnist_trainset
# model definition
self.model0 = nn.Sequential(
nn.Conv2d(
in_channels=1,
out_channels=5,
kernel_size=5,
stride=1,
padding=2,
),
nn.ReLU(),
nn.AdaptiveMaxPool2d(3),
nn.Flatten(),
nn.Linear(45, 1),
nn.Sigmoid()
)
if (weights_flag):
self.model0.apply(init_weights)
self.obj_function = self._obj_function
# prepares data from sampler to be inputted to model
def load_data_from_sampler(self, subset):
batch_data = torch.tensor([])
batch_labels = torch.tensor([])
# loop over the batch generated,
# data samples go in batch_data tensor, labels go in batch_labels
for data, target in subset:
batch_data = torch.cat((batch_data, data), dim=0)
batch_labels = torch.cat((batch_labels, torch.tensor([int(target)])), dim=0)
return batch_data, batch_labels
def _obj_function(self, model):
# defining criteria of loss
criterion = nn.BCELoss()
# use random sampler to get random indices in dataset
# the indices will determine our random batch
mnist_trainset = self.dataset
# get defining indices
batch_indices = RandomSampler(mnist_trainset, replacement=True, num_samples=self.batch_size, generator=None)
# pass indices to subset in order to get a sample
current_batch = Subset(mnist_trainset, list(batch_indices))
running_loss = 0
# split the subsample into data and labels to be fed to model
batch_data, batch_labels = self.load_data_from_sampler(current_batch)
# reshaping bc pytorch wants input of [batch, channel, height, width] - since we have o
batch_data = batch_data[:, None, :, :]
y_hat = model(batch_data)
batch_labels = torch.reshape(batch_labels, (5, 1))
loss = criterion(y_hat, batch_labels)
running_loss += loss.item()
self.running_loss = running_loss
return loss
def get_obj_function(self):
return self.obj_function