-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
271 lines (245 loc) · 10.4 KB
/
Main.java
File metadata and controls
271 lines (245 loc) · 10.4 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
import java.io.*;
import java.util.StringTokenizer;
/**
* This is the main class which will run the network
* Eventually this class will be replaced by an
* interface which will be able to perform actions
* on the network such as changing the number of layers
* or changing the number of neurons in a layer.
*
* @author Utkarsh Priyam
* @version 9/4/19
*/
public class Main
{
/**
* The default network configuration file paths
*/
private static String defaultFileConfigPath = "fileConfig.txt";
private static String defaultNetworkConfigPath = "networkConfig.txt";
/**
* This is the public static void main(...) method that will run
* the network until an interface is created to replace it.
*
* This method takes the classic String[] parameter args.
*
* @param args A 1D array of Strings that holds any additional
* parameters the method might need in order to run.
*/
public static void main(String[] args)
{
BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(System.in));
String inputFilePath, outputsFilePath;
String weightsFilePath, weightDumpPath, outputDumpPath, otherDumpPath;
try
{
System.out.println("Enter the file path to the file configuration file");
String fileConfigPath;
if (args.length >= 1)
fileConfigPath = args[0];
else
fileConfigPath = inputStreamReader.readLine();
if (fileConfigPath.equals(""))
fileConfigPath = defaultFileConfigPath;
File fileConfigFile = new File(fileConfigPath);
BufferedReader fileConfigReader = new BufferedReader(new FileReader(fileConfigFile));
// Required
inputFilePath = fileConfigReader.readLine();
outputsFilePath = fileConfigReader.readLine();
// Not Required
weightsFilePath = fileConfigReader.readLine();
weightDumpPath = fileConfigReader.readLine();
outputDumpPath = fileConfigReader.readLine();
otherDumpPath = fileConfigReader.readLine();
// Confirmation print
System.out.println("Using the file config path \"" + fileConfigPath + "\"\n");
}
catch (IOException ioException)
{
System.out.println("The file configuration file was missing information, badly formatted, or missing");
ioException.printStackTrace();
return;
}
String networkStructure, runType;
String lambdaConfig, randomWeightBounds, useRaggedArrays;
double minimumError;
int maximumIterationCount, numTestCases;
try
{
System.out.println("Enter the file path to the network configuration file");
String networkStructureFile;
if (args.length >= 2)
networkStructureFile = args[1];
else
networkStructureFile = inputStreamReader.readLine();
if (networkStructureFile.equals(""))
networkStructureFile = defaultNetworkConfigPath;
File fileConfigFile = new File(networkStructureFile);
BufferedReader fileConfigReader = new BufferedReader(new FileReader(fileConfigFile));
networkStructure = fileConfigReader.readLine();
runType = fileConfigReader.readLine();
numTestCases = parseInt(fileConfigReader.readLine(), 0);
if (numTestCases == 0)
throw new IllegalArgumentException("There must be at least 1 test case to execute over!");
lambdaConfig = fileConfigReader.readLine();
minimumError = parseDouble(fileConfigReader.readLine(),0.00001);
maximumIterationCount = parseInt(fileConfigReader.readLine(),100000);
randomWeightBounds = fileConfigReader.readLine();
useRaggedArrays = fileConfigReader.readLine();
System.out.println("Using the network config path \"" + networkStructureFile + "\"\n");
}
catch (IOException ioException)
{
System.out.println("The network configuration file was missing information, badly formatted, or missing");
ioException.printStackTrace();
return;
}
// Read all 3 files
File weightsFile = new File(weightsFilePath);
File inputsFile = new File(inputFilePath);
File outputsFile = new File(outputsFilePath);
// Create a new Perceptron with the specified dimensions
String[] networkConfig = networkStructure.split("-");
int[] networkDimensions = new int[networkConfig.length];
for (int networkLayerIndex = 0; networkLayerIndex < networkConfig.length; networkLayerIndex++)
networkDimensions[networkLayerIndex] = parseInt(networkConfig[networkLayerIndex],0);
boolean useRagged = useRaggedArrays.equals("true");
Perceptron perceptronNetwork = new Perceptron(networkDimensions,useRagged);
// Load lambda configuration information
StringTokenizer lambdaConfigTokenizer = new StringTokenizer(lambdaConfig);
double[] lambdaConfigurations = new double[4];
// Default 0 if missing (in most cases will simply prevent training the network)
for (int i = 0; i < lambdaConfigurations.length; i++)
lambdaConfigurations[i] = parseDouble(lambdaConfigTokenizer.nextToken(),0.0);
perceptronNetwork.loadLambdaConfig(lambdaConfigurations);
// Read the weights for the network from the file
StringTokenizer randomWeightBoundsTokenizer = new StringTokenizer(randomWeightBounds);
double minWeight = parseDouble(randomWeightBoundsTokenizer.nextToken(),0.0);
double maxWeight = parseDouble(randomWeightBoundsTokenizer.nextToken(),0.0);
perceptronNetwork.readWeights(weightsFile,minWeight,maxWeight);
// Load in the other stopping condition parameters for the network
perceptronNetwork.loadStopConditions(minimumError,maximumIterationCount);
// Calculated Outputs
double[][] calculatedOutputs;
switch (runType)
{
case "run":
{
calculatedOutputs = perceptronNetwork.runNetwork(inputsFile, numTestCases);
break;
}
case "train":
{
// Train network
perceptronNetwork.trainNetwork(inputsFile, outputsFile, numTestCases);
calculatedOutputs = perceptronNetwork.runNetwork(inputsFile, numTestCases);
break;
}
case "test":
{
// Nothing done here right now
calculatedOutputs = new double[0][0];
break;
}
default:
{
System.out.println(runType + " is not a valid run type for this network.");
System.out.println("The only valid run types are \"run\", \"train\", and \"test\".");
return;
}
}
try
{
double[][][] finalWeights = perceptronNetwork.weights;
PrintWriter weightsDumpWriter = new PrintWriter(new BufferedWriter(new FileWriter(weightDumpPath)));
for (int weightLayerIndex = 0; weightLayerIndex < finalWeights.length; weightLayerIndex++)
{
int prevLayerNeuronCount = networkDimensions[weightLayerIndex];
int nextLayerNeuronCount = networkDimensions[weightLayerIndex + 1];
for (int prevLayerElementIndex = 0; prevLayerElementIndex < prevLayerNeuronCount; prevLayerElementIndex++)
for (int nextLayerElementIndex = 0; nextLayerElementIndex < nextLayerNeuronCount; nextLayerElementIndex++)
{
double weightValue = finalWeights[weightLayerIndex][prevLayerElementIndex][nextLayerElementIndex];
weightsDumpWriter.print(weightValue + " ");
}
weightsDumpWriter.println();
}
weightsDumpWriter.close();
PrintWriter outputsDumpWriter = new PrintWriter(new BufferedWriter(new FileWriter(outputDumpPath)));
for (double[] testCaseOutputs : calculatedOutputs)
{
for (double singularOutput : testCaseOutputs)
outputsDumpWriter.print(singularOutput + " ");
outputsDumpWriter.println();
}
outputsDumpWriter.close();
if (runType.equals("train"))
{
PrintWriter otherDumpWriter = new PrintWriter(new BufferedWriter(new FileWriter(otherDumpPath)));
otherDumpWriter.println("Iterations:\n -- " + perceptronNetwork.iterationCounter);
otherDumpWriter.println("Highest Test Case Error:\n -- " + perceptronNetwork.maximumTestCaseError);
otherDumpWriter.println("Final Lambda:\n -- " + perceptronNetwork.lambda);
otherDumpWriter.close();
}
}
catch (IOException ioException)
{
System.out.println("Uncontrolled IOException encountered while logging data to dump files");
ioException.printStackTrace();
}
}
/**
* This method parses a integer from a single string token.
* If the token is not a integer, then it just returns the default value.
*
* This method takes two String parameters: nextToken and defaultValue
*
* @param nextToken The token to parse
* @param defaultValue The default value to return
*
* @return The parsed integer, or the defaultValue if the token cannot be parsed
*/
private static int parseInt(String nextToken, int defaultValue)
{
/*
* The use of 2 return statements in this method is completely
* intentional as it improves the readability of the method significantly
* over using a single return and intermediate storage variables.
*/
try
{
return Integer.parseInt(nextToken);
}
catch (NumberFormatException ignored)
{
return defaultValue;
}
}
/**
* This method parses a double from a single string token.
* If the token is not a double, then it just returns the default value.
*
* This method takes two String parameters: nextToken and defaultValue
*
* @param nextToken The token to parse
* @param defaultValue The default value to return
*
* @return The parsed double, or the defaultValue if the token cannot be parsed
*/
private static double parseDouble(String nextToken, double defaultValue)
{
/*
* The use of 2 return statements in this method is completely
* intentional as it improves the readability of the method significantly
* over using a single return and intermediate storage variables.
*/
try
{
return Double.parseDouble(nextToken);
}
catch (NumberFormatException ignored)
{
return defaultValue;
}
}
}