-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNet.h
More file actions
120 lines (102 loc) · 2.8 KB
/
Copy pathNet.h
File metadata and controls
120 lines (102 loc) · 2.8 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
#ifndef NET_H
#define NET_H
#include <string>
#include <random>
#include "Layers.h"
class Net{
Loss *lossFunc = nullptr;
Layer **layers = nullptr;
int depth, current;
double learnRate = 0.001;
double regStrength = 0.00001;
double learnRateDecay = 0.9999;
int batchSize = 200;
int num_iter = 5000;
public:
Net(Loss *lossFunc, int numLayers): depth(numLayers), current(0){
this->lossFunc = lossFunc;
layers = new Layer*[numLayers];
}
void push(Layer *layer){
// if(current >= depth) return;
layers[current++] = layer;
}
int predict(Vector &data){
Vector temp(data);
for(int i=0; i<depth; ++i) layers[i]->forward(temp);
double score = temp[0];
int index = 0;
for(int i=1; i<temp.length(); ++i){
if(temp[i] > score){
index = i;
score = temp[i];
}
}
return index;
}
void update(){
for(int i=0; i<depth; ++i) layers[i]->update(learnRate, regStrength);
learnRate *= learnRateDecay;
}
/*double validate(Matrix &valid, Vector &labels){
int guess, correct = 0, index;
std::random_device rd;
std::mt19937 rand(rd());
for(int i=0; i<1000; ++i){
index = rand()%valid.yDim();
guess = predict(valid[index]);
if(guess == (int)labels[index]) ++correct;
}
return (double)correct/10;
}*/
void train(Matrix &data, Vector &labels, Matrix &valid, Vector &validLabels){
Vector current;
std::random_device rd;
std::mt19937 rand(rd());
double score, loss = 0, validPercent;
int guess, correct = 0;
// for(int i=0; i<depth; ++i) layers[i]->isTraining(true);
for(int i=0, index; i<num_iter*batchSize; ++i){
index = rand()%data.yDim();
current.copy(data[index]);
lossFunc->setCorrect(labels[index]);
for(int j=0; j<depth; ++j) layers[j]->forward(current);
lossFunc->forward(current);
/*score = current[0];
guess = 0;
for(int j=1; j<current.length(); ++j){
if(current[j] > score){
score = current[j];
guess = j;
}
}
if(guess == (int)labels[index]) ++correct;*/
current.fill(0);
lossFunc->backward(current);
loss+=lossFunc->getLoss();
for(int j=depth-1; j>=0; --j) layers[j]->backward(current);
if((i+1)%batchSize == 0){
update();
// validPercent = validate(valid, validLabels);
std::cout << (i+1)/batchSize <<
"\nBatch Loss: " << (double)loss/batchSize <<
// "\nTest Accuracy: " << (double)100.0*correct/batchSize <<
// "\nValidation Accuracy: " << validPercent <<
"\nLearn Rate: " << learnRate << "\n\n";
// correct = 0;
if((i+1)/batchSize == num_iter>>1){
for(int i=0; i<depth; ++i) layers[i]->isTraining(true);
std::cout << "Applying dropout\n";
}
loss = 0;
}
}
for(int i=0; i<depth; ++i) layers[i]->isTraining(false);
}
~Net(){
for(int i=0; i<depth; ++i) delete layers[i];
delete[] layers;
delete lossFunc;
}
};
#endif