-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayer.java
More file actions
133 lines (108 loc) · 3.97 KB
/
Layer.java
File metadata and controls
133 lines (108 loc) · 3.97 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
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* Created by Srikanth on 2/27/2017.
*/
public class Layer {
private List<Node> nodes;
private LayerType layerType;
private Layer previousLayer;
public Layer(Layer previousLayer, LayerType layerType) {
nodes = new ArrayList<>();
this.previousLayer = previousLayer;
this.layerType = layerType;
}
public boolean initializeInputLayer() {
if (ApplicationRunner.getData().dataValues == null || ApplicationRunner.getData().dataValues.size() == 0)
return false;
// access data : input accessing
List lstinputNodes = ApplicationRunner.getData().dataValues.get(0).getAttributes();
for (int i = 0; i < lstinputNodes.size(); i++) {
Node node = new Node(this);
nodes.add(node);
}
return true;
}
public void initializeHiddenLayer() {
for (int i = 0; i < ApplicationRunner.getHiddenLayerNodeCount(); i++) {
Node node = new Node(this);
Random rand = new Random();
// create edges from previous node
List<Node> previousNodes = previousLayer.getNodes();
for (Node prevNode : previousNodes) {
float value = rand.nextFloat() * 2 - 1;
Edge edge = new Edge(value, prevNode, node);
Edge revEdge = new Edge(value, node, prevNode);
prevNode.addEdge(edge);
node.addRevEdge(revEdge);
}
nodes.add(node);
}
}
public void initializeOutputLayer() {
Node outputNode = new Node(this);
Random rand = new Random();
List<Node> previousNodes = previousLayer.getNodes();
for (Node prevNode : previousNodes) {
float value = rand.nextFloat() * 2 - 1;
Edge edge = new Edge(value, prevNode, outputNode);
Edge revEdge = new Edge(value, outputNode, prevNode);
prevNode.addEdge(edge);
outputNode.addRevEdge(revEdge);
}
nodes.add(outputNode);
}
public void setInputLayerOutputValues(int recordId) {
if (this.layerType != LayerType.INPUT)
return;
// access data : input accessing
List<Double> inputLayerOutputValues = ApplicationRunner.getData().dataValues.get(recordId).attributes;
for (int i = 0; i < inputLayerOutputValues.size(); i++) {
nodes.get(i).setOutputX(inputLayerOutputValues.get(i));
}
}
public void setTargetValues(double outputLayerTargetValue) {
if (this.layerType != LayerType.OUTPUT)
return;
nodes.get(0).setTargetValue(outputLayerTargetValue);
}
public void performForwardPassCalculation() {
for (Node node : nodes)
node.performForwardPassCalculation();
}
public void calculateOutputGradientValue() {
if (this.layerType != LayerType.OUTPUT)
return;
nodes.get(0).calculateOutputGradientValue();
}
public void calculateGradientValue() {
for (Node node : nodes)
node.calculateGradientValue();
}
public void updateEdgesWeight(int hiddenLayerId) {
System.out.println("Hidden Layer Id " + hiddenLayerId + " :");
for (int i = 0;i < nodes.size();i++) {
nodes.get(i).updateEdgesWeight();
}
}
public int calculateError(int recordId) {
// access data : Output
Node outputNode = nodes.get(0);
int nodeOutput = outputNode.getOutputX() >= 0.5 ? 1 : -1;
Double actualOutput = (Double) Data.a2.get(recordId);
return nodeOutput * actualOutput == 1 ? 0 : 1;
}
public List<Node> getNodes() {
return nodes;
}
public void setNodes(List<Node> nodes) {
this.nodes = nodes;
}
public LayerType getLayerType() {
return layerType;
}
public void setLayerType(LayerType layerType) {
this.layerType = layerType;
}
}