-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackprop.cpp
More file actions
executable file
·212 lines (184 loc) · 6.56 KB
/
backprop.cpp
File metadata and controls
executable file
·212 lines (184 loc) · 6.56 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include<iostream>
#include<vector>
#include<cmath>
#include<cstdlib>
#include "layer.cpp"
using namespace std;
class BACKPROP{
private:
double OverAllError;
double MinimumError;
double **ExpectedOutput;
double **Input;
double LearningRate;
double Momentum;
int NumberOfLayers;
int NumberOfSamples;
int SampleNumber;
long int MaximumNumberOfIterations;
void CalculateDelta();
void BackPropError();
void CalculateOverallError();
public:
LAYER *Layer;
double ** ActualOutput;
void FeedForward();
void UpdateWeights();
BACKPROP(int *NumberOfNodes,double **InputSamples,double **OutputSamples,double LearnRate,double Moment,double MinError,long int MaxIter,int nos,int nol);
void TrainNetwork();
double Test(double input[]);
double get_Error(){
CalculateOverallError();
return OverAllError;
}
double dsigmoid(double x){
return x*(1.0 - x);}
};
void BACKPROP::FeedForward(){
int i,j;
for (i = 0; i < Layer[0].NodeLength; i++)
Layer[0].Node[i].Output = Layer[0].Input[i];
//Layer[0].FeedForward();
/* double *xxx=Layer[0].OutputVector();
Layer[1].Input=new double[Layer[0].NodeLength];
for(i=0;i<Layer[0].NodeLength;i++)
{
Layer[1].Input[i]=xxx[i];
}*/
Layer[1].Input = Layer[0].OutputVector();
for (i = 1; i < NumberOfLayers; i++) {
Layer[i].FeedForward();
if (i != NumberOfLayers-1)
{
/*xxx=Layer[i].OutputVector();
Layer[i+1].Input = new double[Layer[i].NodeLength];
for(int f=0;f<Layer[i].NodeLength;f++)
{
Layer[i+1].Input[f]=xxx[f];
}*/
Layer[i+1].Input = Layer[i].OutputVector();
}
//Layer[i+1].Input = Layer[i].OutputVector();
}
}
void BACKPROP::UpdateWeights(){
CalculateDelta();
BackPropError();
}
void BACKPROP::CalculateDelta(){
int i,j,k;
for(i=0;i<Layer[NumberOfLayers-1].NodeLength;i++){
Layer[NumberOfLayers-1].Node[i].Delta=(ExpectedOutput[SampleNumber][i] - Layer[NumberOfLayers-1].Node[i].Output)*dsigmoid(Layer[NumberOfLayers-1].Node[i].Output);
//cout << ExpectedOutput[SampleNumber][i] << endl;
}
for(i = NumberOfLayers -2;i>0;i--){
for(j=0;j<Layer[i].NodeLength;j++){
double sum = 0.0;
for(k=0;k<Layer[i+1].NodeLength;k++){
sum += Layer[i+1].Node[k].Weight[j]*Layer[i+1].Node[k].Delta;
}
double temp;
Layer[i].Node[j].Delta = dsigmoid(Layer[i].Node[j].Output)*sum;
/*for(int x = 0;x<Layer[i-1].NodeLength;x++){
temp = Layer[i].Node[j].Weight[x] + LearningRate*Layer[i].Node[j].SignalError*Layer[i-1].Node[x].Output;
Layer[i].Node[j].Weight[x]=temp;
temp = Layer[i].Node[j].Threshold + LearningRate*Layer[i].Node[j].SignalError;
Layer[i].Node[j].Threshold = temp;
}*/
}
}
}
void BACKPROP::BackPropError(){
int i,j,k;
for(i = NumberOfLayers-1;i>0;i--){
for(j=0;j<Layer[i].NodeLength;j++){
Layer[i].Node[j].ThresholdDiff = LearningRate*Layer[i].Node[j].Delta +Momentum*Layer[i].Node[j].ThresholdDiff;
Layer[i].Node[j].Threshold+=Layer[i].Node[j].ThresholdDiff;
for(k=0;k<Layer[i].InputLength;k++){
Layer[i].Node[j].WeightDiff[k] = LearningRate*Layer[i].Node[j].Delta*Layer[i-1].Node[k].Output + Momentum*Layer[i].Node[j].WeightDiff[k];
Layer[i].Node[j].Weight[k] += Layer[i].Node[j].WeightDiff[k];
}
}
}
}
void BACKPROP::CalculateOverallError(){
int i,j;
OverAllError = 0;
i = SampleNumber-1;
for(j=0;j<Layer[NumberOfLayers-1].NodeLength;j++)
OverAllError+=pow(ExpectedOutput[i][j]-ActualOutput[i][j],2);
//cout<<" i ="<<i<<" j = "<<j<<;
OverAllError/=2.0;
}
BACKPROP::BACKPROP(int NumberOfNodes[],double **InputSamples,double ** OutputSamples,double LearnRate,double Moment,double MinError,long int MaxIter,int nos,int nol){
int i,j;
NumberOfSamples=nos;//sizeof(InputSamples)/sizeof(InputSamples[0]);
MinimumError = MinError;
LearningRate = LearnRate;
Momentum = Moment;
NumberOfLayers = nol;//sizeof(NumberOfNodes)/sizeof(NumberOfNodes[0]);
cout<<NumberOfLayers<<endl;
MaximumNumberOfIterations = MaxIter;
Layer = (LAYER *)malloc(NumberOfLayers*sizeof(LAYER));
Layer[0]=LAYER(NumberOfNodes[0],NumberOfNodes[0]);
for (i = 1; i < NumberOfLayers; i++)
Layer[i] =LAYER(NumberOfNodes[i],NumberOfNodes[i-1]);
Input = new double *[NumberOfSamples];
for(i=0;i<NumberOfSamples;i++)
Input[i]=new double [Layer[0].NodeLength];
ExpectedOutput = new double*[NumberOfSamples];
ActualOutput = new double*[NumberOfSamples];
for(int i=0;i<NumberOfSamples;i++){
ExpectedOutput[i] = new double[Layer[NumberOfLayers-1].NodeLength];
ActualOutput[i] = new double[Layer[NumberOfLayers-1].NodeLength];
}
for (i = 0; i < NumberOfSamples; i++)
for (j = 0; j < Layer[0].NodeLength; j++)
Input[i][j] = InputSamples[i][j];
for (i = 0; i < NumberOfSamples; i++)
for (j = 0; j < Layer[NumberOfLayers-1].NodeLength; j++)
ExpectedOutput[i][j] = OutputSamples[i][j];
TrainNetwork();
}
void BACKPROP::TrainNetwork(){
int i,j;
long int k=0;
do{
for (SampleNumber = 0; SampleNumber < NumberOfSamples; SampleNumber++) {
for (i = 0; i < Layer[0].NodeLength; i++)
{
Layer[0].Input[i] = Input[SampleNumber][i];
//cout << Layer[0].Input[i] << " ";
}
FeedForward();
for (i = 0; i < Layer[NumberOfLayers-1].NodeLength; i++)
ActualOutput[SampleNumber][i] =
Layer[NumberOfLayers-1].Node[i].Output;
UpdateWeights();
}
k++;
CalculateOverallError();
//cout<<OverAllError<<endl;
}while(k < MaximumNumberOfIterations);
}
double BACKPROP::Test(double input[]){
int winner=0;
double *output_nodes;
int nl=NumberOfLayers;
for (int j = 0; j < Layer[0].NodeLength; j++){
Layer[0].Input[j] = input[j];
//cout<<"fine"<<endl;
}
FeedForward();
//cout<<"OK1"<<endl;
output_nodes = Layer[nl-1].OutputVector();
//cout<<"OK2 "<<nl<<endl;
//int n=0;
double nu=0;
//for(int i=0;i<Layer[nl-1].NodeLength;i++){
// nu+=Layer[nl-1].Node[i].Output;}
//cout<<Layer[nl-1].Node[i].Output<<endl;}
//cout<<"OK3 "<<n<<endl;
//cout<<"OK4"<<endl;
return output_nodes[0];
}