-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathANN.cpp
376 lines (313 loc) · 9.6 KB
/
ANN.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
370
371
372
373
374
375
376
/**
* @file ANN.cpp
*
* @author RogerNi(NI Ronghao)
* Contact: [email protected]
*
*/
#include "ANN.h"
#include <algorithm>
#include <iostream>
#include <vector>
#include <random>
#include <cmath>
#include <fstream>
#include <string>
#include <sstream>
#include <ctime>
#include <chrono>
ANN::ANN(std::vector<int> ns_of_l) : testX(nullptr), testY(nullptr), auto_save(false), cfg(ns_of_l)
{
auto seed = std::chrono::system_clock::now().time_since_epoch().count();//seed
std::default_random_engine dre(seed);//engine
std::uniform_real_distribution<float> di(-1, 1);//distribution
for (int i = 0; i < ns_of_l.size() - 1; ++i) {
//weights.push_back(Matrix(ns_of_l.at(i) + 1, ns_of_l.at(i + 1)));
std::vector<float> v1(ns_of_l.at(i)*ns_of_l.at(i + 1));
std::generate(v1.begin(), v1.end(), [&] { return di(dre); });
weights.push_back(v1);
std::vector<float> v2(ns_of_l.at(i + 1));
std::generate(v2.begin(), v2.end(), [&] { return di(dre); });
biases.push_back(v2);
}
}
ANN::ANN(std::string inputPath) : testX(nullptr), testY(nullptr), auto_save(false)
{
std::string line;
std::ifstream infile(inputPath);
if (infile.is_open())
{
std::vector<std::vector<float>> temp_mat;
int lastLayer = 0;
// read cfg
std::getline(infile, line);
std::stringstream line_stream(line);
std::string token;
std::vector<float> temp_row;
while (line_stream >> token)
{
cfg.push_back(std::stof(token));
}
// read weights
for (int i = 0; i < cfg.size()-1;++i)
{
std::getline(infile, line);
std::vector<float> w;
std::stringstream line_stream(line);
std::string token;
std::vector<float> temp_row;
while (line_stream >> token)
{
w.push_back(std::stof(token));
}
weights.push_back(w);
}
// read bias
for (int i = 0; i < cfg.size() - 1; ++i)
{
std::getline(infile, line);
std::vector<float> b;
std::stringstream line_stream(line);
std::string token;
std::vector<float> temp_row;
while (line_stream >> token)
{
b.push_back(std::stof(token));
}
biases.push_back(b);
}
}
}
std::vector<float> ANN::predict(const std::vector<float>& input)
{
std::vector<std::vector<float>> as;
const std::vector<float> * lastIn = &input;
for (int c = 0; c < cfg.size() - 1; ++c)
{
as.push_back(sigmoid(dot(*lastIn, weights[c], 1, cfg[c], cfg[c + 1]) + biases[c]));
lastIn = &as[as.size() - 1];
}
return *(--(as.end()));
}
int ANN::positionPredict(std::vector<float>& input)
{
std::vector<float> out = predict(input);
return std::distance(out.begin(), std::max_element(out.begin(), out.end()));
}
void ANN::writeTo(std::string outPath)
{
std::ofstream outFile(outPath);
for (auto & c: cfg)
{
outFile << c << "\t";
}
outFile << "\n";
for (auto & w : weights)
{
for (auto & num : w)
{
outFile << num << "\t";
}
outFile << "\n";
}
for (auto & b: biases)
{
for (auto & num: b)
{
outFile << num << "\t";
}
outFile << "\n";
}
}
std::vector<float> ANN::dot(const std::vector<float>& m1, const std::vector<float>& m2, const int m1_rows, const int m1_columns, const int m2_columns)
{
std::vector <float> output(m1_rows*m2_columns);
#pragma omp parallel for
for (int row = 0; row < m1_rows; ++row) {
for (int col = 0; col != m2_columns; ++col) {
output[row * m2_columns + col] = 0.f;
for (int k = 0; k != m1_columns; ++k) {
output[row * m2_columns + col] += m1[row * m1_columns + k] * m2[k * m2_columns + col];
}
}
}
return output;
}
std::vector<float> operator-(const std::vector<float>& m1, const std::vector<float>& m2)
{
const unsigned long VECTOR_SIZE = m1.size();
std::vector <float> difference(VECTOR_SIZE);
for (unsigned i = 0; i != VECTOR_SIZE; ++i) {
difference[i] = m1[i] - m2[i];
};
return difference;
}
void ANN::setTestData(std::vector<std::vector<float>>& x, std::vector<std::vector<float>>& y)
{
testX = &x;
testY = &y;
}
float ANN::testAccuracy()
{
float error = 0;
#pragma omp parallel for
for (int i = 0; i < testX->size(); ++i) {
if (this->positionPredict((*testX)[i]) != std::distance((*testY)[i].begin(), std::find((*testY)[i].begin(), (*testY)[i].end(), 1)))
{
#pragma omp critical
error++;
}
}
return 1 - (error / testX->size());
}
void ANN::set_auto_save(bool save)
{
auto_save = save;
}
void ANN::train( std::vector<std::vector<float>>& input, std::vector<std::vector<float>>& base, float lr, int epoch, int batch_size)//input data, output data,learing rate, epoch and batch size
{
std::cout << "Learning rate: " << lr << std::endl;
std::cout << "Epoch Num: " << epoch << std::endl;
std::cout << "Minibatch_size: " << batch_size << std::endl;
int inputSize = input.size();
int batch_num = input.size() / batch_size;
std::cout << "Batch number: " << batch_num << std::endl;
if (input.size() % batch_size != 0) {
std::cout << "Batch Size Invalid!" << std::endl;
return;
}
for (int e = 0; e < epoch; ++e) {
auto seed = unsigned(std::time(0));
std::srand(seed);
std::random_shuffle(input.begin(), input.end());
std::srand(seed);
std::random_shuffle(base.begin(), base.end());
float epoch_loss = 0;
//clock_t tStart = clock();
std::chrono::steady_clock sc;
auto start = sc.now();
for (int i = 0; i < batch_num; i++)
{
//std::cout << "batch " << i << " :" << std::endl;
//int j = (rand() % (batch_num)) + 0;
//std::cout << j;
std::vector<float> b_X;
std::vector<float> b_y;
for (int j = 0;j<batch_size;++j)
{
b_X.insert(b_X.end(), input[i*batch_size + j].begin(), input[i*batch_size + j].end());
b_y.insert(b_y.end(), base[i*batch_size + j].begin(), base[i*batch_size + j].end());
}
epoch_loss += trainOneBatch(b_X, b_y, batch_size, lr);
}
auto end = sc.now();
auto time_span = static_cast<std::chrono::duration<double>>(end - start);
printf("Time taken for this epoch: %.2fs\n", time_span.count());
epoch_loss /= batch_size;
std::cout << "Train Loss: " << epoch_loss << std::endl;
if (testX != nullptr)
std::cout << "Test Accuracy: " << testAccuracy() << std::endl;
if (auto_save)
{
std::string file = "model_auto_saved_at_" + std::to_string(e + 1);
this->writeTo(file);
std::cout << "Model saved to " << file << std::endl;
}
}
//std::cout << batch[0][1];
}
std::vector<float> ANN::sigmoid(const std::vector<float>& m1)
{
const unsigned long VECTOR_SIZE = m1.size();
std::vector <float> output(VECTOR_SIZE);
for (unsigned i = 0; i != VECTOR_SIZE; ++i) {
output[i] = 1 / (1 + exp(-m1[i]));
}
return output;
}
std::vector<float> ANN::sigmoid_d(const std::vector<float>& m1)
{
const unsigned long VECTOR_SIZE = m1.size();
std::vector <float> output(VECTOR_SIZE);
for (unsigned i = 0; i != VECTOR_SIZE; ++i) {
output[i] = m1[i] * (1 - m1[i]);
}
return output;
}
float ANN::trainOneBatch(const std::vector<float>& input, const std::vector<float>& base,int batch_size, float lr)
{
// Feed forward
std::vector<std::vector<float>> as;
as.push_back(input);
const std::vector<float> * lastIn = &input;
for (int c = 0; c < cfg.size()-1; ++c)
{
std::vector<float> temp_bias;
for (int b = 0; b< batch_size;++b)
{
temp_bias.insert(temp_bias.end(), biases[c].begin(), biases[c].end());
}
as.push_back(sigmoid(dot(*lastIn, weights[c], batch_size, cfg[c], cfg[c + 1])+ temp_bias));
lastIn = &as[as.size() - 1];
}
// Back propagation
std::vector<float> dyhat = (as[as.size()-1] - base);
std::vector<std::vector<float>> dWs;
std::vector<std::vector<float>> dbs;
std::vector<std::vector<float>> dzs;
std::vector<float> * lastZ = &dyhat;
float loss = 0.0;
for (unsigned k = 0; k < batch_size * cfg[cfg.size()-1]; ++k) {
loss += dyhat[k] * dyhat[k];
}
loss /= batch_size;
for (int c = cfg.size() -2 ; c >=0;--c)
{
dWs.push_back(dot(transpose(&((as[c])[0]), batch_size, cfg[c]), *lastZ, cfg[c], batch_size, cfg[c+1]));
dbs.push_back(dot(std::vector<float>(batch_size,1), *lastZ, 1, batch_size, cfg[c + 1]));
if (c==0)
break;
dzs.push_back(dot(*lastZ, transpose(&((weights[c])[0]), cfg[c], cfg[c+1]), batch_size, cfg[c+1], cfg[c]) * sigmoid_d(as[c]));
lastZ = &dzs[dzs.size() - 1];
}
// Updating the parameters
for (int c = 0; c < cfg.size() - 1; ++c)
{
weights[cfg.size() - 2 - c] = weights[cfg.size() - 2 - c] - lr * dWs[c];
biases[cfg.size() - 2 - c] = biases[cfg.size() - 2 - c] - lr * dbs[c];
}
return loss;
}
std::vector <float> transpose(float *m, const int C, const int R) {
std::vector <float> mT(C*R);
for (unsigned n = 0; n != C * R; n++) {
unsigned i = n / C;
unsigned j = n % C;
mT[n] = m[R*j + i];
}
return mT;
}
std::vector <float> operator+(const std::vector <float>& m1, const std::vector <float>& m2) {
const unsigned long VECTOR_SIZE = m1.size();
std::vector <float> sum(VECTOR_SIZE);
for (unsigned i = 0; i != VECTOR_SIZE; ++i) {
sum[i] = m1[i] + m2[i];
};
return sum;
}
std::vector <float> operator*(const std::vector <float>& m1, const std::vector <float>& m2) {
const unsigned long VECTOR_SIZE = m1.size();
std::vector <float> product(VECTOR_SIZE);
for (unsigned i = 0; i != VECTOR_SIZE; ++i) {
product[i] = m1[i] * m2[i];
};
return product;
}
std::vector <float> operator*(const float m1, const std::vector <float>& m2) {
const unsigned long VECTOR_SIZE = m2.size();
std::vector <float> product(VECTOR_SIZE);
for (unsigned i = 0; i != VECTOR_SIZE; ++i) {
product[i] = m1 * m2[i];
};
return product;
}