forked from KatrinaArnold/SigRecNN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuralNet.cpp
More file actions
192 lines (144 loc) · 3.69 KB
/
NeuralNet.cpp
File metadata and controls
192 lines (144 loc) · 3.69 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
#include "NeuralNet.h"
NeuralNet::NeuralNet(int nodes, int iter, double b, double a, double t, string trainPath)
{
TrainData *trainData;
midNodes = nodes;
maxIter = iter;
beta = b;
alpha = a;
thresh = t;
numLayers = 3; // currently always 3
/* Load Training Data */
//cout << "Loading training data...";
trainData = db.loadTrainDataFromDir(trainPath, &numTr);
numRes = db.tableSize(); /* the number of possible results is determined
* by asking the DB how many signatures are
* known */
// 32xmidNoes + midNodes*numRes = numRes * nTrainEx
layerSize = new int [numLayers];
layerSize[0] = 8*2*2;
layerSize[1] = midNodes;
layerSize[2] = numRes;
// Create the neural net
bp = new BackPropNN(numLayers, layerSize, beta, alpha);
train(trainPath, trainData);
}
NeuralNet::NeuralNet(string filePath)
{
load(filePath);
numRes = db.tableSize();
}
bool NeuralNet::train(string trainPath, TrainData *trainData)
{
double **answer;
int max, ii;
// prepare the array with the correct answer for each signature
answer = new double* [numTr];
max = -1;
for (ii = 0; ii < numTr; ii++) {
answer[ii] = new double [numRes];
for (int jj = 0; jj < numRes; jj++) {
if (jj == (int) trainData[ii].value)
answer[ii][jj] = 1;
else
answer[ii][jj] = 0;
}
if (max != (int)trainData[ii].value) {
for(int jj = 0; jj < numRes; jj++)
max = (int)trainData[ii].value;
}
}
/* Train the net */
for (ii = 0; ii < maxIter ; ii++) {
bp->bpgt(trainData[ii%numTr].sigArr, answer[ii%numTr]);
if ((MSE = bp->mse(answer[ii%numTr])) < thresh) { // Training Complete
//cout << "MSE = " << MSE << endl;
break; // Stop
}
}
return true;
}
int NeuralNet::testDir(string dirPath, int *nTest)
{
Database testDb;
TrainData *testData;
double *test, temp = 0;
int ii, jj, max, correct;
testData = testDb.loadTrainDataFromDir(dirPath, nTest);
correct = 0;
for(jj = 0; jj < *nTest; jj++) {
temp = 0; max = 0;
/* Load the test data */
test = testData[jj].sigArr;
//cout << endl << "\t\t\tTesting for " << testData[jj].name << "..." << endl;
/* feed the test data to the net */
bp->ffwd(test);
for (ii = 0 ; ii < numRes ; ii++) {
// the results are the values at the output nodes
//cout << db.tableLookup(ii) << ": " << bp->Out(ii)*100 << "%" << endl;
if (bp->Out(ii) > temp) {
/* the highest value of the output nodes
* will determine the actual result
* i.e. who the test signature belongs to */
temp = bp->Out(ii);
max = ii;
}
}
if (testData[jj].name == db.tableLookup(max)) {
correct++;
}
}
return correct;
}
int NeuralNet::test(string filePath, double *prob)
{
Sig testSig;
double temp;
double *test;
int max, ii;
testSig.load(filePath);
test = testSig.toArray();
/* feed the test data to the net */
bp->ffwd(test);
for (ii = 0 ; ii < numRes ; ii++) {
if ((prob[ii] = bp->Out(ii)) > temp) {
/* the highest value of the output nodes
* will determine the actual result
* i.e. who the test signature belongs to */
temp = bp->Out(ii);
max = ii;
}
}
return max;
}
string NeuralNet::whoIs(int index)
{
return db.tableLookup(index);
}
int NeuralNet::getNum()
{
return numRes;
}
void NeuralNet::save(string filePath) {
string bpFile;
string dbFile;
bpFile = filePath + ".bp";
dbFile = filePath + ".db";
bp->save(bpFile);
db.save(dbFile);
}
void NeuralNet::load(string filePath) {
string base;
string bpFile;
string dbFile;
int endpos;
endpos = filePath.find_last_not_of(".");
base = filePath.substr(0, endpos - 1);
bpFile = filePath + ".bp";
dbFile = filePath + ".db";
bp = new BackPropNN(bpFile);
db.load(dbFile);
}
NeuralNet::~NeuralNet()
{
}