-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathNeuralNet.pde
More file actions
265 lines (200 loc) · 8.49 KB
/
NeuralNet.pde
File metadata and controls
265 lines (200 loc) · 8.49 KB
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
class NeuralNet {
int iNodes;//No. of input nodes
int hNodes;//No. of hidden nodes
int oNodes;//No. of output nodes
int hLayers;//No. of hidden layers
Matrix whi;//matrix containing weights between the input nodes and the hidden nodes
Matrix whh;//matrix containing weights between the hidden nodes and the second layer hidden nodes
Matrix woh;//matrix containing weights between the second hidden layer nodes and the output nodes
Matrix weights[];//matrix containing all weights between the layers
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//constructor
NeuralNet(int inputs, int hiddenNo, int outputNo) {
//set dimensions from parameters
iNodes = inputs;
oNodes = outputNo;
hNodes = hiddenNo;
//create first layer weights
//included bias weight
whi = new Matrix(hNodes, iNodes +1);
//create second layer weights
//include bias weight
whh = new Matrix(hNodes, hNodes +1);
//create second layer weights
//include bias weight
woh = new Matrix(oNodes, hNodes +1);
//set the matricies to random values
whi.randomize();
whh.randomize();
woh.randomize();
}
//Constructor
NeuralNet(int inputs, int hiddenNo; int outputNo, int hiddenLayers)
{
//Set dimensions from parameters
iNodes = inputs;
oNodes = outputNo;
hNodes = hiddenNo;
hLayers = hiddenLayers
//create first layer weights(input layer)
//included bias weight
weights[0] = new Matrix(hNodes, iNodes+1);
//create second layer weights(hidden layers)
//bias included for each hidden layer in for loop
for(int i = 1; i < hLayers; i++)
{
weights[i] = new Matrix(hNodes, hNodes +1)
}
//create third layer weights(output layer)
//included bias weight
weights[weights.length-1] = new Matrix(oNodes, hNodes +1);
//set the matricies to random values
weights[0].randomize();
for(int i=0; i < hLayers; i++)
{
weights[i].randomize();
}
weights[weights.length-1].randomize();
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//mutation function for genetic algorithm
void mutate(float mr) {
//mutates each weight matrix
whi.mutate(mr);
whh.mutate(mr);
woh.mutate(mr);
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//calculate the output values by feeding forward through the neural network
float[] output(float[] inputsArr) {
//convert array to matrix
//Note woh has nothing to do with it its just a function in the Matrix class
Matrix inputs = woh.singleColumnMatrixFromArray(inputsArr);
//add bias
Matrix inputsBias = inputs.addBias();
//-----------------------calculate the guessed output
//apply layer one weights to the inputs
Matrix hiddenInputs = whi.dot(inputsBias);
//pass through activation function(sigmoid)
Matrix hiddenOutputs = hiddenInputs.activate();
//add bias
Matrix hiddenOutputsBias = hiddenOutputs.addBias();
//apply layer two weights
Matrix hiddenInputs2 = whh.dot(hiddenOutputsBias);
Matrix hiddenOutputs2 = hiddenInputs2.activate();
Matrix hiddenOutputsBias2 = hiddenOutputs2.addBias();
//apply level three weights
Matrix outputInputs = woh.dot(hiddenOutputsBias2);
//pass through activation function(sigmoid)
Matrix outputs = outputInputs.activate();
//convert to an array and return
return outputs.toArray();
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//calculate the output values by feeding forward through the deep neural network
float[] output_dnn(float[] inputsArr) {
//convert array to matrix
//Note weights[0] has nothing to do with it its just a function in the Matrix class
Matrix inputs = weights[0].singleColumnMatrixFromArray(inputsArr);
//add bias
Matrix inputsBias = inputs.addBias();
//-----------------------calculate the guessed output
//apply layer one weights to the inputs
Matrix hiddenInputs = weights[0].dot(inputsBias);
//pass through activation function(sigmoid)
Matrix hiddenOutputs = hiddenInputs.activate();
//add bias
Matrix hiddenOutputsBias = hiddenOutputs.addBias();
//apply hidden layer two weights
for(int i = 1; i < hLayers; i++){
Matrix hiddenInputs2 = weights[i].dot(hiddenOutputsBias);
Matrix hiddenOutputs2 = hiddenInputs2.activate();
Matrix hiddenOutputsBias2 = hiddenOutputs2.addBias();
}
//apply level three weights
Matrix outputInputs = weights[weights.length-1].dot(hiddenOutputsBias2);
//pass through activation function(sigmoid)
Matrix outputs = outputInputs.activate();
//convert to an array and return
return outputs.toArray();
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//crossover function for genetic algorithm
NeuralNet crossover(NeuralNet partner) {
//creates a new child with layer matrices from both parents
NeuralNet child = new NeuralNet(iNodes, hNodes, oNodes);
child.whi = whi.crossover(partner.whi);
child.whh = whh.crossover(partner.whh);
child.woh = woh.crossover(partner.woh);
return child;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//return a neural net which is a clone of this Neural net
NeuralNet clone() {
NeuralNet clone = new NeuralNet(iNodes, hNodes, oNodes);
clone.whi = whi.clone();
clone.whh = whh.clone();
clone.woh = woh.clone();
return clone;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//converts the weights matrices to a single table
//used for storing the snakes brain in a file
Table NetToTable() {
//create table
Table t = new Table();
//convert the matricies to an array
float[] whiArr = whi.toArray();
float[] whhArr = whh.toArray();
float[] wohArr = woh.toArray();
//set the amount of columns in the table
for (int i = 0; i< max(whiArr.length, whhArr.length, wohArr.length); i++) {
t.addColumn();
}
//set the first row as whi
TableRow tr = t.addRow();
for (int i = 0; i< whiArr.length; i++) {
tr.setFloat(i, whiArr[i]);
}
//set the second row as whh
tr = t.addRow();
for (int i = 0; i< whhArr.length; i++) {
tr.setFloat(i, whhArr[i]);
}
//set the third row as woh
tr = t.addRow();
for (int i = 0; i< wohArr.length; i++) {
tr.setFloat(i, wohArr[i]);
}
//return table
return t;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//takes in table as parameter and overwrites the matrices data for this neural network
//used to load snakes from file
void TableToNet(Table t) {
//create arrays to tempurarily store the data for each matrix
float[] whiArr = new float[whi.rows * whi.cols];
float[] whhArr = new float[whh.rows * whh.cols];
float[] wohArr = new float[woh.rows * woh.cols];
//set the whi array as the first row of the table
TableRow tr = t.getRow(0);
for (int i = 0; i< whiArr.length; i++) {
whiArr[i] = tr.getFloat(i);
}
//set the whh array as the second row of the table
tr = t.getRow(1);
for (int i = 0; i< whhArr.length; i++) {
whhArr[i] = tr.getFloat(i);
}
//set the woh array as the third row of the table
tr = t.getRow(2);
for (int i = 0; i< wohArr.length; i++) {
wohArr[i] = tr.getFloat(i);
}
//convert the arrays to matrices and set them as the layer matrices
whi.fromArray(whiArr);
whh.fromArray(whhArr);
woh.fromArray(wohArr);
}
}