forked from nickoppen/nnP
-
Notifications
You must be signed in to change notification settings - Fork 5
/
nnP.cpp
349 lines (301 loc) · 8.4 KB
/
nnP.cpp
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
#include "nn.hpp"
#include "networkFile.hpp"
#include "dataFile.hpp"
void callback_RunComplete(const int index, void * caller)
{
vector<float> * resVec; // created by the net
unsigned int i;
resVec = new vector<float>(((nn*)caller)->layerNWidth());
resVec = ((nn*)caller)->runResult(resVec);
cout << "Node,Value";
for(i = 0; i< resVec->size(); i++)
cout << "\n" << i << "," << (*resVec)[i];
cout << "\n";
delete resVec;
}
void callback_TrainingComplete(void * caller)
{
vector<float> * errVec; // created by the net
unsigned int i;
errVec = new vector<float>(((nn*)caller)->layerNWidth());
if ( ((nn*)caller)->trainingError(errVec) == SUCCESS)
{
cout << "Node,Error";
for(i = 0; i< errVec->size(); i++)
cout << "\n" << i << "," << (*errVec)[i];
cout << "\n";
}
else
cout << "Error: failed to retrieve the error vector.\n";
delete errVec;
}
void callback_TestComplete(const int index, vector<float>* inputVector, vector<float>* desiredOutput, vector<float>* actualOutput, vector<float> * errVec, void * caller)
{
unsigned int i;
if (index == 0) // title row
{
cout << "Index";
for(i = 0; i< inputVector->size(); i++)
cout << "\tInput:" << i;
for(i = 0; i< desiredOutput->size(); i++)
cout << "\tDesired:" << i;
for(i = 0; i< actualOutput->size(); i++)
cout << "\tActual:" << i;
for(i = 0; i< errVec->size(); i++)
cout << "\tError:" << i;
cout << "\n";
}
cout << index;
for(i = 0; i< inputVector->size(); i++)
cout << "\t" << (*inputVector)[i];
for(i = 0; i< desiredOutput->size(); i++)
cout << "\t" << (*desiredOutput)[i];
for(i = 0; i< actualOutput->size(); i++)
cout << "\t" << (*actualOutput)[i];
for(i = 0; i< errVec->size(); i++)
cout << "\t" << (*errVec)[i];
cout << "\n";
}
int main(int argc, char *argv[])
{
nn * theNet = NULL;
string argvI;
string strArg;
string strSave;
int in, out, hidden;
// float fVal;
bool unknownFlag = false;
bool quiet = false;
int i;
string nnName = "TestNet";
cout.precision(12);
for (i=1; i<argc; i++)
{
argvI = argv[i];
if (argvI == "-n")
{
if (theNet != NULL)
{
cout << "A network was already loaded and has been deleted.\n";
delete theNet;
}
try
{
networkFile netFile(argv[++i]);
theNet = new nn(&netFile);
if (!quiet)
cout << "Done with -n\n";
}
catch (format_Error & e)
{
cout << e.mesg << "\n";
}
}
else
if (argvI == "-t")
{
if (theNet == NULL)
cout << "A network must be loaded before it is trained.\n";
else
try
{
trainingFile trFile(argv[++i]);
theNet->train(&trFile, &callback_TrainingComplete);
if (!quiet)
cout << "Done with -t\n";
}
catch (format_Error & e)
{
cout << e.mesg << "\n";
}
}
else
if ((argvI == "-r") || (argvI == "-run"))
{
if (theNet == NULL)
cout << "A network must be loaded before it is run.\n";
else
try
{
inputFile dataFile(argv[++i]);
theNet->run(&dataFile, &callback_RunComplete);
if (!quiet)
cout << "Done with -r or -run\n";
}
catch (format_Error & e)
{
cout << e.mesg << "\n";
}
}
else
if (argvI == "-s")
{
if (theNet == NULL)
cout << "A network must be loaded before it is saved.\n";
else
try
{
theNet->saveTo(argv[++i]);
if (!quiet)
cout << "Done with -s\n";
}
catch (format_Error & e)
{
cout << e.mesg << "\n";
}
}
else
if (argvI == "-c")
{
if (theNet != NULL)
{
cout << "A network was already loaded and has been deleted.\n";
delete theNet;
}
try
{
unsigned int layer, layerCount;
layerCount = atoi(argv[++i]);
vector<unsigned int> layers(layerCount);
for (layer=0; layer<layerCount; layer++)
layers[layer] = atoi(argv[++i]);
strArg = argv[++i];
theNet = new nn(&layers, strArg);
if (!quiet)
cout << "Done with -c\n";
}
catch (format_Error & e)
{
cout << e.mesg << "\n";
}
}
else
if (argvI == "-rand")
{
if (theNet == NULL)
cout << "A network must be loaded before it is randomised.\n";
else
try
{
theNet->randomise();
if (!quiet)
cout << "Done with -rand\n";
}
catch (format_Error & e)
{
cout << e.mesg << "\n";
}
}
else
if (argvI == "-at")
{
if (theNet == NULL)
cout << "A network must be loaded before it is altered.\n";
else
try
{
in = atoi(argv[++i]);
hidden = atoi(argv[++i]);
out = atoi(argv[++i]);
theNet->alter(in, hidden, out);
if (!quiet)
cout << "Done with -at\n";
}
catch (format_Error & e)
{
cout << e.mesg << "\n";
}
}
else
if (argvI == "-am")
{
if (theNet == NULL)
cout << "A network must be loaded before it is altered.\n";
else
try
{
unsigned int layerNo;
size_t colonPos;
string modifier;
string key;
string value;
layerNo = atoi(argv[++i]);
modifier = argv[++i];
colonPos = modifier.find(":");
if (layerNo == 0)
{
key = modifier.substr(0, colonPos);
if (key == "biasNode")
{
value = modifier.substr(colonPos + 1);
if (value == "true")
theNet->alter(layerNo, BIAS_NODE, true);
else
if (modifier.substr(colonPos + 1, modifier.size() - colonPos) == "false")
theNet->alter(layerNo, BIAS_NODE, false);
else
cout << "Invalid argument for modifier biasNode: " << value << "\n";
}
else
cout << "Modifer:" << key << " not valid\n";
}
else
cout << "Invalid layer number:" << layerNo << "\n";
if (!quiet)
cout << "Done with -am\n";
}
catch (format_Error & e)
{
cout << e.mesg << "\n";
}
}
else
if (argvI == "-test")
{
if (theNet == NULL)
cout << "A network must be loaded before it is tested.\n";
else
try
{
trainingFile testFile(argv[++i]);
theNet->test(&testFile, callback_TestComplete);
if (!quiet)
cout << "Done with -test\n";
}
catch (format_Error & e)
{
cout << e.mesg << "\n";
}
}
else
if (argvI == "-q+")
quiet = true;
else
if (argvI == "-q-")
{
quiet = false;
cout << "Done with -q-\n";
}
else
{
unknownFlag = true;
cout << "Don't know that one:" << argvI << "\n";
}
}
if (unknownFlag)
{
cout << "\n-n %file load a network from %file\n";
cout << "-t %file training set %file and write the final error vector to standard output\n";
cout << "(-r | -run) %file run from input set in %file and write each result vector to standard output\n";//and save the results in outPath\n";
cout << "-rand randomise the network\n";
cout << "-test %file run the input/output pattern from training file %file and write the final difference vector to standard output\n";
cout << "-s %path save on %path (with no trailing /) Use . to write to the working directory.\n";
cout << "-c %i %h %o %n create a new randomised network with %i input nodes %h hidden nodes %o output nodes, called %n (with a learning rate of 0.1)\n";
cout << "-at %i %h %o alter the topology to be %i input nodes %h hidden nodes %o output nodes\n";
cout << "-am 1 (biasNode:true OR biasNode:false) add or remove an input bias node to layer one (default is false)\n";
cout << "-q+ OR -q- Switch quiet mode on (-q+) or off (-q-) +q+ supresses the 'Done with...' after each command line arguement\n";
}
if (theNet != NULL)
delete theNet;
return 1;
}