-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainv1.cpp
More file actions
193 lines (173 loc) · 6.29 KB
/
Copy pathmainv1.cpp
File metadata and controls
193 lines (173 loc) · 6.29 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
193
#include "DeepLearning.h"
// Setting Epochs, LearningRate and so on
bool quiet = false;
bool debugMode = true;
double LearningRate = 0.001;
double LearningRatio = 0.95;
int LearningChange = 1;
// we use adam to fasten training
// will use corrected error
bool adam = true;
double momCoff = 0.9;
double rmsCoff = 0.99;
double minStep = eps;
double maxStep = 1e5;
double regular = 1e-4;
int TrainData = 60000;
int TestData = 10000;
int inputWidth = 28;
int inputHeight = 28;
// number of choice each task
int outputWidth = 10;
// A epoch means running through whole training set
int maxEpoch = 4;
// Because of multiple thread, please go to DeepLearning.h
// to change batchSize
// int batchSize = 60;
// To decide how many iter between show changes
// must be no bigger than maxIter
int show = 20;
int type = MNIST;
double pic[maxWidth][maxHeight];
int label[maxNumLabel];
FILE* fpDebug = NULL;
FILE* fpResult = NULL;
ifstream* fpTrain = NULL;
ifstream* fpTest = NULL;
ifstream* fpTrainLabel = NULL;
ifstream* fpTestLabel = NULL;
vector<int> ran;
thread thr[batchSize];
const char strShow[2] = "+";
int main(){
readData(type);
InputLayer inp(fpTrain,fpTest,TrainData,TestData,1,inputHeight,inputWidth);
ThirdSpanLayer span(1, inputHeight, inputWidth);
LinearLayer line(inputHeight * inputWidth, 128);
ReLULayer rel(128, 1);
LinearLayer line2(128, outputWidth);
SoftMaxLayer soft(outputWidth, 1);
OutputLayer out(fpTrainLabel,fpTestLabel,TrainData,TestData,outputWidth);
if(!quiet){
printf("Training...\n");
fprintf(fpResult, "Training...\n");
fflush(fpResult);
}
clock_t begin = clock();
double iterTime = 0;
double epochTime = 0;
for(int k = 0;k < maxEpoch;k++){
// Training
if(!quiet){
if(!quiet) printf("Epochs %d : ",k);
if(debugMode) printf("\n");
if(!quiet) fprintf(fpResult,"Epochs %d : ",k);
if(debugMode) fprintf(fpResult,"\n");
fflush(stdout);
}
DecreaseLearningRate(k);
inp.InitShuffle();
// Number of iters in an epoch
int maxIter = TrainData / batchSize;
double oldTotalLoss = 0;
int tar = 0;
for(int i = 0;i < maxIter;i++){
if((!debugMode) && (!quiet) && (i % show == 0) && (i != 0)){
printf("%s",strShow);
fflush(stdout);
}
tar = i * batchSize;
inp.forward(tar);
span.forward(inp.output);
line.forward(span.output);
rel.forward(line.output);
line2.forward(rel.output);
soft.forward(line2.output);
out.forward(tar,soft.output);
out.backward(tar);
soft.backward(out.error);
line2.backward(soft.error);
rel.backward(line2.error);
line.backward(rel.error);
if(debugMode && (i % show == (show - 1))){
double showLoss = (out.totalLoss - oldTotalLoss) / show;
double showAcc = out.accuracy / (show * batchSize);
clock_t iter = clock();
double newIter = (double)(iter - begin) / CLOCKS_PER_SEC;
double usedTime = newIter - iterTime;
printf("in iteration %d:\t",i);
printf("Training Loss = %llf ",showLoss);
printf("Train Accuracy = %llf ",showAcc);
printf("Used Time = %llf\n", usedTime);
fprintf(fpResult,"in iteration %d: ",i);
fprintf(fpResult,"Training Loss = %llf ",showLoss);
fprintf(fpResult,"Train Accuracy = %llf ",showAcc);
fprintf(fpResult,"Used Time = %llf\n", usedTime);
fprintf(fpDebug,"%llf, %llf, %llf; ", showLoss, showAcc, usedTime);
fflush(fpDebug);
fflush(fpResult);
oldTotalLoss = out.totalLoss;
iterTime = newIter;
out.accuracy = 0;
}
}
// Testing
if(debugMode){
printf("Testing : ");
fflush(stdout);
}
out.totalLoss = 0;
for(int i = 0;i < maxIter;i++){
if((!quiet) && (i % show == 0) && (i != 0)){
printf("%s",strShow);
fflush(stdout);
}
tar = i * batchSize;
inp.forward(tar);
span.forward(inp.output);
line.forward(span.output);
rel.forward(line.output);
line2.forward(rel.output);
soft.forward(line2.output);
out.forward(tar,soft.output);
out.backward(tar);
}
out.ComputeTrainAccuracy();
double trainAcc = out.accuracy;
int testIter = TestData / batchSize;
for(int i = 0;i < testIter;i++){
if(i * batchSize == TestData){
break;
}
if((!quiet) && (i % show == 0) && (i != 0)){
printf("%s",strShow);
fflush(stdout);
}
tar = i * batchSize;
inp.StartTest(tar);
span.forward(inp.output);
line.forward(span.output);
rel.forward(line.output);
line2.forward(rel.output);
soft.forward(line2.output);
out.TestForward(tar,soft.output);
}
out.ComputeAccuracy();
if(!quiet){
clock_t epoch = clock();
double newEpoch = (double)(epoch - begin) / CLOCKS_PER_SEC;
double usedTime = newEpoch - epochTime;
printf("\nTraining Accuracy : %llf\n",trainAcc);
fprintf(fpResult, "\nTraining Accuracy : %llf\n",trainAcc);
printf("Training Loss : %llf\n",out.totalLoss / maxIter);
fprintf(fpResult, "Training Loss : %llf\n",out.totalLoss / maxIter);
printf("Test accuracy : %llf\n",out.accuracy);
fprintf(fpResult, "Test accuracy : %llf\n",out.accuracy);
printf("Used Time = %llf\n", usedTime);
fflush(fpResult);
epochTime = newEpoch;
}
iterTime = epochTime;
out.totalLoss = 0;
}
}