-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMOMoGP.py
420 lines (364 loc) · 15 KB
/
MOMoGP.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
"""
Created on June 08, 2021
@author: Zhongjie Yu
@author: Mingye Zhu
"""
import numpy as np
from MOMoGPstructure import Sum_, Product_x_, GPMixture, Product_y_
import gc
import torch
import gpytorch
from gpytorch.kernels import *
from gpytorch.likelihoods import *
from gpytorch.mlls import *
from torch.optim import *
from scipy.special import logsumexp
class Sum:
"""This class defines the inference of a Sum node.
"""
def __init__(self, **kwargs):
self.children = []
self.weights = []
self.scope=kwargs['scope']
self.mll = []
return None
def forward(self, x_pred, **kwargs):
"""Propagation of the mean and covariance matrix for a Sum node.
Details can be found in Sec. 4.5, equation (10) and (11).
"""
if len(self.children) == 1:
r_ = self.children[0].forward(x_pred, **kwargs)
mu_x = r_[0]
co_x = r_[1]
elif len(self.children) == 2:
# eq (10)
mu_0, cov_0 = self.children[0].forward(x_pred, **kwargs)
mu_1, cov_1 = self.children[1].forward(x_pred, **kwargs)
mu_x = mu_0 * self.weights[0] + mu_1 * self.weights[1]
# 1st term in eq (11)
co1 = cov_0 * self.weights[0] + cov_1 * self.weights[1]
mu_00 = np.matmul(mu_0, mu_0.transpose((0,2,1)))
mu_11 = np.matmul(mu_1, mu_1.transpose((0,2,1)))
# 2nd term in eq (11)
co2 = mu_00 * self.weights[0] + mu_11 * self.weights[1]
# 3rd term in eq (11)
co3 = np.matmul(mu_x, mu_x.transpose((0,2,1)))
co_x = co1+co2-co3 # compute the covariance matrix of the mixture distribution
elif len(self.children)==4:
# eq (10)
mu_0, cov_0 = self.children[0].forward(x_pred, **kwargs)
mu_1, cov_1 = self.children[1].forward(x_pred, **kwargs)
mu_2, cov_2 = self.children[2].forward(x_pred, **kwargs)
mu_3, cov_3 = self.children[3].forward(x_pred, **kwargs)
mu_x = mu_0 * self.weights[0] + mu_1 * self.weights[1] + mu_2 * self.weights[2] + mu_3 * self.weights[3]
# 1st term in eq (11)
co1 = cov_0 * self.weights[0] + cov_1 * self.weights[1] + cov_2 * self.weights[2] + cov_3 * self.weights[3]
mu_00 = np.matmul(mu_0, mu_0.transpose((0,2,1)))
mu_11 = np.matmul(mu_1, mu_1.transpose((0,2,1)))
mu_22 = np.matmul(mu_2, mu_2.transpose((0,2,1)))
mu_33 = np.matmul(mu_3, mu_3.transpose((0,2,1)))
# 2nd term in eq (11)
co2 = mu_00 * self.weights[0] + mu_11 * self.weights[1] + mu_22 * self.weights[2] + mu_33 * self.weights[3]
# 3rd term in eq (11)
co3 = np.matmul(mu_x, mu_x.transpose((0, 2, 1)))
co_x = co1 + co2 - co3 # compute the covariance matrix of the mixture distribution
else:
raise NotImplementedError
return mu_x, co_x
def update(self):
c_mllh = np.array([c.update() for c in self.children])
lw = logsumexp(c_mllh)
logweights = c_mllh - lw
## add a prior in case one child has too large a weight
self.weights = np.exp(logweights) + np.ones(len(logweights)) * 0.5
self.weights = self.weights / np.sum(self.weights)
return lw
def update_mll(self):
if len(self.children)==2:
a = torch.stack((torch.tensor(self.children[0].update_mll()),torch.tensor(self.children[1].update_mll())))
w_log = torch.logsumexp(a,0)+torch.tensor(0.5)
return w_log
else:
raise NotImplementedError
class Product_x:
"""This class defines the inference of a Product node of covariate space.
"""
def __init__(self, **kwargs):
self.children = []
self.split = kwargs['split']
self.dimension = kwargs['dimension']
self.depth = kwargs['depth']
self.splits = kwargs['splits']
return None
def forward(self, x_pred, **kwargs):
"""Propagation of the mean and covariance matrix for a Product node of covariate space.
Details can be found in Sec. 4.5.
"""
y_d = dict.get(kwargs, 'y_d', 0)
mu_x = np.zeros((len(x_pred),1, y_d))
co_x = np.zeros((len(x_pred), y_d,y_d))
idx_all = []
for i, child in enumerate(self.children):
if i < len(self.children) :
if i == 0:
idx = np.where((x_pred[:, self.dimension] <= self.splits[i + 1]))[0]
elif i == len(self.children)-1:
idx = np.where((x_pred[:, self.dimension] > self.splits[i]))[0]
else:
idx = np.where((x_pred[:, self.dimension]>self.splits[i]) & (x_pred[:, self.dimension] <= self.splits[i+1]))[0]
idx_all.append(idx)
mu_x[idx, :, :], co_x[idx, :, :] = child.forward(x_pred[idx], **kwargs)
return mu_x, co_x
def update(self):
return np.sum([c.update() for c in self.children])
def update_mll(self):
sum = 0
for c in self.children:
sum+=c.update_mll()
return sum
class Product_y:
"""This class defines the inference of a Product node of the output space.
"""
def __init__(self, **kwargs):
self.children = []
self.scope = kwargs['scope']
return None
def forward(self, x_pred, **kwargs):
"""Propagation of the mean and covariance matrix for a Product node of output space.
Details can be found in Sec. 4.5, equation (8) and (9).
"""
y_d = dict.get(kwargs, 'y_d', 0)
mu_x = np.zeros((len(x_pred),1, y_d))
co_x = np.zeros((len(x_pred), y_d,y_d ))
if type(self.children[0]) is Sum:
for i, child in enumerate(self.children):
mu_c, co_c= child.forward(x_pred, **kwargs)
mu_x += mu_c
co_x += co_c
return mu_x, co_x
elif type(self.children[0]) is GP:
for i, child in enumerate(self.children):
mu_c, co_c= child.forward(x_pred, **kwargs)
mu_x[:,0, child.scope], co_x[:, child.scope, child.scope] = mu_c.squeeze(-1), co_c.squeeze(-1)
return mu_x, co_x
def update(self):
return np.sum([c.update() for c in self.children])
def update_mll(self):
sum = 0
for c in self.children:
sum+=c.update_mll()
return sum
class ExactGPModel(gpytorch.models.ExactGP):
"""This class defines an exact GP model.
Details can be found in GPyTorch library.
https://docs.gpytorch.ai/en/v1.4.2/examples/01_Exact_GPs/Simple_GP_Regression.html
"""
def __init__(self, **kwargs):
x = dict.get(kwargs,'x')
y = dict.get(kwargs,'y')
likelihood = dict.get(kwargs,'likelihood')
gp_type = dict.get(kwargs,'type')
xd = x.shape[1]
active_dims = torch.tensor(list(range(xd)))
super(ExactGPModel, self).__init__(x, y, likelihood)
self.mean_module = gpytorch.means.ConstantMean()
if gp_type == 'mixed':
m = MaternKernel(nu=1.5, ard_num_dims=xd, active_dims=active_dims)
l = PeriodicKernel()
self.covar_module = ScaleKernel(m + l)
return
elif gp_type == 'matern0.5':
k = MaternKernel(nu=0.5)
elif gp_type == 'matern1.5':
k = MaternKernel(nu=1.5)
elif gp_type == 'matern2.5':
k = MaternKernel(nu=2.5)
elif gp_type == 'rbf':
k = RBFKernel()
elif gp_type == 'matern0.5_ard':
k = MaternKernel(nu=0.5, ard_num_dims=xd, active_dims=active_dims)
elif gp_type == 'matern1.5_ard':
k = MaternKernel(nu=1.5, ard_num_dims=xd, active_dims=active_dims)
elif gp_type == 'matern2.5_ard':
k = MaternKernel(nu=2.5, ard_num_dims=xd, active_dims=active_dims)
elif gp_type == 'rbf_ard':
k = RBFKernel(ard_num_dims=xd)
elif gp_type == 'linear':
k = LinearKernel(ard_num_dims=xd) # ard_num_dims for linear doesn't actually work
else:
raise Exception("Unknown GP type")
self.covar_module = ScaleKernel(k)
lengthscale_prior = gpytorch.priors.GammaPrior(2, 3)
outputscale_prior = gpytorch.priors.GammaPrior(2, 3)
self.covar_module.base_kernel.lengthscale = lengthscale_prior.sample()
self.covar_module.outputscale = outputscale_prior.sample()
def forward(self, x):
mean_x = self.mean_module(x)
covar_x = self.covar_module(x)
return gpytorch.distributions.MultivariateNormal(mean_x, covar_x)
class GP:
"""This class defines the inference of a Leaf node.
"""
def __init__(self, **kwargs):
self.type = kwargs['type']
self.mins = kwargs['mins']
self.maxs = kwargs['maxs']
self.mll = 0
self.x = dict.get(kwargs, 'x', [])
self.scope = kwargs['scope']
self.n = None
self.y = dict.get(kwargs, 'y', [])
self.count = kwargs['count']
def forward(self, x_pred, **kwargs):
"""Compute the mean and covariance matrix of a multivariate Gaussian distribution
from a GP Leaf.
Parameters
----------
x_pred
Test data.
Returns
-------
pm_
The mean.
pv_
The covariance matrix.
"""
device_ = torch.device("cuda" if self.cuda else "cpu")
self.model = self.model.to(device_)
self.model.eval()
self.likelihood.eval()
x = torch.from_numpy(x_pred).float().to(device_)
with torch.no_grad(), gpytorch.settings.fast_pred_var():
observed_pred = self.likelihood(self.model(x))
pm, pv = observed_pred.mean, observed_pred.variance
pm_ = pm.detach().cpu()
pv_ = pv.detach().cpu()
del observed_pred,pm,pv
del self.model
torch.cuda.empty_cache()
gc.collect()
x.detach()
del x
torch.cuda.empty_cache()
gc.collect()
return pm_, pv_
def update(self):
return self.mll
def update_mll(self):
return self.mll_grad
def init(self, **kwargs):
lr = dict.get(kwargs, 'lr', 0.2)
steps = dict.get(kwargs, 'steps', 100)
iter = dict.get(kwargs, 'iter', 0)
self.n = len(self.x)
self.cuda = dict.get(kwargs, 'cuda') and torch.cuda.is_available()
self.device = torch.device("cuda" if self.cuda else "cpu")
if self.cuda:
torch.cuda.empty_cache()
self.x = torch.from_numpy(self.x).float().to(self.device)
self.y = torch.from_numpy(self.y.ravel()).float().to(self.device)
self.likelihood = GaussianLikelihood()
self.likelihood.train()
self.model = ExactGPModel(x=self.x, y=self.y, likelihood=self.likelihood, type='matern1.5_ard').to(
self.device)
self.optimizer = Adam([{'params': self.model.parameters()}], lr=lr)
self.model.train()
mll = ExactMarginalLogLikelihood(self.likelihood, self.model)
print(f"\tGP {self.type} init completed. Training on {self.device}")
iner_LMM = np.zeros((1,steps))
for i in range(steps):
self.optimizer.zero_grad() # Zero gradients from previous iteration
output = self.model(self.x) # Output from model
loss = -mll(output, self.y) # Calc loss and backprop gradients
loss.backward()
self.optimizer.step()
iner_LMM[:, i] = loss.detach().item()
# LOG LIKELIHOOD NOW POSITIVE
self.mll = -loss.detach().item()
self.loss = loss.detach().item()
self.x.detach()
self.y.detach()
del self.x
del self.y
self.x = self.y = None
self.model = self.model.to('cpu')
torch.cuda.empty_cache()
gc.collect()
return iner_LMM
def structure(root_region, scope, **kwargs):
"""This function constructs the MOMoGP with inference routine.
Parameters
----------
root_region
The root node from structure learning.
scope
List of scopes of the output space.
gp_types
Defines the GP leaf type.
Returns
-------
root
The root node.
list(gps.values())
The list of GP leaves.
"""
count=0
root = Sum(scope=scope)
to_process, gps = [(root_region, root)], dict()
gp_types = dict.get(kwargs, 'gp_types', ['matern1.5_ard'])
while len(to_process):
gro, sto = to_process.pop()
# sto = structure object
if type(gro) is Sum_:
for child in gro.children:
if type(child) is Product_x_:
_child = Product_x(split=child.split, depth=child.depth, dimension=child.dimension,splits=child.splits)
sto.children.append(_child)
_cn = len(sto.children)
sto.weights = np.ones(_cn) / _cn
elif type(child) is Product_y_:
scope = child.scope
_child = Product_y(scope = scope)
sto.children.append(_child)
else:
print(type(child))
raise Exception('1')
to_process.extend(zip(gro.children, sto.children))
elif type(gro) is Product_x_: # sto is Product_x
for child in gro.children:
if type(child) is Product_y_:
scope = child.scope
_child = Product_y(scope = scope)
sto.children.append(_child)
elif type(child) is Sum_:
scope = child.scope
_child = Sum(scope=scope)
sto.children.append(_child)
elif type(child) is Product_x_:
_child = Product_x(split=child.split, depth=child.depth, dimension=child.dimension,splits=child.splits)
sto.children.append(_child)
_cn = len(sto.children)
sto.weights = np.ones(_cn) / _cn
else:
raise Exception('1')
to_process.extend(zip(gro.children, sto.children))
elif type(gro) is Product_y_:
i = 0
for child in gro.children:
if type(child) is Sum_:
scope = child.scope
_child = Sum(scope = scope)
sto.children.append(_child)
elif type(child) is GPMixture:
gp_type = gp_types[0]
scopee = gro.scope
key = (*gro.mins, *gro.maxs, gp_type, count, scopee[i])
gps[key] = GP(type=gp_type, mins=gro.mins, maxs=gro.maxs, count=count,
scope=scopee[i])
count+=1
sto.children.append(gps[key])
i += 1
else:
raise Exception('1')
to_process.extend(zip(gro.children, sto.children))
return root, list(gps.values())