-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNNetwork.java
More file actions
382 lines (314 loc) · 12.7 KB
/
Copy pathNNetwork.java
File metadata and controls
382 lines (314 loc) · 12.7 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class NNetwork {
public int numInputDimensions;
public int numInputNodes;
public int numHiddenNodes;
public int numOutputNodes;
public double[][] weightsInputHidden; //store the weights from input nodes to hidden nodes
public double[][] weightsHiddenOutput; //store the weights from hidden nodes to output nodes
public double[][] previousWeightChangeIH; //to store deltas at time t so we can use them with momentum in update at t+1
public double[][] previousWeightChangeHO;
public double[] hiddenLayerInputs; //store the net input to each hidden node
public double[] outputLayerInputs; //store net input to each output node
public double[] inputLayerOutputs; //store output
public double[] hiddenLayerOutputs;
public double[] outputLayerOutputs;
public double[] outputLayerDeltas;
public double[] hiddenLayerDeltas;
public double learningRate;
public double momentum;
public int maxEpochs;
public double[][] currentTrainingOutputs;
public int numTrainingPatterns;
public int numTestPatterns;
public double[][] trainingInputs;
public double[][] targetTrainingOutputs;
public double[][] testInputs;
public double[][] testOutputs;
public static void main(String[] args) throws IOException {
NNetwork nn = new NNetwork(64, 50, 10, 0.15, 0.8, 6000, 3823);
//Training phase
nn.readTrainingData();
nn.train();
//Testing phase
nn.readTestData();
int correct = nn.test();
//Report network accuracy
System.out.println("Classification accuracy (test): " + (correct*100.0/nn.testInputs.length) + "%\nCorrect: " + correct + "\nIncorrect: " + (nn.testInputs.length - correct));
}
//Initialise values in constructor
public NNetwork(int inputDimensions, int hiddenNodes, int outputNodes, double lr, double momentum, int maxEpochs, int numTrainingInputs) {
this.numInputDimensions = inputDimensions;
this.numInputNodes = this.numInputDimensions+1; //for bias node
this.numHiddenNodes = hiddenNodes;
this.numOutputNodes = outputNodes;
this.learningRate = lr;
this.maxEpochs = maxEpochs;
this.weightsInputHidden = new double[numInputNodes][numHiddenNodes]; //accounts for the added bias weight to each hidden node
this.weightsHiddenOutput = new double[numHiddenNodes+1][numOutputNodes]; //to account for the added bias weight to each output node
this.hiddenLayerInputs = new double[numHiddenNodes];
this.outputLayerInputs = new double[numOutputNodes];
this.inputLayerOutputs = new double[numInputNodes];
this.hiddenLayerOutputs = new double[numHiddenNodes];
this.outputLayerOutputs = new double[numOutputNodes];
this.outputLayerDeltas = new double[numOutputNodes];
this.hiddenLayerDeltas = new double[numHiddenNodes];
this.previousWeightChangeIH = new double[numInputNodes][numHiddenNodes];
this.previousWeightChangeHO = new double[numHiddenNodes][numOutputNodes];
this.numTrainingPatterns = numTrainingInputs;
this.trainingInputs = new double[numTrainingPatterns][numInputNodes];
this.targetTrainingOutputs = new double[numTrainingPatterns][numOutputNodes];
this.currentTrainingOutputs = new double[numTrainingPatterns][numOutputNodes];
initialiseWeights();
System.out.println("Network created!");
}
//Read in training data from file
public void readTrainingData() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("training.txt"));
ArrayList<String> dataPoints = new ArrayList<String>();
String s;
while ((s = br.readLine()) != null) { //read in data line by line
dataPoints.add(s);
}
br.close();
for (int i = 0; i < this.numTrainingPatterns; i++) {
String[] points = dataPoints.get(i).split(",");
if (points.length != 65) {
System.out.println("WARNING: Data point does not have 65 dimensions!");
}
for (int j = 0; j < points.length-2; j++) {
this.trainingInputs[i][j] = Integer.parseInt(points[j]);
}
this.trainingInputs[i][points.length-1] = 1; //bias entry
for (int k = 0; k < this.numOutputNodes; k++) {
int outputClass = Integer.parseInt(points[points.length-1]);
if (k==outputClass) {
this.targetTrainingOutputs[i][k] = 0.9; //since we're using a sigmoid function we need approximate values
} else {
this.targetTrainingOutputs[i][k] = 0.1;
}
}
}
}
//Read in test data from file
public void readTestData() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("testing.txt"));
ArrayList<String> dataPoints = new ArrayList<String>();
String s;
while ((s = br.readLine()) != null) { //read in data line by line
dataPoints.add(s);
}
br.close();
this.testInputs = new double[dataPoints.size()][numInputNodes];
this.testOutputs = new double[dataPoints.size()][numOutputNodes];
for (int i = 0; i < dataPoints.size(); i++) {
String[] points = dataPoints.get(i).split(",");
if (points.length != 65) {
System.out.println("WARNING: Data point does not have 65 dimensions!");
}
for (int j = 0; j < points.length-2; j++) {
this.testInputs[i][j] = Integer.parseInt(points[j]);
}
this.testInputs[i][points.length-1] = 1; //bias entry
for (int k = 0; k < this.numOutputNodes; k++) {
int outputClass = Integer.parseInt(points[points.length-1]);
if (k==outputClass) {
this.testOutputs[i][k] = 0.9; //since we're using a sigmoid function we need approximate values
} else {
this.testOutputs[i][k] = 0.1;
}
}
}
}
//Initialise weights to random values bounded between 0.5 and -0.5
public void initialiseWeights() {
//initialise input to hidden layer weights
for (int i = 0; i < this.numInputNodes; i++) {
for (int j = 0; j < this.numHiddenNodes; j++) {
this.weightsInputHidden[i][j] = Math.random() - 0.5; //return values between -0.5 and 0.5
// System.out.println("Weight from input node " + i + " to hidden node " + j + ": " + this.weightsInputHidden[i][j]);
}
}
//initialise hidden to output layer weights
for (int i = 0; i < this.numHiddenNodes; i++) {
for (int j = 0; j < this.numOutputNodes; j++) {
this.weightsHiddenOutput[i][j] = Math.random() - 0.5; //return values between -0.5 and 0.5
// System.out.println("Weight from hidden node " + i + " to output node " + j + ": " + this.weightsHiddenOutput[i][j]);
}
}
System.out.println("Weights initialised.");
}
//Training method
public void train() {
double mse = Integer.MAX_VALUE;
int epoch = 0;
double[] pattern;
double[] desiredOutput;
do {
epoch++;
for (int i = 0; i < this.numTrainingPatterns; i++) {
pattern = this.trainingInputs[i];
desiredOutput = this.targetTrainingOutputs[i];
this.currentTrainingOutputs[i] = feedforward(pattern);
backpropagate(pattern, desiredOutput, this.currentTrainingOutputs[i]);
// printNetworkState();
}
if (epoch % 100 == 0) {
System.out.println("Epoch: " + epoch);
mse = computeMSE();
System.out.println("MSE: " + mse);
}
// printNetworkState();
} while (mse > 0.0008 && epoch < this.maxEpochs);
System.out.println("Network trained. Calculating classification accuracy...");
//calculate # correct
int correct = calculateClassificationAccuracy();
System.out.println("Classification accuracy (training): " + (correct*100.0/this.numTrainingPatterns) + "%\nCorrect: " + correct + "\nIncorrect: " + (this.numTrainingPatterns - correct));
}
public double[] feedforward(double[] pattern) {
//assign outputs of input layer
for (int i = 0; i < this.numInputNodes; i++) {
this.inputLayerOutputs[i] = pattern[i];
}
//assign inputs of hidden layer
for (int i = 0; i < this.numHiddenNodes; i++) {
double sum = 0;
for (int j = 0; j < this.numInputNodes; j++) {
sum += this.weightsInputHidden[j][i] * this.inputLayerOutputs[j];
}
this.hiddenLayerInputs[i] = sum;
}
//work out outputs of hidden layer
for (int i = 0; i < this.numHiddenNodes; i++) {
this.hiddenLayerOutputs[i] = sigmoid(this.hiddenLayerInputs[i]);
}
//assign inputs of output layer
for (int i = 0; i < this.numOutputNodes; i++) {
double sum = 0;
for (int j = 0; j < this.numHiddenNodes; j++) {
sum += this.weightsHiddenOutput[j][i] * this.hiddenLayerOutputs[j];
}
this.outputLayerInputs[i] = sum;
}
//work out output of output layer
for (int i = 0; i < this.numOutputNodes; i++) {
this.outputLayerOutputs[i] = sigmoid(this.outputLayerInputs[i]);
}
return this.outputLayerOutputs;
}
public double sigmoid(double input) {
return 1 / (1 + Math.pow(Math.E, -input));
}
public void backpropagate(double[] pattern, double[] targetOutput, double[] actualOutput) {
double error;
double sum;
//work out the error and deltas of each output node
for (int i = 0; i < this.numOutputNodes; i++) {
if (targetOutput[i] == 0.9 && actualOutput[i] >= 0.9) {
error = 0;
} else if (targetOutput[i] == 0.1 && actualOutput[i] <= 0.1) {
error = 0;
} else {
error = targetOutput[i] - actualOutput[i];
}
this.outputLayerDeltas[i] = error * actualOutput[i] * (1-actualOutput[i]);
}
//adjust the weights from hidden layer to output layer using momentum
double change;
for (int out = 0; out < this.numOutputNodes; out++) {
for (int hid = 0; hid < this.numHiddenNodes; hid++) {
change = (this.learningRate * this.hiddenLayerOutputs[hid] * this.outputLayerDeltas[out]) + (this.momentum * this.previousWeightChangeHO[hid][out]);
this.weightsHiddenOutput[hid][out] += change;
this.previousWeightChangeHO[hid][out] = change;
}
this.weightsHiddenOutput[this.numHiddenNodes][out] += this.learningRate * this.outputLayerDeltas[out]; //update bias
}
//work out deltas of hidden layer nodes
for (int i = 0; i < this.numHiddenNodes; i++) {
sum = 0;
for (int j = 0; j < this.numOutputNodes; j++) {
sum += this.outputLayerDeltas[j] * this.weightsHiddenOutput[i][j];
}
error = sum * this.hiddenLayerOutputs[i] * (1 - this.hiddenLayerOutputs[i]);
this.hiddenLayerDeltas[i] = error;
}
//adjust weights from input to hidden layer
for (int hid = 0; hid < this.numHiddenNodes; hid++) {
for (int in = 0; in < this.numInputNodes; in++) {
change = (this.learningRate * this.hiddenLayerDeltas[hid] * this.inputLayerOutputs[in]) + (this.momentum * this.previousWeightChangeIH[in][hid]);
this.weightsInputHidden[in][hid] += change;
this.previousWeightChangeIH[in][hid] = change;
}
this.weightsInputHidden[this.numInputNodes-1][hid] += this.learningRate * this.hiddenLayerDeltas[hid]; //update bias
}
}
public double computeMSE() {
double sum = 0;
for (int i = 0; i < this.numTrainingPatterns; i++) {
double[] output = feedforward(this.trainingInputs[i]);
for (int j = 0; j < output.length; j++) {
if (this.targetTrainingOutputs[i][j] == 0.9 && output[j] >= 0.9) {
sum += 0;
} else if (this.targetTrainingOutputs[i][j] == 0.1 && output[j] <= 0.1) {
sum += 0;
} else {
sum += Math.pow((this.targetTrainingOutputs[i][j]-output[j]), 2);
}
}
}
return sum / this.numTrainingPatterns;
}
public int calculateClassificationAccuracy() {
int correct = 0;
for (int i = 0; i < this.numTrainingPatterns; i++) {
double[] output = feedforward(this.trainingInputs[i]);
double[] target = this.targetTrainingOutputs[i];
int outputMax = findMaxIndex(output);
int targetMax = findMaxIndex(target);
boolean allCorrect = true;
for (int j = 0; j < this.numOutputNodes; j++) {
if (target[j] == 0.9 && output[j] >= 0.9) {
//correct
} else if (target[j] == 0.1 && output[j] <= 0.1) {
//correct
} else {
allCorrect = false;
}
}
if (allCorrect) {
correct++;
}
System.out.println("Training pattern " + (i+1) + ": Target class - " + targetMax + " Network class - " + outputMax);
}
return correct;
}
//Test method
public int test() {
int correct = 0;
for (int i = 0; i < this.testInputs.length; i++) {
double[] output = feedforward(this.testInputs[i]);
double[] target = this.testOutputs[i];
int outputMax = findMaxIndex(output);
int targetMax = findMaxIndex(target);
if (output[outputMax] >= 0.9 && outputMax==targetMax) {
correct++;
}
System.out.println("Test pattern " + (i+1) + ": Target class - " + targetMax + " Predicted class - " + outputMax);
}
return correct;
}
private int findMaxIndex(double[] v) {
int index = 0;
double max = 0;
for (int i = 0; i < v.length; i++) {
if (v[i] > max) {
max = v[i];
index = i;
}
}
return index;
}
}