-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.cpp
More file actions
466 lines (361 loc) · 12.2 KB
/
reader.cpp
File metadata and controls
466 lines (361 loc) · 12.2 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
* Implementation of reader functionalities - currently reads in
* network structure and training data from a properly formatted
* input file as well as hyperparameters from an optional config file.
* Through this implementation, initial reading is independent of the network
* and training functionalities
*
* Reader class services:
* void readConfigFile(string config) updates hyperparameters given by config file
* values (must be properly formatted), void readMetaData(ifstream& fileIn) reads in
* Reader important values such as number of training sets, a weights existence flag, and
* the number of layers in the network as well as the shape of the network layers,
* void readTrainingData(ifstream& fileIn) reads in training data given the number of
* training sets and input activations per set, void readWeights(ifstream& fileIn)
* populates a weights array if the user has predefined values, void exportWeights(weights)
* stores the given weights in an output
*
* @author Kailash Ranganathan
* @version 3/21/20
*/
#include <fstream>
#include <iostream>
#include <string>
#include "reader.hpp"
using namespace std;
/*
* Retrieving already defined values from other source files.
* (global variables)
*/
extern double lambda;
extern int maxIter;
extern double randomWeightMin;
extern double randomWeightMax;
extern double minError;
extern string outputFile;
/*
* This function is a helper method to read in values of the
* hyperparameters from the given config file. It takes in the filepath
* (name of the file unless its not in the root folder) and opens
* a file input stream and parses the file. The file MUST have only one
* token, be it the name of a hyperparameter or its value, on each line.
* After each successive hyperparameter name should follow its value.
* @param config the name of the config file
* note - All the values are stored back into the hyperparameter variables
* defined in the header file.
*/
void readConfigFile(string config)
{
ifstream confstream(config); //Opening file input stream
string currentArg;
while (!confstream.eof()) //Keep reading while the input stream
{ //is not finished
/*
* Gets the hyperparameter at the current line
* and the value at the next line.
*/
getline(confstream, currentArg);
string value;
getline(confstream,value);
double val = atof(value.c_str());
/*
* Parsing of the configuration files. The valid expressions
* are lambda, maxIter, minWeight, and maxWeight. Their values
* must be on the line following the hyperparameter name.
*/
if (currentArg.find("lambda") != string::npos)
{
lambda = val;
}
else if (currentArg.find("maxIter") != string::npos)
{
maxIter = val;
}
else if (currentArg.find("minWeight") != string::npos)
{
randomWeightMin = val;
}
else if (currentArg.find("maxWeight") != string::npos)
{
randomWeightMax = val;
}
else if (currentArg.find("minError") != string::npos)
{
minError = val;
}
}
confstream.close(); //Closing the input stream
return;
} //readConfig(string filename) method
/*
* Constructor for the Reader class. Takes in a file name for the training data
* and network parameters and configFile name (if it is empty, then just uses
* default values) - the constructor reads all of the input in
* from the file and stores them in their
* respective data structures (weights array, input array,
* truths array, and network layer size array)
* @param filename the name of the training data file
* @param configFile the name of the optional config file (\0 if no file is provided)
*/
Reader::Reader(string filename, string configFile, string testFile)
{
ifstream fileIn(filename);
/*
* If a valid filename is given for configurations/hyperparameters
* use that instead and read from that file.
*/
if (configFile != "\0")
{
readConfigFile(configFile);
}
/*
* Data at the top of the file (number of training sets, hasWeights flag,
* and number of layers for the network) helps the reader correctly read in inputs
*/
readMetaData(fileIn);
if (hasWeights == 1) //Read weights if the user has written them
{
readWeights(fileIn);
}
if(testOrTrain == 1)
{
readTrainingData(fileIn);
}
else
{
readTestData(testFile);
}
fileIn.close(); //Closing the input stream
} //Reader class constructor
/*
* Reads the test data from the test directory
* and prepares it to be executed by the network
*/
void Reader::readTestData(string testFileName)
{
ifstream testFile(testFileName.c_str());
test = new double[numInputs];
for (int i = 0; i < numInputs; i++)
{
double current;
testFile >> current;
test[i] = current/(255.0);
}
cout << "Evaluation of file " << "\"" << testFileName << "\"" << endl;
return;
}
double* Reader::getTest()
{
return test;
}
/*
* Reads the metadata at the top of a text file giving the
* total number of training sets and whether the user would like to input weights or not
* Stores these values in their respective variables
*
* @param fileIn the file input stream passed by reference
*/
void Reader::readMetaData(ifstream& fileIn)
{
fileIn >> numTrain >> hasWeights >> numLayers >> testOrTrain; //Reading in training set amounts
layerSizes = new int[numLayers];
cout << endl << endl;
/*
* Reading in the layer sizes from the input file
*/
cout << "Network structure: ";
for (int n = 0; n < numLayers; n++)
{
int currentLayerSize;
fileIn >> currentLayerSize;
layerSizes[n] = currentLayerSize;
cout << layerSizes[n] << " ";
}
cout << endl;
/*
* Declared for readability in later sections
*/
numInputs = layerSizes[0];
numOutputs = layerSizes[numLayers-1];
/*
* Allocating memory space for the weights array
*
*/
weightsRead.resize(numLayers - 1);
for (int n = 0; n < numLayers - 1; n++) //Iterating over the number of layers
{
/*
* Allocating memory for the current weights layer
* Resizing the 2nd dimension to the source layer (current layer)
* and iterating over that to resize the 3rd dimension to the destination layer
*
*/
weightsRead[n].resize(layerSizes[n]);
for (int j = 0; j < layerSizes[n]; j++) //Iterating over the current source node
{
weightsRead[n][j].resize(layerSizes[n+1]); //Allocating memory for the current node's weights
}
} //for (int n = 0; n < numLayers - 1; n++)
return;
} //void Reader::readMetaData(ifstream& fileIn)
/*
* This method allocates memory for the truth and input arrays and reads the
* values in from the input file into these arrays. The arrays are held in the
* Reader instance.
*
*/
void Reader::readTrainingData(ifstream& fileIn)
{
inputs = new double*[numTrain];
truths = new double*[numTrain];
/*
* Allocating memory for the second dimension of the
* truth values and input values
*/
for (int i = 0; i < numTrain; i++)
{
truths[i] = new double[numOutputs];
inputs[i] = new double[numInputs];
}
double currentInput = 5.5;
double currentTruth = 5.5;
string currentImage = "wacko";
string currentTruthPath = "truth";
for (int i = 0; i < numTrain; ++i) //Iterates over each training set to read it
{
currentImage = "train/train";
currentTruthPath = "truth/truth";
currentImage += to_string(i);
currentTruthPath += to_string(i);
cout << currentImage << endl;
cout << currentTruthPath << endl;
ifstream currentFile(currentImage);
ifstream currentTruthFile(currentTruthPath);
/*
* Reading in the current input values
*
*/
for (int j = 0; j < numInputs; j++) //Reads in the appropriate number of inputs
{
currentFile >> currentInput;
inputs[i][j] = (1.0*currentInput)/(255.0);
}
/*
* Reading in the current truth values
*
*/
for (int j = 0; j < numOutputs; j++) //Reads in the appropriate number of outputs(truth values)
{
currentTruthFile >> currentTruth;
cout << currentTruth << endl;
truths[i][j] = currentTruth;
}
currentInput = 0.0;
currentTruth = 0.0;
currentFile.close();
currentTruthFile.close();
} // for (int i = 0; i < numTrain; i++)
//cout << numOutputs;
return;
} // void Reader::readTrainingData(ifstream& fileIn)
/*
* If the user inputs weights as part of the file, this method
* reads in those weights given by the dimensions of the weights array
* @param fileIn the file input stream passed by reference
*/
void Reader::readWeights(ifstream& fileIn)
{
string weightsFile;
string throwaway;
getline(fileIn, throwaway);
getline(fileIn, weightsFile);
std::string const weightsFileName = weightsFile + "\0";
ifstream weightsFileIn;
weightsFileIn.open("finalweights");
cout << "Weights from from " << weightsFile << endl << endl;
for (int n = 0; n < numLayers - 1; n++) //Iterating over the layers
{
for (int j = 0; j < layerSizes[n]; j++) //Iterating over the source layer
{
for (int i = 0; i < layerSizes[n+1]; i++) //Iterating over the destination layer
{
weightsFileIn >> weightsRead[n][j][i]; //Reading in the current weight
}
}
}
weightsFileIn.close();
return;
} //void Reader::readWeights(ifstream& fileIn)
/*
* Returns a shallow copy of the metadata in a double array (currently)
* of length 3. The first index has the number of training sets, the second index
* has the hasWeights flag, and the third index gives the number of layers for the network
*/
int* Reader::getMetaData()
{
static int metaData[4]; // Static so that it does not disappear
// at the end of this block
metaData[0] = numTrain;
metaData[1] = hasWeights;
metaData[2] = numLayers;
metaData[3] = testOrTrain;
return metaData;
}
/*
* Returns the array containing the size of each layer of the network
*/
int* Reader::getLayerSizes()
{
return layerSizes;
}
/*
* Returns the weights array shaped by the reader. If no weights were read in,
* hasWeights will be zero and the network MUST populate the weights randomly or else
* it will a bunch of uninitialized double values.
*/
vector<vector<vector<double> > > Reader::getWeights()
{
return weightsRead;
}
/*
* Returns a copy of the training data
*/
double** Reader::getTrainingData()
{
return inputs;
}
/*
* Returns a copy of the truth values
* for each training set in a two dimensional pointer
*/
double** Reader::getTruths()
{
return truths;
}
/*
* Exports the given weights to a file with the name of the parameter
* @param weights the weights to export
* @param filename the filename of the weights file.
*/
void exportWeights(vector<vector<vector<double> > > weights, string fileName)
{
ofstream fout(fileName);
/*
* Iterates over the weights array and outputs the weights
* one by one - each "layer" of weights corresponds to a line
* in the file.
*/
for (int n = 0; n < weights.size(); n++) //Iterating over the layers
{
for (int j = 0; j < weights[n].size(); j++) //Iterating over the source layer
{
for (int i = 0; i < weights[n][j].size(); i++) //Iterating over the destination layer
{
fout << weights[n][j][i] << " "; //Exporting current weight to file
}
}
fout << endl;
}
fout.close(); //Closing the output stream
return;
} //exportWeights method