-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbf_functions.py
310 lines (233 loc) · 7.54 KB
/
rbf_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
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
import itertools
# pip install platypus
from platypus import Real
import numpy as np
import numba
def squared_exponential_rbf(rbf_input, centers, radii, weights):
"""
Parameters
----------
rbf_input : numpy array
1-D, shape is (n_inputs,)
centers : numpy array
2-D, shape is (n_rbfs X n_inputs)
radii : 2-D, shape is (n_rbfs X n_inputs)
weights : 2-D, shape is (n_rbfs X n_outputs)
Returns
-------
numpy array
"""
# sum over inputs
a = rbf_input[np.newaxis, :] - centers #(rbf_input[np.newaxis])-centre
#the np.newaxis function creates a new array, converting the 1D array into
#a 2D array, thus making the subtraction possible
b = a ** 2
c = radii ** 2
rbf_scores = np.exp(-(np.sum(b / c, axis=1))) #exp(-b/c)
# n_rbf x n_output, n_rbf
weighted_rbfs = weights * rbf_scores[:, np.newaxis]
output = weighted_rbfs.sum(axis=0)
return output
def gaussian_rbf(rbf_input, centers, radii, weights):
"""
Parameters
----------
rbf_input : numpy array
1-D, shape is (n_inputs,)
centers : numpy array
2-D, shape is (n_rbfs X n_inputs)
radii : 2-D, shape is (n_rbfs X n_inputs)
weights : 2-D, shape is (n_rbfs X n_outputs)
Returns
-------
numpy array
"""
a = rbf_input[np.newaxis, :] - centers
n = a / radii
p = n ** 2
q = np.sum(p, axis=1)
rbf_scores = np.exp(-1 * q)
# n_rbf x n_output, n_rbf
weighted_rbfs = weights * rbf_scores[:, np.newaxis]
output = weighted_rbfs.sum(axis=0)
return output
def multiquadric_rbf(rbf_input, centers, radii, weights):
"""
Parameters
----------
rbf_input : numpy array
1-D, shape is (n_inputs,)
centers : numpy array
2-D, shape is (n_rbfs X n_inputs)
radii : 2-D, shape is (n_rbfs X n_inputs)
weights : 2-D, shape is (n_rbfs X n_outputs)
Returns
-------
numpy array
"""
a = rbf_input[np.newaxis, :] - centers
b = a / radii
c = b ** 2
d = np.sum(c, axis=1)
rbf_scores = np.sqrt(1 + d)
weighted_rbfs = weights * rbf_scores[:, np.newaxis]
output = weighted_rbfs.sum(axis=0)
return output
def inverse_quadric_rbf(rbf_input, centers, radii, weights):
"""
Parameters
----------
rbf_input : numpy array
1-D, shape is (n_inputs,)
centers : numpy array
2-D, shape is (n_rbfs X n_inputs)
radii : 2-D, shape is (n_rbfs X n_inputs)
weights : 2-D, shape is (n_rbfs X n_outputs)
Returns
-------
numpy array
"""
a = rbf_input[np.newaxis, :] - centers
b = a / radii
c = b ** 2
d = np.sum(c, axis=1)
rbf_scores = 1 / (1 + d)
weighted_rbfs = weights * rbf_scores[:, np.newaxis]
output = weighted_rbfs.sum(axis=0)
return output
def inverse_multiquadric_rbf(rbf_input, centers, radii, weights):
"""
Parameters
----------
rbf_input : numpy array
1-D, shape is (n_inputs,)
centers : numpy array
2-D, shape is (n_rbfs X n_inputs)
radii : 2-D, shape is (n_rbfs X n_inputs)
weights : 2-D, shape is (n_rbfs X n_outputs)
Returns
-------
numpy array
"""
a = rbf_input[np.newaxis, :] - centers
b = (a / radii) ** 2
rbf_scores = 1 / np.sqrt(1 + np.sum(b, axis=1))
weighted_rbfs = weights * rbf_scores[:, np.newaxis]
output = weighted_rbfs.sum(axis=0)
return output
def exponential_rbf(rbf_input, centers, radii, weights):
"""
Parameters
----------
rbf_input : numpy array
1-D, shape is (n_inputs,)
centers : numpy array
2-D, shape is (n_rbfs X n_inputs)
radii : 2-D, shape is (n_rbfs X n_inputs)
weights : 2-D, shape is (n_rbfs X n_outputs)
Returns
-------
numpy array
"""
a = rbf_input[np.newaxis, :] - centers
b = (a / radii) ** 2
rbf_scores = np.exp(-1 * np.sum(b, axis=1))
weighted_rbfs = weights * rbf_scores[:, np.newaxis]
output = weighted_rbfs.sum(axis=0)
return output
def multi_quadric2_rbf(rbf_input, centers, radii, weights):
"""
Parameters
----------
rbf_input : numpy array
1-D, shape is (n_inputs,)
centers : numpy array
2-D, shape is (n_rbfs X n_inputs)
radii : 2-D, shape is (n_rbfs X n_inputs)
weights : 2-D, shape is (n_rbfs X n_outputs)
Returns
-------
numpy array
"""
rbf_scores = np.sqrt(np.sum((radii ** 2) + ((rbf_input - centers) ** 2),
axis=1))
weighted_rbfs = weights * rbf_scores[:, np.newaxis]
output = weighted_rbfs.sum(axis=0)
return output
def matern32_rbf(rbf_input, centers, radii, weights):
"""
Parameters
----------
rbf_input : numpy array
1-D, shape is (n_inputs,)
centers : numpy array
2-D, shape is (n_rbfs X n_inputs)
radii : 2-D, shape is (n_rbfs X n_inputs)
weights : 2-D, shape is (n_rbfs X n_outputs)
Returns
-------
numpy array
"""
diff = rbf_input - centers
squared = (diff / radii) ** 2
sqrt = np.sqrt(3 * np.sum(squared, axis=1))
rbf_scores = (1 + sqrt) * (np.exp(-sqrt))
weighted_rbfs = weights * rbf_scores[:, np.newaxis]
output = weighted_rbfs.sum(axis=0)
return output
rbfs = [squared_exponential_rbf,
gaussian_rbf,
multiquadric_rbf,
inverse_multiquadric_rbf,
inverse_quadric_rbf,
exponential_rbf,
multi_quadric2_rbf,
matern32_rbf]
class RBF:
def __init__(self, n_rbfs, n_inputs, n_outputs,
rbf_function=squared_exponential_rbf):
self.n_rbfs = n_rbfs
self.n_inputs = n_inputs
self.n_outputs = n_outputs
self.rbf = rbf_function
types = []
c_i = []
r_i = []
w_i = []
count = itertools.count()
for i in range(self.n_rbfs):
for j in range(self.n_inputs):
types.append(Real(-1, 1)) # center
c_i.append(next(count))
types.append(Real(0, 1)) # radius
r_i.append(next(count))
for _ in range(self.n_rbfs):
for _ in range(self.n_outputs):
types.append(Real(0, 1)) # weight
w_i.append(next(count)) # weight
self.platypus_types = types
self.c_i = np.asarray(c_i, dtype=np.int)
self.r_i = np.asarray(r_i, dtype=np.int)
self.w_i = np.asarray(w_i, dtype=np.int)
self.centers = None
self.radii = None
self.weights = None
def set_decision_vars(self, decision_vars):
decision_vars = decision_vars.copy()
self.centers = decision_vars[self.c_i].reshape((self.n_rbfs,
self.n_inputs))
self.radii = decision_vars[self.r_i].reshape((self.n_rbfs,
self.n_inputs))
self.weights = decision_vars[self.w_i].reshape((self.n_rbfs,
self.n_outputs))
# sum of weights per input is 1
self.weights /= self.weights.sum(axis=0)[np.newaxis, :]
def apply_rbfs(self, inputs):
outputs = self.rbf(inputs, self.centers, self.radii, self.weights)
return outputs
# # @numba.jit
# def format_output(output, weights):
# a = weights * output[:, np.newaxis] # n_rbf x n_output, n_rbf
# b = a.sum(axis=1)
#
# return b