-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathrbf_network.py
96 lines (81 loc) · 3.38 KB
/
rbf_network.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
import numpy as np
import metrics
import matplotlib.pyplot as plt
import k_means_plus
import kernel
class RbfNet:
def __init__(self, debug=True, mode='classification'):
self.__debug = debug
self.__mode = mode
def fit(self, X, y, units, epochs, optimizer):
'''
Parameters
----------
X : shape (n_samples, n_features)
Training data
y : shape (n_samples,)
Target values, 1 or 0
epochs : The number of epochs
optimizer : Optimize algorithm, see also optimizer.py
units : The number of unit
'''
n_samples, n_features = X.shape
self.__units = units
model = k_means_plus.KMeansPlus()
model.fit(X, self.__units, 10)
self.__centers = model.centers
self.__sigmas = np.ones(self.__units)
self.__weights = np.random.randn(self.__units)
if self.__debug:
accuracy = []
loss = []
for _ in range(epochs):
outs = np.zeros((n_samples, self.__units))
for i in range(self.__units):
outs[:, i] = kernel.gaussian_kernel(X, self.__centers[i].reshape(1, -1), self.__sigmas[i])
h = outs.dot(self.__weights)
residual = h - y
g_centers = np.zeros_like(self.__centers)
g_sigmas = np.zeros_like(self.__sigmas)
g_weights = np.zeros_like(self.__weights)
for i in range(self.__units):
g_centers[i] = self.__weights[i] * np.mean((residual * outs[:, i]).reshape((-1, 1)) * (X - self.__centers[i]), axis=0) / (self.__sigmas[i] ** 2)
g_sigmas[i] = self.__weights[i] * np.mean(residual * outs[:, i] * (np.linalg.norm(X - self.__centers[i], axis=1) ** 2), axis=0) / (self.__sigmas[i] ** 3)
g_weights[i] = np.mean(residual * outs[:, i], axis=0)
g_centers, g_sigmas, g_weights = optimizer.optimize([g_centers, g_sigmas, g_weights])
self.__centers -= g_centers
self.__sigmas -= g_sigmas
self.__weights -= g_weights
if self.__debug:
h = self.score(X)
loss.append(np.mean((h - y) ** 2))
if self.__mode == 'classification':
accuracy.append(metrics.accuracy(y, np.around(h)))
if self.__debug:
_, ax_loss = plt.subplots()
ax_loss.plot(loss, 'b')
if self.__mode == 'classification':
ax_accuracy = ax_loss.twinx()
ax_accuracy.plot(accuracy, 'r')
plt.show()
def score(self, X):
n_samples, n_features = X.shape
outs = np.zeros((n_samples, self.__units))
for i in range(self.__units):
outs[:, i] = kernel.gaussian_kernel(X, self.__centers[i].reshape(1, -1), self.__sigmas[i])
return outs.dot(self.__weights)
def predict(self, X):
'''
Parameters
----------
X : shape (n_samples, n_features)
Predicting data
Returns
-------
y : shape (n_samples,)
Predicted class label per sample, 1 or 0
'''
if self.__mode == 'classification':
return np.around(self.score(X))
else:
return self.score(X)