-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
278 lines (228 loc) · 5.83 KB
/
Copy pathmain.cpp
File metadata and controls
278 lines (228 loc) · 5.83 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
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
#include <math.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
const double LR = 0.7;
const double LR_HO = 0.07;
const int tryTime = 1000;
const int situation = 4;
int selected_situation;
const int inputNumber = 2;
const int neuronLayer = 1;
const int neuronNumber = 2;
const int outNumber = 1;
double input[situation][inputNumber];
double inputWeight[situation][inputNumber][neuronNumber];
double neuron[neuronLayer][neuronNumber];
double neuronWeight[neuronLayer][neuronNumber];
double neuronOutWeight[1][neuronNumber][outNumber];
double neuronOut[neuronLayer][neuronNumber];
double bias[neuronLayer + outNumber];
double out[1][outNumber];
double outOutput[outNumber];
double error[outNumber];
double target[situation];
double totalError;
//Functions
double randomize();
double sigmoid(double h);
void calcErrorOfOut();
void setInputs();
void setInputWeights();
void setNeuron();
void setNeuronWeights();
void setNeuronOut();
void setBias();
void setOut();
void setError();
void netHidden(int layer, int layerNeuron);
void outHidden(int layer, int layerNeuron);
void netOutput(int layer, int layerNeuron);
void outOfOutput();
int main()
{
selected_situation = 3;
setInputs();
setInputWeights();
setBias();
setNeuron();
setNeuronWeights();
setNeuronOut();
setOut();
setError();
for(int i = 0; i < neuronLayer; i++)
{
netHidden(i, neuronNumber);
}
outHidden(neuronLayer, neuronNumber);
netOutput(neuronLayer, neuronNumber);
outOfOutput();
calcErrorOfOut();
system("PAUSE");
return 0;
}
double randomize()
{
return double(rand() / double(RAND_MAX));
}
void setInputs()
{
input[0][0] = 0.0;
input[0][1] = 0.0;
target[0] = 0.0;
input[1][0] = 0.0;
input[1][1] = 1.0;
target[1] = 1.0;
input[2][0] = 1.0;
input[2][1] = 0.0;
target[2] = 1.0;
input[3][0] = 1.0;
input[3][1] = 1.0;
target[3] = 0.0;
}
void setInputWeights()
{
for(int i = 0; i < situation; i++)
{
for(int j = 0; j < inputNumber; j++)
{
for(int k = 0; k < (neuronNumber); k++)
{
inputWeight[i][j][k] = randomize();
cout << (i+1) << ". Situation " << (i*neuronNumber)+j << ". Input Weights:" << inputWeight[i][j][k] << endl;
}
}
}
}
void setBias()
{
for(int i = 0; i < (neuronLayer+outNumber); i++)
{
bias[i] = randomize();
cout << (i+1) << ". bias :" << bias[i] << endl;
}
}
void setNeuron()
{
for(int i = 0; i < neuronLayer; i++)
{
for(int j = 0; j < neuronNumber; j++)
{
neuron[i][j] = 0.0;
}
}
}
void setNeuronWeights()
{
for(int i = 0; i < neuronLayer; i++)
{
for(int j = 0; j < neuronNumber; j++)
{
neuronWeight[i][j] = randomize();
cout << (i+1) << ". Layer " << (i*neuronNumber)+j << ". Neuron Weight:" << neuronWeight[i][j] << endl;
}
}
for (int i = 0; i < neuronNumber; ++i)
{
for(int j = 0; j < outNumber; j++)
{
neuronOutWeight[0][i][j] = randomize();
cout << (i+1) << ". Neuron Out Weight: " << neuronOutWeight[0][i][j] << endl;
}
}
}
void setNeuronOut()
{
for(int i = 0; i < neuronLayer; i++)
{
for(int j = 0; j < neuronNumber; j++)
{
neuronOut[i][j] = randomize();
}
}
}
void setOut()
{
for (int i = 0; i < outNumber; i++)
{
out[0][i] = 0.0;
}
}
void setError()
{
for (int i = 0; i < outNumber; ++i)
{
error[i] = 0.0;
}
}
void netHidden(int layer, int layerNeuron)
{
if(layer > 0)
{
for(int i = 0; i < layerNeuron; i++)
{
for(int j = 0; j < layerNeuron; j++)
{
neuron[layer][i] += neuronOut[layer-1][j] * neuronWeight[layer-1][j] + bias[layer];
cout << layer << ". Layer " << ((i*layerNeuron)+layerNeuron) << ". Net Neuron:" << neuron[layer][i];
}
}
}else
{
for(int i = 0; i < inputNumber; i++)
{
for(int j = 0; j < layerNeuron; j++)
{
neuron[layer][j] += input[selected_situation][i] * inputWeight[selected_situation][i][j] + bias[layer];
cout << layer+1 << ". Layer " << ((i*layerNeuron)+j) << ". Net Neuron: " << neuron[layer][j] << endl;
}
}
}
}
void outHidden(int layer, int layerNeuron)
{
for(int i = 0; i < layer; i++)
{
for(int j = 0; j < layerNeuron; j++)
{
neuronOut[i][j] = sigmoid(neuron[i][j]);
cout << (i+1) << ". Layer " << ((i*layerNeuron)+j) << ". Out Neuron:" << neuronOut[i][j] << endl;
}
}
}
void netOutput(int layer, int layerNeuronOut)
{
for(int i = 0; i < outNumber; i++)
{
for(int j = 0; j < layerNeuronOut; j++)
{
out[0][i] += neuronOut[layer - 1][j] * neuronOutWeight[0][i][j] + bias[neuronLayer];
cout << i+1 << ". Net Out: " << out[0][i] << " Neuron Out : " << neuronOut[layer-1][j] << " Neuron Out weight :" << neuronOutWeight[0][i][j] << endl;
}
}
}
void outOfOutput()
{
for(int i = 0; i < outNumber; i++)
{
outOutput[i] = sigmoid(out[0][i]);
cout << i+1 << ". Out of Output: " << outOutput[i] << endl;
}
}
void calcErrorOfOut()
{
totalError = 0.0;
for(int i = 0; i < outNumber; i++)
{
totalError += (0.5)*(pow((target[selected_situation] - outOutput[i]), 2.0));
cout << i+1 << ". Out Of Output Error : " << (0.5)*(pow((target[selected_situation] - outOutput[i]), 2.0)) << " target:" << target[selected_situation] << endl;
}
cout << "Total Error: " << totalError << endl;
}
void backPropagation()
{
}
double sigmoid(double h)
{
return (1/(1+exp(-h)));
}