-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathrbf.py
74 lines (47 loc) · 1.52 KB
/
rbf.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
from math import exp
import numpy as np
from scipy.interpolate import Rbf
# Naive python implementation of a Radial Basis Function (RBF) approximation scheme
def rbf_network(X, beta, theta):
N = X.shape[0]
D = X.shape[1]
Y = np.zeros(N)
for i in range(N):
for j in range(N):
r = 0
for d in range(D):
r += (X[j, d] - X[i, d]) ** 2
r = r**0.5
Y[i] += beta[j] * exp(-(r * theta)**2)
return Y
# Scipy implementation of a Radial Basis Function (RBF) approximation scheme
def rbf_scipy(X, beta):
N = X.shape[0]
D = X.shape[1]
rbf = Rbf(X[:,0], X[:,1], X[:,2], X[:,3], X[:, 4], beta)
#Xtuple = tuple([X[:, i] for i in range(D)])
Xtuple = tuple([X[:, i] for i in range(D)])
return rbf(*Xtuple)
# Cython implementation of a Radial Basis Function (RBF) approximation scheme
#
# TODO: Write the Cython implementation in a separate fastloop.pyx file, compile and import it here
#
# from fastloop import rbf_network_cython
# Make up some data
D = 5
N = 1000
X = np.array([np.random.rand(N) for d in range(D)]).T
beta = np.random.rand(N)
theta = 10
# Simple testing of the performance of the Python and Scipy implementations
import time
t0 = time.time()
rbf_network(X, beta, theta)
print("Python: ", time.time() - t0)
t0 = time.time()
rbf_scipy(X, beta)
print("Scipy: ", time.time() - t0)
# Testing the performance of Cython
#t0 = time.time()
#rbf_network_cython(X, beta, theta)
#print("Cython: ", time.time() - t0)