forked from parallella/nnP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataFile.hpp
353 lines (294 loc) · 7.23 KB
/
dataFile.hpp
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
#ifndef _datafile_h
#define _datafile_h
#include "nnFile.hpp"
#include "nn.hpp"
/*
* The eNN file wrapper hierarchy:
*
* NNFile
* networkFile
* dataFile
* inputFile
* trainingFile
*
* The inputFile contains just input data. These are the "production" files that contain the data that you
* want to classify.
*
* The trainingFile contains known, preclassified data sets and their desired output separated by a ;
*
* See the file content descriptions for more information.
*
* All data is stored as floats in the standard template class vector. These classes contain a vectors of vector<float>
* buried in the class twoDFloatArray.
*
* To access an input set call NNFile::inputFile::inputSet(unsigned int) or NNFile::trainingFile::inputSet(unsigned int) where the arguement is
* the row that you want. Similarly call trainingFile::outputSet(unsigned int) to retrieve the output set.
*
* The easiest way to use these classes is to pass your file reference to your newly created network and let it sort out the data.
*/
class dataFile: public NNFile
{
public:
dataFile() :
NNFile()
{
lineCount = 0;
// inputArray = NULL;
}
dataFile(ifstream * theFile) :
NNFile(theFile)
{
lineCount = 0;
// inputArray = NULL;
}
dataFile(const char * cstrFileName) :
NNFile(cstrFileName)
{
lineCount = 0;
}
dataFile(string * strFileName) :
NNFile(strFileName)
{
lineCount = 0;
}
virtual ~dataFile() //: ~NNFile()
{
}
public:
// access
unsigned int inputLines() // return how many lines have been read in
{ // return how many lines have been read in
return lineCount;
}
virtual nnFileContents fileType() = 0;
private:
virtual status_t decodeLine(string * strLine) = 0;
protected:
virtual status_t readInLines(bool isTrainingFile) = 0;
unsigned int lineCount;
};
class inputFile: public dataFile
{
public:
inputFile() :
dataFile()
{
}
inputFile(ifstream * theFile) :
dataFile(theFile)
{
}
inputFile(const char * cstrFileName) :
dataFile(cstrFileName)
{
}
inputFile(string * strFileName) :
dataFile(strFileName)
{
}
virtual ~inputFile() // : ~dataFile()
{
}
virtual nnFileContents fileType()
{
return DATA;
}
protected:
status_t readInLines(bool shouldNotGetHere)
{
return FAILURE;
}
private:
status_t decodeLine(string * strLine)
{
std::string::size_type bracketPos;
string verb = "";
string arguments = "";
status_t decodeResult;
bracketPos = strLine->find('(', 0);
if (bracketPos == std::string::npos)
throw format_Error(ENN_ERR_NON_FILE);
if (verbArguement(strLine, verb, arguments))
{
if (verb == "inputVector")
{
#ifdef _DEBUG_
cout << "Decoding Input Vector\n";
#endif
return decodeInputVector(&arguments);
}
if (verb == "networkTopology")
{
#ifdef _DEBUG_
cout << "Decode Topology\n";
#endif
vector<unsigned int> layerWidths(maxLayers);
decodeResult = decodeNetworkTopology(&arguments, maxLayers, &layerWidths);
if (layerWidths[0] != ((nn*) theNetwork)->layerZeroWidth())
throw format_Error(ENN_ERR_NONMATCHING_TOPOLOGY);
return decodeResult;
}
// errMessage.Format("%s: %s", ENN_ERR_UNK_KEY_WORD, verb.GetBuffer());
errMessage = ENN_ERR_UNK_KEY_WORD;
errMessage += ": ";
errMessage += verb;
throw format_Error(errMessage.c_str());
}
else
throw format_Error(ENN_ERR_NON_FILE);
return FAILURE; // will not happen
}
status_t decodeInputVector(string * fragment)
{
float inputValue;
unsigned int node;
std::string::size_type startPos;
unsigned int inputNodeCount;
vector<float> lineVector(
inputNodeCount = ((nn*) theNetwork)->layerZeroWidth());
startPos = 1;
#ifdef _DEBUG_
cout << "Input Values,";
#endif
for (node = 0; node < (inputNodeCount - 1); node++) //>
{
inputValue = nextFValue(fragment, startPos);
lineVector[node] = inputValue;
#ifdef _DEBUG_
cout << " Node " << node << ": " << inputValue;
#endif
}
inputValue = nextFValue(fragment, startPos, ')');
#ifdef _DEBUG_
cout << " Node " << node << ": " << inputValue << "\n";
#endif
lineVector[node] = inputValue;
((nn*) theNetwork)->run(&lineVector, NULL, lineCount); // run immediately
return SUCCESS;
}
};
class trainingFile: public dataFile
{
public:
trainingFile() :
dataFile()
{
}
trainingFile(ifstream * theFile) :
dataFile(theFile)
{
}
trainingFile(const char * cstrFileName) :
dataFile(cstrFileName)
{
}
trainingFile(string * strFileName) :
dataFile(strFileName)
{
}
virtual ~trainingFile() //: ~dataFile()
{
}
virtual nnFileContents fileType()
{
return TRAIN_TEST;
}
protected:
status_t readInLines(bool isTrainingFile)
{
inputToTrain = isTrainingFile;
return NNFile::readInLines();
}
private:
status_t decodeLine(string * strLine)
{
std::string::size_type bracketPos;
string verb = "";
string arguments = "";
status_t decodeResult;
bracketPos = strLine->find('(', 0);
if (bracketPos == std::string::npos)
throw format_Error(ENN_ERR_NON_FILE);
if (verbArguement(strLine, verb, arguments))
{
if (verb == "networkTopology")
{
#ifdef _DEBUG_
cout << "Decode Topology\n";
#endif
vector<unsigned int> layerWidths(maxLayers);
decodeResult = decodeNetworkTopology(&arguments, maxLayers, &layerWidths);
if ((layerWidths[0] == ((nn*) theNetwork)->layerZeroWidth())
&& (layerWidths[3] == ((nn*) theNetwork)->layerNWidth()))
return decodeResult;
else
throw format_Error(ENN_ERR_NONMATCHING_TOPOLOGY);
return decodeResult;
}
if (verb == "inputOutputVector")
{
#ifdef _DEBUG_
cout << "Decode Input/Output Vector\n";
#endif
return decodeTrainingVector(&arguments);
}
errMessage = ENN_ERR_UNK_KEY_WORD;
errMessage += ": ";
errMessage += verb;
throw format_Error(errMessage.c_str());
}
else
throw format_Error(ENN_ERR_NON_FILE);
return FAILURE; // will not happen
}
status_t decodeTrainingVector(string * fragment)
{
float readValue;
unsigned int node;
std::string::size_type startPos;
unsigned int inWidth = ((nn*) theNetwork)->layerZeroWidth();
unsigned int outWidth = ((nn*) theNetwork)->layerNWidth();
vector<float> inVector(inWidth);
vector<float> outVector(outWidth);
startPos = 1;
#ifdef _DEBUG_
cout << "Input Vector,";
#endif
for (node = 0; node < (inWidth - 1); node++) //>
{
readValue = nextFValue(fragment, startPos);
inVector[node] = readValue;
#ifdef _DEBUG_
cout << " Node: " << node << ": " << readValue;
#endif
}
readValue = nextFValue(fragment, startPos, ';');
#ifdef _DEBUG_
cout << " Node: " << node << ": " << readValue << "\n";
#endif
inVector[node] = readValue;
#ifdef _DEBUG_
cout << "Output Vector,";
#endif
for (node = 0; node < (outWidth - 1); node++) //>
{
readValue = nextFValue(fragment, startPos);
outVector[node] = readValue;
#ifdef _DEBUG_
cout << " Node: " << node << ": " << readValue;
#endif
}
readValue = nextFValue(fragment, startPos, ')');
#ifdef _DEBUG_
cout << " Node: " << node << ": " << readValue << "\n";
#endif
outVector[node] = readValue;
if (inputToTrain)
((nn*) theNetwork)->train(&inVector, &outVector);
else
((nn*)theNetwork)->test(lineCount, &inVector, &outVector);
return SUCCESS;
}
bool inputToTrain;
};
#endif