-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuralNet.java
More file actions
101 lines (84 loc) · 3.23 KB
/
NeuralNet.java
File metadata and controls
101 lines (84 loc) · 3.23 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
import java.util.ArrayList;
import java.util.List;
/**
* Created by Srikanth on 2/27/2017.
*/
public class NeuralNet {
private List<Layer> layers;
public NeuralNet() {
layers = new ArrayList<>();
}
public void initializeNetwork() {
// create input layer
Layer inputLayer = new Layer(null, LayerType.INPUT);
boolean validity = inputLayer.initializeInputLayer();
if (!validity) {
System.out.println("Data is not loaded properly. Please load data for processing");
return;
}
layers.add(inputLayer);
// create hidden layer
for (int i = 0; i < ApplicationRunner.getHiddenLayerCount(); i++) {
Layer hiddenLayer = new Layer(layers.get(layers.size() - 1), LayerType.HIDDEN);
hiddenLayer.initializeHiddenLayer();
layers.add(hiddenLayer);
}
// create output layer
Layer outputLayer = new Layer(layers.get(layers.size() - 1), LayerType.OUTPUT);
outputLayer.initializeOutputLayer();
layers.add(outputLayer);
}
public void train() {
int errorCount = 0;
for (int i = 0; i < ApplicationRunner.getTrainingSetLimit(); i++) {
performForwardPass(i);
errorCount += calculateError(i);
performBackwardPass();
}
double trainingAccuracy = (errorCount * 100.0) / (ApplicationRunner.getTrainingSetLimit());
System.out.println("Training Percentage = " + trainingAccuracy);
}
public void test() {
int errorCount = 0;
for (int i = ApplicationRunner.getTrainingSetLimit(); i < ApplicationRunner.getData().dataValues.size(); i++) {
performForwardPass(i);
errorCount += calculateError(i);
}
double testingAccuracy = (errorCount * 100.0) /
(ApplicationRunner.getData().dataValues.size() - ApplicationRunner.getTrainingSetLimit());
System.out.println("Accuracy Percentage = " + testingAccuracy);
}
private int calculateError(int recordId) {
return layers.get(layers.size() - 1).calculateError(recordId);
}
private void performForwardPass(int recordId) {
// set the input values
Layer inputLayer = layers.get(0);
inputLayer.setInputLayerOutputValues(recordId);
for (int i = 1; i < layers.size(); i++) {
layers.get(i).performForwardPassCalculation();
}
}
private void performBackwardPass() {
Layer outputLayer = layers.get(layers.size() - 1);
//TODO: Here is where we need to integrate Expected/target output
double targetValue = 1.0;
outputLayer.setTargetValues(targetValue);
// Calculate output layer gradients:
outputLayer.calculateOutputGradientValue();
// Calculate hidden layer gradients:
for (int i = layers.size() - 2; i > 0; i--) {
layers.get(i).calculateGradientValue();
}
// Update all connection weights:
for (int i = layers.size() - 2; i >= 0; i--) {
layers.get(i).updateEdgesWeight(i);
}
}
public List<Layer> getLayers() {
return layers;
}
public void setLayers(List<Layer> layers) {
this.layers = layers;
}
}