forked from alesfav/convolutional_neural_kernels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel_functions.py
252 lines (184 loc) · 5.91 KB
/
kernel_functions.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
import math
import torch
import torch.nn.functional as F
def k0(t):
"""
Computes the zeroth order arc-cosine kernel.
Args:
t: cosines
"""
k0 = (math.pi - torch.arccos(torch.clamp(t, min=-1.0, max=1.0))) / math.pi
return k0
def k1(t):
"""
Computes the first order arc-cosine kernel.
Args:
t: cosines
"""
t = torch.clamp(t, min=-1.0, max=1.0)
k1 = (torch.sqrt(1.0 - t.pow(2)) + (math.pi - torch.arccos(t)) * t) / math.pi
return k1
def dntk(x, y, depth, normalize=False):
"""
Computes the NTK of a (deep) FCN without bias.
Args:
x: first input
y: second input
depth: network depth
normalize: If set to True normalise the inputs on a sphere.
Default = False
"""
if normalize:
x /= torch.norm(x, dim=-1, keepdim=True)
y /= torch.norm(y, dim=-1, keepdim=True)
cosines = torch.matmul(x, y.t())
ntk = cosines
for i in range(depth):
rfk = k1(cosines)
ntk = (k0(cosines) * ntk) + rfk
cosines = rfk
return ntk
def dcntk(x, y, filtersizes, normalize=False):
"""
Computes the NTK of a 1d hierarchical (L)CNN with non-overlapping patches.
Args:
x: first input
y: second input
filtersizes: list with filter sizes starting from first layet
normalize: If set to True normalise the input patches on a sphere.
Default = False
"""
d = x.size(-1)
assert y.size(-1) == d, "The inputs must have the same dimension"
prod = torch.tensor(filtersizes).prod().item()
assert d % prod == 0, "The filter sizes are incompatible with input dimension"
temp_x = [x.size(0)]
temp_y = [y.size(0)]
xpatch = F.unfold(
x.reshape(torch.tensor(temp_x).prod().item(), 1, 1, -1),
kernel_size=(1, prod),
dilation=1,
padding=0,
stride=prod,
).transpose(1, 2)
ypatch = F.unfold(
y.reshape(torch.tensor(temp_y).prod().item(), 1, 1, -1),
kernel_size=(1, prod),
dilation=1,
padding=0,
stride=prod,
).transpose(1, 2)
temp_x.append(d // prod)
temp_y.append(d // prod)
for filtersize in filtersizes[1:][::-1]:
prod //= filtersize
xpatch = F.unfold(
xpatch.reshape(torch.tensor(temp_x).prod().item(), 1, 1, -1),
kernel_size=(1, prod),
dilation=1,
padding=0,
stride=prod,
).transpose(1, 2)
ypatch = F.unfold(
ypatch.reshape(torch.tensor(temp_y).prod().item(), 1, 1, -1),
kernel_size=(1, prod),
dilation=1,
padding=0,
stride=prod,
).transpose(1, 2)
temp_x.append(filtersize)
temp_y.append(filtersize)
xpatch = xpatch.reshape(*temp_x, prod)
ypatch = ypatch.reshape(*temp_y, prod)
if normalize:
xpatch /= torch.norm(xpatch, dim=-1, keepdim=True)
ypatch /= torch.norm(ypatch, dim=-1, keepdim=True)
cosines = torch.matmul(
xpatch.permute(
*[i + 1 for i in range(len(filtersizes))], 0, len(filtersizes) + 1
),
ypatch.permute(
*[i + 1 for i in range(len(filtersizes))], len(filtersizes) + 1, 0
),
)
ntk = cosines
for _ in range(len(filtersizes)):
rfk = k1(cosines).mean(dim=-3)
ntk = (k0(cosines) * ntk).mean(dim=-3) + rfk
cosines = rfk
return ntk
def dcrfk(x, y, filtersizes, normalize=False):
"""
Computes the RFK (of NNGP kernel) of a 1d hierarchical (L)CNN with non-overlapping patches.
Args:
x: first input
y: second input
filtersizes: list with filter sizes starting from first layet
normalize: If set to True normalise the input patches on a sphere.
Default = False
"""
d = x.size(-1)
assert y.size(-1) == d, "The inputs must have the same dimension"
prod = torch.tensor(filtersizes).prod().item()
assert d % prod == 0, "The filter sizes are incompatible with input dimension"
temp_x = [x.size(0)]
temp_y = [y.size(0)]
xpatch = F.unfold(
x.reshape(torch.tensor(temp_x).prod().item(), 1, 1, -1),
kernel_size=(1, prod),
dilation=1,
padding=0,
stride=prod,
).transpose(1, 2)
ypatch = F.unfold(
y.reshape(torch.tensor(temp_y).prod().item(), 1, 1, -1),
kernel_size=(1, prod),
dilation=1,
padding=0,
stride=prod,
).transpose(1, 2)
temp_x.append(d // prod)
temp_y.append(d // prod)
for filtersize in filtersizes[1:][::-1]:
prod //= filtersize
xpatch = F.unfold(
xpatch.reshape(torch.tensor(temp_x).prod().item(), 1, 1, -1),
kernel_size=(1, prod),
dilation=1,
padding=0,
stride=prod,
).transpose(1, 2)
ypatch = F.unfold(
ypatch.reshape(torch.tensor(temp_y).prod().item(), 1, 1, -1),
kernel_size=(1, prod),
dilation=1,
padding=0,
stride=prod,
).transpose(1, 2)
temp_x.append(filtersize)
temp_y.append(filtersize)
xpatch = xpatch.reshape(*temp_x, prod)
ypatch = ypatch.reshape(*temp_y, prod)
if normalize:
xpatch /= torch.norm(xpatch, dim=-1, keepdim=True)
ypatch /= torch.norm(ypatch, dim=-1, keepdim=True)
cosines = torch.matmul(
xpatch.permute(
*[i + 1 for i in range(len(filtersizes))], 0, len(filtersizes) + 1
),
ypatch.permute(
*[i + 1 for i in range(len(filtersizes))], len(filtersizes) + 1, 0
),
)
for _ in range(len(filtersizes)):
rfk = k1(cosines).mean(dim=-3)
cosines = rfk
return rfk
def power_ntk(ntk, q):
"""
Computes the power of an NTK matrix elementwise.
Args:
ntk: ntk matrix
q: exponent
"""
return ntk.pow(q)