-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRBFKernelDirectionalGrad.py
291 lines (253 loc) · 9.13 KB
/
RBFKernelDirectionalGrad.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
#!/usr/bin/env python3
import torch
from gpytorch.lazy.kronecker_product_lazy_tensor import KroneckerProductLazyTensor
from gpytorch.kernels.rbf_kernel import RBFKernel, postprocess_rbf
class RBFKernelDirectionalGrad(RBFKernel):
r"""
Pass in v1 and v2 through the params. If v1 has n_dir1 directions per
point in x2 then it should be shape n1*n_dir1 x dim. The directions
are assumed to be stored in blocks so that the first n_dir1 directions
belong to x1[0] and the second n_dir1 directions belong to x1[1] etc.
If you have a single set of global directions such as torch.eye(dim), then
you can repeat those to make v1 and v2 with
v1 = torch.eye(dim).repeat(n1,1)
Args:
:attr:`batch_shape` (torch.Size, optional):
Set this if you want a separate lengthscale for each
batch of input data. It should be `b` if :attr:`x1` is a `b x n x d` tensor. Default: `torch.Size([])`.
:attr:`active_dims` (tuple of ints, optional):
Set this if you want to compute the covariance of only a few input dimensions. The ints
corresponds to the indices of the dimensions. Default: `None`.
:attr:`lengthscale_prior` (Prior, optional):
Set this if you want to apply a prior to the lengthscale parameter. Default: `None`.
:attr:`lengthscale_constraint` (Constraint, optional):
Set this if you want to apply a constraint to the lengthscale parameter. Default: `Positive`.
:attr:`eps` (float):
The minimum value that the lengthscale can take (prevents divide by zero errors). Default: `1e-6`.
Attributes:
:attr:`lengthscale` (Tensor):
The lengthscale parameter. Size/shape of parameter depends on the
:attr:`ard_num_dims` and :attr:`batch_shape` arguments.
"""
def forward(self, x1, x2, diag=False, **params):
batch_shape = x1.shape[:-2]
n_batch_dims = len(batch_shape)
n1, d = x1.shape[-2:]
n2 = x2.shape[-2]
v1 = params["v1"]
v2 = params["v2"]
# number of directions per point
n_dir1 = int(v1.shape[-2] / n1)
# print("n_dir1", n_dir1)
n_dir2 = int(v2.shape[-2] / n2)
# print("n_dir2", n_dir2)
# set num the number of directions for num_outputs_per_input
self.set_num_directions(n_dir1, n_dir2)
# normalize directions
v1 = (v1.T / torch.norm(v1, dim=1)).T
v2 = (v2.T / torch.norm(v2, dim=1)).T
# K = torch.zeros(*batch_shape, n1 * (d + 1), n2 * (d + 1), device=x1.device, dtype=x1.dtype)
K = torch.zeros(
*batch_shape,
n1 * (n_dir1 + 1),
n2 * (n_dir2 + 1),
device=x1.device,
dtype=x1.dtype
)
K = torch.zeros(
*batch_shape,
n1 * (n_dir1 + 1),
n2 * (n_dir2 + 1),
device=x1.device,
dtype=x1.dtype
)
if not diag:
# Scale the inputs by the lengthscale (for stability)
x1_ = x1.div(self.lengthscale)
x2_ = x2.div(self.lengthscale)
x1__ = x1_.div(self.lengthscale)
x2__ = x2_.div(self.lengthscale)
# 1) Kernel block
diff = self.covar_dist(
x1_,
x2_,
square_dist=True,
dist_postprocess_func=postprocess_rbf,
**params
)
K_11 = diff
K[..., :n1, :n2] = K_11
# 2) First gradient block
x2_v2 = x2__.reshape(n2, 1, d).bmm(
torch.transpose(v2.reshape(n2, n_dir2, d), -2, -1)
)
x1_v2 = x1__ @ v2.T
outer = x1_v2 - x2_v2.flatten()
# permute cols so we get blocks for v1,v2,v3,...
pi1 = (
torch.arange(n2 * (n_dir2))
.view(n2, n_dir2)
.t()
.reshape((n2 * (n_dir2)))
)
"""
Dimension does not match
outer1 = outer[:, pi1] / self.lengthscale.unsqueeze(-2)
"""
outer1 = outer[:, pi1]
K[..., :n1, n2:] = outer1 * K_11.repeat(
[*([1] * (n_batch_dims + 1)), n_dir2]
)
# Second gradient block
x1_v1 = x1__.reshape(n1, 1, d).bmm(
torch.transpose(v1.reshape(n1, n_dir1, d), -2, -1)
)
x2_v1 = x2__ @ v1.T
outer = x1_v1.flatten() - x2_v1
# permute cols so we get blocks for v1,v2,v3,...
pi2 = (
torch.arange(n1 * (n_dir1))
.view(n1, n_dir1)
.t()
.reshape((n1 * (n_dir1)))
)
outer2 = outer[:, pi2]
# outer2 = outer2.t() / self.lengthscale.unsqueeze(-2)
outer2 = outer2.t()
K[..., n1:, :n2] = -outer2 * K_11.repeat(
[n_dir1, *([1] * (n_batch_dims + 1))]
)
# 4) Hessian block (n1*n_dir1, n2*n_dir2)
outer3 = outer1.repeat(1, n_dir1, 1) * outer2.repeat(1, 1, n_dir2)
# kronecker product term
# kp = v1 @ v2.T / self.lengthscale.pow(2)
kp = (v1 / self.lengthscale) @ (v2 / self.lengthscale).T
kp = kp[:, pi1][pi2, :]
chain_rule = kp - outer3
K[..., n1:, n2:] = chain_rule * K_11.repeat(
[*([1] * n_batch_dims), n_dir1, n_dir2]
)
# Apply a perfect shuffle permutation to match the MutiTask ordering
pi1 = (
torch.arange(n1 * (n_dir1 + 1))
.view(n_dir1 + 1, n1)
.t()
.reshape((n1 * (n_dir1 + 1)))
)
pi2 = (
torch.arange(n2 * (n_dir2 + 1))
.view(n_dir2 + 1, n2)
.t()
.reshape((n2 * (n_dir2 + 1)))
)
K = K[..., pi1, :][..., :, pi2]
return K
else:
assert 0
if not (
n1 == n2
and torch.eq(x1, x2).all()
and n_dir1 == n_dir2
and torch.eq(v1, v2).all()
):
raise RuntimeError("diag=True only works when x1 == x2 and v1 == v2")
kernel_diag = super(RBFKernelDirectionalGrad, self).forward(
x1, x2, diag=True
)
grad_diag = torch.ones(
*batch_shape, n2, n_dir2, device=x1.device, dtype=x1.dtype
) / self.lengthscale.pow(2)
grad_diag = grad_diag.transpose(-1, -2).reshape(*batch_shape, n2 * n_dir2)
k_diag = torch.cat((kernel_diag, grad_diag), dim=-1)
pi = (
torch.arange(n2 * (n_dir2 + 1))
.view(n_dir2 + 1, n2)
.t()
.reshape((n2 * (n_dir2 + 1)))
)
return k_diag[..., pi]
def set_num_directions(self, n_dir1, n_dir2):
"""needed num_outputs_per_intput doesnt take v1,v2 as
args"""
self.n_dir1 = n_dir1
self.n_dir2 = n_dir2
def num_outputs_per_input(self, x1, x2):
return (self.n_dir1 + 1, self.n_dir2 + 1)
# return self.n_dir1+1
def __call__(
self,
x1,
x2=None,
diag=False,
last_dim_is_batch=False,
v1=None,
v2=None,
**params
):
# number of directions per point
n1 = x1.shape[-2]
if x2 is None:
n2 = n1
else:
n2 = x2.shape[-2]
n_dir1 = int(v1.shape[-2] / n1)
n_dir2 = int(v2.shape[-2] / n2)
# set num the number of directions for num_outputs_per_input
self.set_num_directions(n_dir1, n_dir2)
return super().__call__(
x1,
x2,
diag=diag,
last_dim_is_batch=last_dim_is_batch,
v1=v1,
v2=v2,
**params
)
if __name__ == "__main__":
torch.manual_seed(0)
# generate training data
n1 = 2
n2 = 2
dim = 2
train_x1 = torch.tensor(
[
[0., 0.],
[1., 1.],
],
dtype=torch.float,
)
train_x2 = train_x1.clone()
# set directions
v1 = torch.tensor(
[
[1., 0.],
[0., 1.],
],
dtype=torch.float,
)
v1 = v1.repeat(n1, 1)
# v2 = torch.rand(1, dim)
# v2 = v2.repeat(n1, 1)
v2 = v1.clone()
v1 = (v1.T / torch.norm(v1, dim=1)).T
v2 = (v2.T / torch.norm(v2, dim=1)).T
k = RBFKernelDirectionalGrad(ard_num_dims=2)
k.lengthscale = torch.tensor(
[[1., 2.]], dtype=torch.float
)
# kernel = RBFKernelDirectionalGrad()
# params = {"v1": v1, "v2": v2}
# K = k(train_x, train_x2, **params)
K = k(train_x1, train_x2, v1=v1, v2=v2)
tmp = K.evaluate()
print(tmp)
# print(K.detach().numpy().shape)
# verify against RBFKernelGrad
from gpytorch.kernels import RBFKernelGrad
kk = RBFKernelGrad(ard_num_dims=2)
kk.lengthscale = torch.tensor(
[[1., 2.]], dtype=torch.float
)
KK = kk(train_x1, train_x2)
print(KK.evaluate())
# print(KK.detach().numpy() - K.detach().numpy())