-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadXML.java
162 lines (137 loc) · 5.09 KB
/
ReadXML.java
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
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class ReadXML {
private int imageSideLength;
private int hiddenUnitCount;
private int hiddenLayerCount;
private int outputUnitCount;
private int hasFireValue;
private int inputUnitCount;
private double[][] hiddenInputWeights;
private double[][] hiddenHiddenWeights;
private double[][] outputHiddenWeights;
private int pixelCount;
private File file;
ArrayList<String> hiddenArray = new ArrayList();
ArrayList<String> outputArray = new ArrayList();
public ReadXML(File file) throws IOException {
this.file = file;
readNetworkVariables();
}
@Override
public String toString(){
String txt = "ImageSideLength : "+ imageSideLength + "\n"+ " hiddenUnitCount :" + getHiddenUnitCount() +
" \n" + "hiddenLayerCount :"+ getHiddenLayerCount();
return txt;
}
// Reads the values in the XML file and stores the values in variables
public void readNetworkVariables() throws IOException {
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(file);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("network");
for (int i = 0; i < nList.getLength(); i++) {
Node nNode = nList.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
String imageSideLengthString = eElement.getElementsByTagName("imageSideLength").item(0).getTextContent();
imageSideLength = Integer.parseInt(imageSideLengthString);
String numberOfHiddenUnits = eElement.getElementsByTagName("numberOfHiddenUnits").item(0).getTextContent();
hiddenUnitCount = Integer.parseInt(numberOfHiddenUnits);
String numberOfHiddenLayers = eElement.getElementsByTagName("numberOfHiddenLayers").item(0).getTextContent();
hiddenLayerCount = Integer.parseInt(numberOfHiddenLayers);
String numberOfOutputUnits = eElement.getElementsByTagName("numberOfOutputUnits").item(0).getTextContent();
outputUnitCount = Integer.parseInt(numberOfOutputUnits);
String hasFire = eElement.getElementsByTagName("hasFireValue").item(0).getTextContent();
hasFireValue = Integer.parseInt(hasFire);
}
}
NodeList hiddenWeights = doc.getElementsByTagName("hiddenLayer");
for (int i = 0; i < hiddenWeights.getLength(); i++) {
Node n = hiddenWeights.item(i);
if (n.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) n;
for (int k = 0; k < hiddenUnitCount; k++) {
hiddenArray.add(eElement.getElementsByTagName("weightArray").item(k).getTextContent());
}
}
}
NodeList outputWeights = doc.getElementsByTagName("outputLayer");
for (int i = 0; i < outputWeights.getLength(); i++) {
Node o = outputWeights.item(i);
if (o.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) o;
for (int p = 0; p < outputUnitCount; p++) {
outputArray.add(eElement.getElementsByTagName("weightArray").item(p).getTextContent());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
pixelCount = ((imageSideLength * imageSideLength) * 3) + 1;
hiddenInputWeights = new double[hiddenUnitCount][pixelCount];
for (int i = 0; i < hiddenUnitCount; i++) {
List<String> hiddenList = Arrays.asList(hiddenArray.get(i).split(","));
for (int j = 0; j < pixelCount; j++) {
double number = Double.parseDouble(hiddenList.get(j));
hiddenInputWeights[i][j] = number;
}
}
outputHiddenWeights = new double[outputUnitCount][hiddenUnitCount];
for (int i = 0; i < outputUnitCount; i++) {
List<String> outputList = Arrays.asList(outputArray.get(i).split(","));
for (int j = 0; j < hiddenUnitCount; j++) {
double number = Double.parseDouble(outputList.get(j));
outputHiddenWeights[i][j] = number;
}
}
}
public int getHiddenUnitCount() {
return hiddenUnitCount;
}
public int getHiddenLayerCount() {
return hiddenLayerCount;
}
public int getOutputUnitCount() {
return outputUnitCount;
}
public int getHasFireValue() {
return hasFireValue;
}
public double[][] getHiddenInputWeights() {
return hiddenInputWeights;
}
public double[][] getHiddenHiddenWeights() {
return hiddenHiddenWeights;
}
public double[][] getOutputHiddenWeights() {
return outputHiddenWeights;
}
public int getPixelCount() {
return pixelCount;
}
public File getFile() {
return file;
}
public ArrayList<String> getHiddenArray() {
return hiddenArray;
}
public ArrayList<String> getOutputArray() {
return outputArray;
}
public int getImageSideLength() {
return imageSideLength;
}
}