-
Notifications
You must be signed in to change notification settings - Fork 0
/
ann_V2.cpp
executable file
·369 lines (366 loc) · 10.4 KB
/
ann_V2.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include "ann_V2.h"
/**
*\brief prints out the weights for the first node in the input layer
*/
void ANN::print()
{
for(auto it = Neural_net[0][0]->to_nodes.begin(); it!=Neural_net[0][0]->to_nodes.end(); it++)
{
cout << showpoint << fixed << setprecision(12) << it->second << " ";
}
cout << endl;
}
/**
*\brief Destructor for each node in the Neural Network
*/
ANN::~ANN()
{
for(unsigned int i = 0; i < Neural_net.size(); i++)
{
for(unsigned int j = 0; j < Neural_net[i].size(); j++)
{
delete Neural_net[i][j];
}
}
}
/**
* /param in_structure: file name for Neural Net structure
* in_weights: file name for Neuron weights
* /brief imports the Neural Net
* We first create the nodes necessary for each layer
* then reads in the weights and assigns them to the map for each node
* each weight for the nodes to a node and from a node
*/
void ANN::import_structure(char* in_structure,char* in_weights)
{
ifstream ins;
ifstream inw;
ins.open(in_structure);
inw.open(in_weights);
int num_nodes;
vector<double> weights;
while(ins >> num_nodes)
{
vector<Nodes*> n_layer;
for(int i = 0; i < num_nodes; i++)
{
Nodes* node = new Nodes;
n_layer.push_back(node);
}
Neural_net.push_back(n_layer);
}
double weight;
while(inw >> weight)
weights.push_back(weight);
int num = 0;
int Num = 0;
for(unsigned int i = 0; i < Neural_net.size(); i++)
{
for(unsigned int j = 0; j < Neural_net[i].size(); j++)
{
if(i+1 == Neural_net.size())
Neural_net[i][j]->to_nodes[nullptr] = 0.01;
for(unsigned int x = 0; x < Neural_net[i+1].size(); x++)
{
if(i+1 == Neural_net.size())
{
Neural_net[i][j]->to_nodes[nullptr] = 0.01;
break;
}
if(x==0&&i!=0)
Neural_net[i][j]->to_nodes[nullptr] = 0.01;
if(i < Neural_net.size()-1)
{
Neural_net[i][j]->to_nodes[Neural_net[i+1][x]] = weights[num];
Neural_net[i][j]->weights.push_back(weights[num]);
num++;
}
}
if(i > 0)
{
for(unsigned int a = 0; a < Neural_net[i-1].size(); a++)
{
Neural_net[i][j]->from_nodes[Neural_net[i-1][a]] = Neural_net[i-1][a]->weights[j];
Num++;
}
}
}
}
ins.close();
inw.close();
}
/**
* \brief Trains the weights from node to node
* \param in_train: File name for the training data sets
* out_train: File name for the training output for each data set
* alpha: the alpha used to update weights
* iterations: The number of iterations for training the Neural Network
*/
void ANN::train(char* in_train,char* out_train,char* alpha,char* iterations)
{
ifstream inf;
ifstream of;
int number = atoi(iterations);
double Alpha = atof(alpha);
inf.open(in_train);
of.open(out_train);
string line;
while(getline(inf,line))
{
num_lines++;
}
for(int iter = 0; iter < number; iter++)
{
//cout << iter << endl;
inf.clear();
inf.seekg(0,std::ios::beg);
of.clear();
of.seekg(0,std::ios::beg);
for(int p = 0 ; p < num_lines; p++)
{
vector<double> init_a;
double inV;
for(unsigned int i = 0; i < Neural_net[0].size(); i++)
{
inf >> inV;
Neural_net[0][i]->a_value = inV;
}
int train_output;
of >> train_output;
for(unsigned int i = 1; i < Neural_net.size(); i ++)
{
for(unsigned int j = 0; j < Neural_net[i].size(); j++)
{
long double temp_in = 0;
int x = 0;
for(auto it = Neural_net[i][j]->from_nodes.begin(); it != Neural_net[i][j]->from_nodes.end(); it++)
{
Nodes* cur_node = it->first;
if(x == 0)
{
auto iter = Neural_net[i][j]->to_nodes.find(nullptr);
temp_in += 1*iter->second;
x++;
}
temp_in += cur_node->a_value * it->second;
}
Neural_net[i][j]->in = temp_in;
Neural_net[i][j]->a_value = a_Converter(Neural_net[i][j]->in);
}
}
//step 4 Error checking
assign_OutDelta(out_train,train_output);
//step 5 Delta for other nodes
assign_InnerDelta();
//step 7 Update Weights
update_Weights(Alpha);
}
}
}
/**
*\brief Classifies the testing data set using Euclidean Distance
*\param test_in: Name of file with testing data sets
* test_out: Name of file with the expected ouput for the testing data sets
*/
int ANN::classify(vector<int> Frame,vector<bool> possible)
{
//ifstream infile;
ifstream ofile;
//infile.open(test_in);
ofile.open("2048_TrainingDataOutput.txt");
double in_value;
string line;
int num_lines = 1;
int p = 0;
//while(getline(infile,line))
// num_lines++;
//infile.clear();
//infile.seekg(0,std::ios::beg);
for(p = 0; p < 1; p++) //Needs the size of the input file AKA FIGURE IT OUT
{
for(unsigned int x = 0; x < Neural_net[0].size(); x++)
{
//infile >> in_value;
Neural_net[0][x]->a_value = Frame[x];
}
int result = 3;
//ofile >> result;
//ofile >> result;
for(unsigned int i = 1; i < Neural_net.size(); i++)
{
for(unsigned int j = 0; j < Neural_net[i].size(); j++)
{
long double temp_in = 0;
for(auto it = Neural_net[i][j]->from_nodes.begin(); it != Neural_net[i][j]->from_nodes.end();it++)
{
Nodes* cur_node = it->first;
if(cur_node == Neural_net[i][j]->from_nodes.begin()->first)
{
auto iter = Neural_net[i][j]->to_nodes.find(nullptr);
temp_in += 1*iter->second;
}
temp_in += cur_node->a_value * it->second;
}
Neural_net[i][j]->in = temp_in;
Neural_net[i][j]->a_value = a_Converter(Neural_net[i][j]->in);
}
}
int classification = Euclidean_distance(result,possible);
if(classification == result)
precision++;
cout << classification << endl;
return classification;
}
cout << precision/num_lines << endl;
}
/**
*brief Calculates the Euclidean Distance for the given test query
* First it calculates the Euclidean distance for all numbers in the encoding
* sequence and then classifies the query to the closest value
*/
int ANN::Euclidean_distance(int result,vector<bool> possible)
{
//ifstream of;
//of.open(test_out);
//int result;
//of >> result;
vector<double> Encoded_values;
double summation = 0;
bool horizontal;
bool vertical;
horizontal = possible[0];
vertical = possible[1];
for(unsigned int x = 0; x < encoding_table.size(); x++)
{
summation = 0;
for(unsigned int i = 0; i < encoding_table[x].size(); i++)
summation += pow(encoding_table[x][i] - Neural_net[Neural_net.size()-1][i]->a_value,2);
summation = sqrt(summation);
Encoded_values.push_back(summation);
}
int classification = 0;
double check = 100;
if(vertical == 1 && horizontal == 1)
{
for(unsigned int i = 0; i < Encoded_values.size(); i++)
{
if(Encoded_values[i] < check )
{
classification = i;
check = Encoded_values[i];
}
}
}
if(vertical == 1 && horizontal == 0)
for(int i = 0; i < 2; i++)
{
classification = i;
check = Encoded_values[i];
}
if(vertical == 0 && horizontal ==1)
for(int i = 2; i < 4; i++)
{
classification = i;
check = Encoded_values[i];
}
return classification;
}
/**
*\brief Converts the given value into its a value for the node
* Uses the equation 1/1+e^-x to calculate
*\param in_value : Value given to the converter function
*/
double ANN::a_Converter(long double in_value)
{
in_value *= -1;
double result = 1/(1+exp(in_value));
return result;
}
/**
*\brief Assigns all of the delta values for the output layer
*\param out_train: the name of the ouput file for the training data
*/
void ANN::assign_OutDelta(char* out_train,int result)
{
ifstream inf;
inf.open(out_train);
for(unsigned int i = 0; i < Neural_net[Neural_net.size()-1].size(); i++)
{
Nodes* cur_node = Neural_net[Neural_net.size()-1][i];
double error = cur_node->a_value*(1-cur_node->a_value)*(encoding_table[result][i] - cur_node->a_value);
Neural_net[Neural_net.size()-1][i]->delta = error;
}
}
/**
*\brief Assigns the delta values for all of the hidden layers
*/
void ANN::assign_InnerDelta()
{
for(int i = Neural_net.size()-2; i >= 0; i--)
{
for(unsigned int j = 0; j < Neural_net[i].size(); j++)
{
Nodes* cur_node = Neural_net[i][j];
double error;
double summation = 0;
for(auto it = cur_node->to_nodes.begin(); it != cur_node->to_nodes.end(); it++)
{
if(it->first == nullptr)
{
continue;
}
summation += it->first->delta * it->second;
//contin:;
}
error = cur_node->a_value * (1-cur_node->a_value) * summation;
cur_node->delta = error;
}
}
}
/**
*\brief Updates the weights for all nodes in the Neural Network
* First we get the nodes and weights for the nodes coming from the
* one we're looking at. Then assigns the updated weights for the current
* nodes from_nodes list and also the previous nodes to_nodes list
*\param alpha: The small alpha value to alter the given weights value
*/
void ANN::update_Weights(double alpha)
{
for(unsigned int i = 1; i < Neural_net.size(); i++)//i < Neural)net.size()-1
{
for(unsigned int j = 0; j < Neural_net[i].size(); j++)
{
Nodes* cur_node = Neural_net[i][j];
for(auto it = cur_node->from_nodes.begin(); it != cur_node->from_nodes.end(); it++)
{
if(it->first == cur_node->from_nodes.begin()->first)
{
auto iter = cur_node->to_nodes.find(nullptr);
iter->second = iter->second + alpha*1*cur_node->delta;
}
it->second = it->second + alpha*it->first->a_value*cur_node->delta;
it->first->to_nodes[cur_node] = it->second;
}
}
}
}
/**
*\brief Reads in the encoding table and assigns the value into a 2D vector
*\param enc_file : The name of the file that the encoding table is in
*/
void ANN::in_Encoding(char* enc_file)
{
ifstream inf;
inf.open(enc_file);
string number;
string indv_out;
while(getline(inf,number))
{
stringstream ss(number);
vector<double> out;
while(getline(ss,indv_out,' '))
out.push_back(stod(indv_out));
encoding_table.push_back(out);
}
inf.close();
}