-
Notifications
You must be signed in to change notification settings - Fork 1
/
nngpk.py
258 lines (220 loc) · 8.71 KB
/
nngpk.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
# -*- coding: utf-8 -*-
# Original code: https://github.com/TeaPearce/Bayesian_NN_Ensembles/blob/master/regression/module_gp.py
import math
import numpy as np
import importlib
import torch
import tensorflow as tf
#
# import neural_tangents as nt
# from neural_tangents import stax
class NNGPKernel:
def __init__(self, kernel_type, b_var_list=[1., 0.], w_var_list=[2., 1.]):
self.kernel_type = kernel_type
self.name_ = 'GP'
self.b_var_list = b_var_list
self.w_var_list = w_var_list
def __relu_kernel(self, X, X2=None):
def relu_inner(x, x2):
k_x_x = self.b_var_list[0] + self.w_var_list[0]*(np.matmul(x,x.T))/d_in
k_x2_x2 = self.b_var_list[0] + self.w_var_list[0]*(np.matmul(x2,x2.T))/d_in
k_x_x2 = self.b_var_list[0] + self.w_var_list[0]*(np.matmul(x,x2.T))/d_in
k_s = k_x_x2 / np.sqrt(k_x_x * k_x2_x2)
if k_s>1.0: k_s=1.0 # occasionally get some overflow errors
theta = np.arccos(k_s)
x_bar = np.sqrt(k_x_x)
x2_bar = np.sqrt(k_x2_x2)
return self.b_var_list[1] + self.w_var_list[1]/(2*np.pi) * x_bar * x2_bar * (np.sin(theta) + (np.pi-theta)*np.cos(theta))
if X2 is None:
same_inputs=True
X2 = X
else:
same_inputs=False
cov = np.zeros([X.shape[0],X2.shape[0]])
d_in = X.shape[-1]
if not same_inputs:
for i in range(X.shape[0]):
if i % 10 == 0:
print('compiling cov, row... '+str(i) + ' / ' + str(X.shape[0]),end='\r')
for j in range(X2.shape[0]):
cov[i,j] = relu_inner(X[i], X2[j])
else: # use symmetry
for i in range(X.shape[0]):
if i % 10 == 0:
print('compiling cov, row... '+str(i) + ' / ' + str(X.shape[0]),end='\r')
for j in range(i+1):
cov[i,j] = relu_inner(X[i], X2[j])
cov += np.tril(cov,k=-1).T
return cov
def __relu_kernel_pt(self, X, X2=None):
# https://arxiv.org/pdf/1711.00165.pdf
if X2 is not None:
X = torch.cat([X, X2], 0)
K = X @ X.T / X.shape[-1] * self.w_var_list[0] + self.b_var_list[0]
for i in range(1, len(self.w_var_list)):
K_diag_sqrt = K.diag().sqrt()
normalizer = K_diag_sqrt.view(-1, 1) @ K_diag_sqrt.view(1, -1)
Theta = ((K / normalizer).clamp_(max=1.)).arccos()
K = (Theta.sin() + (np.pi-Theta) * Theta.cos()) * normalizer / (2*np.pi) * self.w_var_list[i] + self.b_var_list[i]
if X2 is not None:
return K[:-X2.shape[0], -X2.shape[0]:]
else:
return K
def make_relu_kernel_tf(self):
def __relu_kernel_tf(X, X2=None):
if X2 is not None:
X = tf.concat([X, X2], 0)
K = tf.matmul(X, X, transpose_b=True) / tf.cast(X.shape[-1], tf.float32) * self.w_var_list[0] + self.b_var_list[0]
for i in range(1, len(self.w_var_list)):
K_diag_sqrt = tf.linalg.tensor_diag_part(K) ** 0.5
normalizer = tf.reshape(K_diag_sqrt, [-1, 1]) @ tf.reshape(K_diag_sqrt, [1, -1])
Theta = tf.math.acos(tf.clip_by_value(K / normalizer, clip_value_min=-1., clip_value_max=1.))
K = (tf.math.sin(Theta) + (np.pi-Theta) * tf.math.cos(Theta)) * normalizer / (2*np.pi) * self.w_var_list[i] + self.b_var_list[i]
if X2 is not None:
return tf.expand_dims(tf.linalg.tensor_diag_part(K[:X2.shape[0], X2.shape[0]:]), -1)
else:
return tf.expand_dims(tf.linalg.tensor_diag_part(K), -1)
return __relu_kernel_tf
def __Lrelu_kernel(self, X, X2=None, a=0.2):
# leaky relu kernel from Tsuchida, 2018, eq. 6
def Lrelu_inner(x, x2):
# actually these should be 1/d_in going by Lee. But we leave it normal
# to be equivalent to our NN ens implementation
k_x_x = self.b_var_list[0] + self.w_var_list[0]*(np.matmul(x,x.T))/d_in
k_x2_x2 = self.b_var_list[0] + self.w_var_list[0]*(np.matmul(x2,x2.T))/d_in
k_x_x2 = self.b_var_list[0] + self.w_var_list[0]*(np.matmul(x,x2.T))/d_in
k_s = k_x_x2 / np.sqrt(k_x_x * k_x2_x2)
theta = np.arccos(k_s)
x_bar = np.sqrt(k_x_x)
x2_bar = np.sqrt(k_x2_x2)
return self.b_var_list[1] + self.w_var_list[1] * x_bar * x2_bar * ( np.square(1-a)/(2*np.pi) * (np.sin(theta) + (np.pi-theta)*np.cos(theta)) + a*np.cos(theta))
if X2 is None:
same_inputs=True
X2 = X
else:
same_inputs=False
cov = np.zeros([X.shape[0],X2.shape[0]])
d_in = X.shape[-1]
if not same_inputs:
for i in range(X.shape[0]):
for j in range(X2.shape[0]):
cov[i,j] = Lrelu_inner(X[i], X2[j])
else: # use symmetry
for i in range(X.shape[0]):
if i % 10 == 0:
print('compiling cov, row... '+str(i) + ' / ' + str(X.shape[0]),end='\r')
for j in range(i+1):
cov[i,j] = Lrelu_inner(X[i], X2[j])
cov += np.tril(cov,k=-1).T
return cov
def __Lrelu_kernel_pt(self, X, X2=None, a=0.2):
if X2 is not None:
X = torch.cat([X, X2], 0)
K = X @ X.T / X.shape[-1] * self.w_var_list[0] + self.b_var_list[0]
for i in range(1, len(self.w_var_list)):
K_diag_sqrt = K.diag().sqrt()
normalizer = K_diag_sqrt.view(-1, 1) @ K_diag_sqrt.view(1, -1)
Theta = ((K / normalizer).clamp_(max=1.)).arccos()
K = ((Theta.sin() + (np.pi-Theta) * Theta.cos()) * np.square(1-a)/(2*np.pi) + a * Theta.cos()) * normalizer * self.w_var_list[i] + self.b_var_list[i]
if X2 is not None:
return K[:-X2.shape[0], -X2.shape[0]:]
else:
return K
def __erf_kernel(self, X, X2=None):
# erf kernel from Williams 1996, eq. 11
def erf_inner(x,x2):
# actually these should be 1/d_in
k_x_x = 2*(self.b_var_list[0] + self.w_var_list[0]*(np.matmul(x,x.T))/d_in)
k_x2_x2 = 2*(self.b_var_list[0] + self.w_var_list[0]*(np.matmul(x2,x2.T))/d_in)
k_x_x2 = 2*(self.b_var_list[0] + self.w_var_list[0]*(np.matmul(x,x2.T))/d_in)
a = k_x_x2 / np.sqrt((1+k_x_x)*(1+k_x2_x2))
return self.b_var_list[1] + self.w_var_list[1]*2*np.arcsin(a)/np.pi
if X2 is None:
same_inputs=True
X2 = X
else:
same_inputs=False
cov = np.zeros([X.shape[0],X2.shape[0]])
d_in = X.shape[-1]
if not same_inputs:
for i in range(X.shape[0]):
for j in range(X2.shape[0]):
cov[i,j] = erf_inner(X[i],X2[j])
else:
for i in range(X.shape[0]):
for j in range(i+1):
cov[i,j] = erf_inner(X[i],X2[j])
# now just reflect - saves recomputing half the matrix
cov += np.tril(cov,k=-1).T
return cov
def __erf_kernel_pt(self, X, X2=None):
if X2 is not None:
X = torch.cat([X, X2], 0)
K = X @ X.T / X.shape[-1] * self.w_var_list[0] + self.b_var_list[0]
for i in range(1, len(self.w_var_list)):
K = K * 2
K_diag_sqrt = K.diag().add(1.).sqrt()
normalizer = K_diag_sqrt.view(-1, 1) @ K_diag_sqrt.view(1, -1)
Theta = ((K / normalizer).clamp_(max=1.)).arcsin()
K = Theta * 2 / np.pi * self.w_var_list[i] + self.b_var_list[i]
if X2 is not None:
return K[:-X2.shape[0], -X2.shape[0]:]
else:
return K
def make_erf_kernel_tf(self):
def __erf_kernel_tf(X, X2=None):
print(X, X2)
if X2 is not None:
X = tf.concat([X, X2], 0)
K = tf.matmul(X, X, transpose_b=True) / tf.cast(X.shape[-1], tf.float32) * self.w_var_list[0] + self.b_var_list[0]
for i in range(1, len(self.w_var_list)):
K = K * 2
K_diag_sqrt = (tf.linalg.tensor_diag_part(K) + 1.) ** 0.5
normalizer = tf.reshape(K_diag_sqrt, [-1, 1]) @ tf.reshape(K_diag_sqrt, [1, -1])
Theta = tf.math.asin(tf.clip_by_value(K / normalizer, clip_value_min=-1., clip_value_max=1.))
K = Theta * 2 / np.pi * self.w_var_list[i] + self.b_var_list[i]
if X2 is not None:
return tf.expand_dims(tf.linalg.tensor_diag_part(K[:X2.shape[0], X2.shape[0]:]), -1)
else:
return tf.expand_dims(tf.linalg.tensor_diag_part(K), -1)
return __erf_kernel_tf
@torch.no_grad()
def __call__(self, X, X2=None):
if self.kernel_type == 'relu':
return self.__relu_kernel_pt(X, X2)
elif 'lrelu' in self.kernel_type:
return self.__Lrelu_kernel_pt(X, X2, a=float(self.kernel_type.replace("lrelu", "")))
elif self.kernel_type == 'erf':
return self.__erf_kernel_pt(X, X2)
else:
raise NotImplementedError
if __name__ == '__main__':
test_mlp_nngpk = 1
if test_mlp_nngpk:
from utils import build_mlp_given_config, init_NN
w_var_list = [2., 2., 2.]
b_var_list = [1., 1., 1.]
X = torch.randn(128, 32)
X2 = torch.randn(64, 32)
for kernel_type in ['relu', 'lrelu0.2', 'erf']:
kernel = NNGPKernel(kernel_type=kernel_type, w_var_list=w_var_list, b_var_list=b_var_list)
k1_0 = kernel(X)
k2_0 = kernel(X, X2)
model = build_mlp_given_config(nonlinearity=kernel_type, input_size=X.shape[-1], hidden_size=16, output_size=1, bias=True, num_layers=len(w_var_list))
# print(model)
samples = []
with torch.no_grad():
for _ in range(100000):
# if _ % 100 == 0:
# print(_)
init_NN(model, w_var_list, b_var_list)
samples.append(model(torch.cat([X, X2])))
samples = torch.cat(samples, -1)
k1_1 = samples[:X.shape[0]] @ samples[:X.shape[0]].T / samples.shape[-1]
k2_1 = samples[:X.shape[0]] @ samples[X.shape[0]:].T / samples.shape[-1]
# print(k1_0.shape, k1_1.shape)
print(k1_0[:5, :5], k1_1[:5, :5])
print(torch.dist(k1_0, k1_1))
print(torch.dist(k2_0, k2_1))
else:
pass