-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutputPictureLayer.cpp
More file actions
151 lines (139 loc) · 4.52 KB
/
Copy pathOutputPictureLayer.cpp
File metadata and controls
151 lines (139 loc) · 4.52 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
#include "DeepLearning.h"
// int choice;
// int inpWidth;
// int inpHeight;
// int batch;
// int numTestData;
// double totalLoss;
// Array trainPredict;
// Array trainTruth;
// Array testPredict;
// Array testTruth;
// Array loss;
// Array error;
OutputPictureLayer::OutputPictureLayer(ifstream* fpTra, ifstream* fpTes, int numTrain, int numTest,int od,int oh,int ow){
outDepth = od;
outHeight = oh;
outWidth = ow;
numTrainData = numTrain;
numTestData = numTest;
//loss will be compute while backward propagation
trainLoss.resize(numTrainData);
testLoss.resize(numTestData);
trainRMSE.resize(numTrainData);
testRMSE.resize(numTestData);
ThirdArray tmp(outDepth,outHeight,outWidth);
for(int i = 0;i < batchSize;i++){
error.push_back(tmp);
}
for(int i = 0;i < numTrainData;i++){
trainPredict.push_back(tmp);
}
for(int i = 0;i < numTestData;i++){
testPredict.push_back(tmp);
}
for(int i = 0;i < numTrainData;i++){
GetPicture(fpTra);
ThirdArray dat(outDepth,outHeight,outWidth);
for(int j = 0;j < outDepth;j++){
for(int k = 0;k < outHeight;k++){
for(int l = 0;l < outWidth;l++){
dat.thiArr[j].arr[k][l] = (double)pic[k][l] / 256.0;
}
}
}
trainTruth.push_back(dat);
}
if(!quiet){
printf("Training label Successfully loaded...\n");
fprintf(fpResult, "Training label Successfully loaded...\n");
fflush(fpResult);
}
for(int i = 0;i < numTestData;i++){
GetPicture(fpTes);
ThirdArray dat(outDepth,outHeight,outWidth);
for(int j = 0;j < outDepth;j++){
for(int k = 0;k < outHeight;k++){
for(int l = 0;l < outWidth;l++){
dat.thiArr[j].arr[k][l] = (double)pic[k][l] / 256.0;
}
}
}
testTruth.push_back(dat);
}
if(!quiet){
printf("Testing label Successfully loaded...\n");
fprintf(fpResult, "Testing label Successfully loaded...\n");
fflush(fpResult);
}
//testTruth.PrintArray(fpDebug);
}
void OutputPictureLayer::backward(int tar){
// Use loss function of 1/N * 1/2 * (a - y)^2
for(int i = 0;i < batchSize;i++){
int iter = ran[tar + i];
error[i] = trainPredict[iter] - trainTruth[iter];
trainLoss[iter] = DotProduct(error[i], error[i]).addTogether();
trainRMSE[iter] = sqrt(trainLoss[iter]);
trainLoss[iter] /= (2.0 * batchSize);
trainRMSE[iter] /= (1.0 * batchSize);
totalTrainLoss += trainLoss[iter];
totalTrainRMSE += trainRMSE[iter];
}
for(int i = 0;i < batchSize;i++){
if(!error[i].CheckFinite()){
fprintf(fpDebug, "In OutputLayer : Array called error\n");
fflush(fpDebug);
exit(0);
}
//error[i].Bound(minStep,maxStep);
}
bool check = true;
for(int j = 0;j < batchSize;j++){
if(!isfinite(trainLoss[j])){
check = false;
}
}
if(!check){
fprintf(fpDebug, "In OutputLayer : Array called eachLoss\n");
fflush(fpDebug);
exit(0);
}
return;
}
void OutputPictureLayer::forward(int tar,const vector<ThirdArray>& inp){
for(int i = 0;i < batchSize;i++){
if(tar + i > TrainData){
fprintf(fpDebug,"Unable to read batch label for training.\n");
exit(0);
}
int iter = ran[tar + i];
trainPredict[iter] = inp[i];
}
return;
}
void OutputPictureLayer::TestForward(int tar,const vector<ThirdArray>& inp){
for(int i = 0;i < batchSize;i++){
if(tar + i > TestData){
fprintf(fpDebug,"Unable to read batch label for testing.\n");
exit(0);
}
testPredict[tar + i] = inp[i];
}
return;
}
void OutputPictureLayer::ComputeTestLoss(void){
totalTestLoss = 0;
totalTestRMSE = 0;
int bound = numTestData - numTestData % batchSize;
for(int i = 0;i < bound;i++){
ThirdArray err = testPredict[i] - testTruth[i];
testLoss[i] = DotProduct(err, err).addTogether();
testRMSE[i] = sqrt(testLoss[i]);
testLoss[i] /= (2.0 * batchSize);
testRMSE[i] /= (1.0 * batchSize);
totalTestLoss += testLoss[i];
totalTestRMSE += testRMSE[i];
}
return;
}