-
Notifications
You must be signed in to change notification settings - Fork 2
/
RBF.py
144 lines (117 loc) · 4.79 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
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
#Chimdindu Orakwue
import numpy as np
#this three class structure will work as a RBF neural network as long as there is one input layer, one hidden layer, and an output
#layer made up of one output neuron
#the Gaussian function is the output function the neuron inputs are passed through
class Neuron:
#constructor
def __init__(self, funcValues):
self.inputs = 0
self.funcValues = list(funcValues)
#adds input to neuron
def addInput(self, value):
self.inputs = self.inputs + value
#Gaussian RBF
def radialBasisFunction(self, x, r, c):
return np.exp(-1 * (((x - c) ** 2) / r**2))
#sigmoid activation function
def sigmoid(self, x, deriv=False):
if(deriv==True):
return x*(1-x)
return 1/(1+np.exp(-x))
#return sigmoidOutput
def outputSigmoid(self):
returnValue = self.sigmoid(self.inputs)
self.inputs = 0
return returnValue
#return RBF output
def outputRBF(self):
x = self.inputs
r = self.funcValues[0]
c = self.funcValues[2]
returnValue = self.radialBasisFunction(x, r, c)
self.inputs = 0
#print("neuron RBF: " + str(returnValue))
return self.funcValues[1] * returnValue
#returns non activated output
def nonOutput(self):
if sum(self.funcValues) != 0:
#print(sum(self.funcValues))
#print("----------")
returnValue = self.inputs * self.funcValues[1]
else:
returnValue = self.inputs
if self.inputs == -1:
self.inputs = -1
else:
self.inputs = 0
#print("bias and input: " + str(returnValue))
return returnValue
class NeuronLayer:
#constructor
def __init__(self, funcValues, numNeurons, includeBias):
self.funcValues = list(funcValues)
self.numNeurons = numNeurons
self.neurons = []
self.createLayer(includeBias)
#create neuron layer
def createLayer(self, includeBias = True):
#includes a bias neuron if includeBias = True
if (includeBias):
if (sum(self.funcValues) != 0):
bias = Neuron([0, self.funcValues[0]])
else:
bias = Neuron([])
bias.addInput(-1)
self.neurons.append(bias)
if len(self.funcValues) >= 2:
for i in range(1, self.numNeurons + 1):
neuronValues = []
neuronValues.append(self.funcValues[len(self.funcValues) - 1])
neuronValues.append(self.funcValues[i])
neuronValues.append(self.funcValues[i + 1])
self.neurons.append(Neuron(neuronValues))
else:
for i in range(0, self.numNeurons):
self.neurons.append(Neuron([]))
class NeuralNetwork:
#constructor
def __init__(self, funcValues, numInputs, numHiddenNeurons):
self.funcValues = list(funcValues)
self.numInputs = numInputs
self.numHiddenNeurons = numHiddenNeurons
self.layers = []
self.createLayers()
#creates neuron layers
def createLayers(self):
self.layers.append(NeuronLayer([], self.numInputs, True))
self.layers.append(NeuronLayer(self.funcValues, self.numHiddenNeurons, True))
self.layers.append(NeuronLayer([], 1, False))
#neural network output
def output(self, inputs = []):
inputLayer = self.layers[0]
hiddenLayer = self.layers[1]
outputLayer = self.layers[2]
#assigns inputs
for i in range(0, len(inputs)):
inputLayer.neurons[i + 1].addInput(inputs[i])
#input layer to hidden layer
for i in range(0, len(inputLayer.neurons)):
value = inputLayer.neurons[i].nonOutput()
for j in range(1, len(hiddenLayer.neurons)):
hiddenLayer.neurons[j].addInput(value)
#adding bias output to output layer
hiddenBias = hiddenLayer.neurons[0]
outputNeuron = outputLayer.neurons[0]
toAdd = hiddenBias.nonOutput()
#print("HIDDEN BIAS: " + str(toAdd))
outputNeuron.addInput(toAdd)
#rest of hidden output to output layer
for i in range(1, len(hiddenLayer.neurons)):
value = hiddenLayer.neurons[i].outputRBF()
#print("HIDDEN NEURON: " + str(value))
outputNeuron.addInput(value)
#return output of output layer
returnValue = outputNeuron.nonOutput()
#print("RBF return: " + str(returnValue))
return returnValue