forked from PnYuan/Machine-Learning_ZhouZhihua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RBF_BP.py
164 lines (126 loc) · 4.49 KB
/
RBF_BP.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
# -*- coding: utf-8 -*
'''
@author: PY131, created on 17.4.29
this is an implementation of RBF network
'''
class RBP_network:
'''
the definition of BP network class
'''
def __init__(self):
'''
initial variables
'''
# node number each layer
# input neuron number equals to input variables' number
self.h_n = 0
# output layer contains only one neuron
# output value for each layer
self.b = [] # hidden layer
self.y = 0.0 # output
# parameters (w, b, c)
self.w = [] # weight of the link between hidden neuron and output neuron
self.beta = [] # scale index of Gaussian-RBF
self.c = [] # center of Gaussian-RBF]
# initial the learning rate
self.lr = 0.05
def CreateNN(self, nh, centers, learningrate):
'''
build a RBF network structure and initial parameters
@param nh : the neuron number of in layer
@param centers: matrix [h_n * i_n] the center parameters object to hidden layer neurons
@param learningrate: learning rate of gradient algorithm
'''
# dependent packages
import numpy as np
# assignment of hidden neuron number
self.h_n = nh
# initial value of output for each layer
self.b = np.zeros(self.h_n)
# self.y = 0.0
# initial centers
self.c = centers
# initial weights for each link (random initialization)
self.w = np.zeros(self.h_n)
self.beta = np.zeros(self.h_n)
for h in range(self.h_n):
self.w[h] = rand(0, 1)
self.beta[h] = rand(0, 1)
# initial learning rate
self.lr = learningrate
def Pred(self, x):
'''
predict process through the network
@param x: array, input array for input layer
@param y: float, output of the network
'''
self.y = 0.0
# activate hidden layer and calculating output
for h in range(self.h_n):
self.b[h] = RBF(x, self.beta[h], self.c[h])
self.y += self.w[h] * self.b[h]
return self.y
def Batch_Pred(self, X):
'''
predict process through the network for batch data
@param x: array, data set for input layer
@param y: array, output of the networks
'''
y_pred = []
# activate hidden layer and calculating output
for i in range(len(X)):
y_pred.append(self.Pred(X[i]))
return y_pred
def BackPropagateRBF(self, x, y):
'''
the implementation of special BP algorithm on one slide of sample for RBF network
@param x, y: array and float, input and output of the data sample
'''
# dependent packages
import numpy as np
# get current network output
self.Pred(x)
# calculate the gradient for hidden layer
g = np.zeros(self.h_n)
for h in range(self.h_n):
g[h] = (self.y - y) * self.b[h]
# updating the parameter
for h in range(self.h_n):
self.beta[h] += self.lr * g[h] * self.w[h] * np.linalg.norm(x-self.c[h],2)
self.w[h] -= self.lr * g[h]
def TrainRBF(self, data_in, data_out):
'''
BP training for RBF network
@param data_in, data_out:
@return e: accumulated error
@return e_k: error array based on each step
'''
e_k = []
for k in range(len(data_in)):
x = data_in[k]
y = data_out[k]
self.BackPropagateRBF(x, y)
# error in train set for each step
y_delta2 = (self.y - y)**2
e_k.append(y_delta2/2)
# total error of training
e = sum(e_k)/len(e_k)
return e, e_k
def RBF(x, beta, c):
'''
the definition of radial basis function (RBF)
@param x: array, input variable
@param beta: float, scale index
@param c: array. center
'''
# dependent packages
from numpy.linalg import norm
return norm(x-c,2)
def rand(a, b):
'''
the definition of random function
@param a,b: the upper and lower limitation of the random value
'''
# dependent packages
from random import random
return (b - a) * random() + a