-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
173 lines (143 loc) · 6.17 KB
/
test.py
File metadata and controls
173 lines (143 loc) · 6.17 KB
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
import torch
import tqdm
import gpytorch
from gpytorch.means import ConstantMean, LinearMean
from gpytorch.kernels import RBFKernel, ScaleKernel
from gpytorch.variational import VariationalStrategy, CholeskyVariationalDistribution
from gpytorch.distributions import MultivariateNormal
from gpytorch.models import ApproximateGP, GP
from gpytorch.mlls import VariationalELBO, AddedLossTerm
from gpytorch.likelihoods import GaussianLikelihood
from gpytorch.models.deep_gps import DeepGPLayer, DeepGP
from gpytorch.mlls import DeepApproximateMLL
import urllib.request
import os
from scipy.io import loadmat
from math import floor
# # this is for running the notebook in our testing framework
# smoke_test = ('CI' in os.environ)
# if not smoke_test and not os.path.isfile('../elevators.mat'):
# print('Downloading \'elevators\' UCI dataset...')
# urllib.request.urlretrieve('https://drive.google.com/uc?export=download&id=1jhWL3YUHvXIaftia4qeAyDwVxo6j1alk', '../elevators.mat')
# if smoke_test: # this is for running the notebook in our testing framework
# X, y = torch.randn(1000, 3), torch.randn(1000)
# else:
data = torch.Tensor(loadmat('data/elevators.mat')['data'])
import IPython as ip
ip.embed()
X = data[:, :-1]
X = X - X.min(0)[0]
X = 2 * (X / X.max(0)[0]) - 1
y = data[:, -1]
train_n = int(floor(0.8 * len(X)))
train_x = X[:train_n, :].contiguous()
train_y = y[:train_n].contiguous()
test_x = X[train_n:, :].contiguous()
test_y = y[train_n:].contiguous()
from torch.utils.data import TensorDataset, DataLoader
train_dataset = TensorDataset(train_x, train_y)
train_loader = DataLoader(train_dataset, batch_size=1024, shuffle=True)
smoke_test = False
class ToyDeepGPHiddenLayer(DeepGPLayer):
def __init__(self, input_dims, output_dims, num_inducing=128, mean_type='constant'):
if output_dims is None:
inducing_points = torch.randn(num_inducing, input_dims)
batch_shape = torch.Size([])
else:
inducing_points = torch.randn(output_dims, num_inducing, input_dims)
batch_shape = torch.Size([output_dims])
variational_distribution = CholeskyVariationalDistribution(
num_inducing_points=num_inducing,
batch_shape=batch_shape
)
variational_strategy = VariationalStrategy(
self,
inducing_points,
variational_distribution,
learn_inducing_locations=True
)
super(ToyDeepGPHiddenLayer, self).__init__(variational_strategy, input_dims, output_dims)
if mean_type == 'constant':
self.mean_module = ConstantMean(batch_shape=batch_shape)
else:
self.mean_module = LinearMean(input_dims)
self.covar_module = ScaleKernel(
RBFKernel(batch_shape=batch_shape, ard_num_dims=input_dims),
batch_shape=batch_shape, ard_num_dims=None
)
def forward(self, x):
mean_x = self.mean_module(x)
covar_x = self.covar_module(x)
return MultivariateNormal(mean_x, covar_x)
def __call__(self, x, *other_inputs, **kwargs):
"""
Overriding __call__ isn't strictly necessary, but it lets us add concatenation based skip connections
easily. For example, hidden_layer2(hidden_layer1_outputs, inputs) will pass the concatenation of the first
hidden layer's outputs and the input data to hidden_layer2.
"""
if len(other_inputs):
if isinstance(x, gpytorch.distributions.MultitaskMultivariateNormal):
x = x.rsample()
processed_inputs = [
inp.unsqueeze(0).expand(gpytorch.settings.num_likelihood_samples.value(), *inp.shape)
for inp in other_inputs
]
x = torch.cat([x] + processed_inputs, dim=-1)
return super().__call__(x, are_samples=bool(len(other_inputs)))
num_hidden_dims = 2 if smoke_test else 10
class DeepGP(DeepGP):
def __init__(self, train_x_shape):
hidden_layer = ToyDeepGPHiddenLayer(
input_dims=train_x_shape[-1],
output_dims=num_hidden_dims,
mean_type='linear',
)
last_layer = ToyDeepGPHiddenLayer(
input_dims=hidden_layer.output_dims,
output_dims=None,
mean_type='constant',
)
super().__init__()
self.hidden_layer = hidden_layer
self.last_layer = last_layer
self.likelihood = GaussianLikelihood()
def forward(self, inputs):
hidden_rep1 = self.hidden_layer(inputs)
output = self.last_layer(hidden_rep1)
return output
def predict(self, test_loader):
with torch.no_grad():
mus = []
variances = []
lls = []
for x_batch, y_batch in test_loader:
preds = self.likelihood(self(x_batch))
mus.append(preds.mean)
variances.append(preds.variance)
lls.append(model.likelihood.log_marginal(y_batch, model(x_batch)))
return torch.cat(mus, dim=-1), torch.cat(variances, dim=-1), torch.cat(lls, dim=-1)
model = DeepGP(train_x.shape)
num_epochs = 1 if smoke_test else 10
num_samples = 3 if smoke_test else 10
optimizer = torch.optim.Adam([
{'params': model.parameters()},
], lr=0.01)
mll = DeepApproximateMLL(VariationalELBO(model.likelihood, model, train_x.shape[-2]))
# epochs_iter = tqdm.notebook.tqdm(range(num_epochs), desc="Epoch")
for i in range(10):
# Within each iteration, we will go over each minibatch of data
# minibatch_iter = tqdm.notebook.tqdm(train_loader, desc="Minibatch", leave=False)
# for x_batch, y_batch in minibatch_iter:
print(i)
for _, (x_batch, y_batch) in enumerate(train_loader):
with gpytorch.settings.num_likelihood_samples(num_samples):
optimizer.zero_grad()
output = model(x_batch)
import IPython as ip
ip.embed()
loss = -mll(output, y_batch)
loss.backward()
optimizer.step()
# minibatch_iter.set_postfix(loss=loss.item())
import IPython as ip
ip.embed()