-
Notifications
You must be signed in to change notification settings - Fork 0
/
neural_network.cpp
369 lines (326 loc) · 8.87 KB
/
neural_network.cpp
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <ctime>
#include <stdlib.h>
using namespace std;
#define max_error 1e-3 // #define a()
int n;
vector<vector<long double> > neuron;
vector<vector<vector<long double> > > weight;
vector<vector<long double> > weight_b;
// vector<long double> actual_value;
vector<vector<vector<long double> > > error_weight;
vector<vector<long double> > error_neuron;
// long double a;
// long double b;
long double alpha;
long double alpha_rate;
void print_n_value()
{
for (int i = 0; i < neuron.size(); i++)
{
printf("the value in the %d layer\n", i + 1);
for (int j = 0; j < neuron[i].size(); j++)
{
cout << neuron[i][j] << endl;
}
}
return;
}
void making_neurons(vector<int> &num_neurons)
{
neuron.resize(num_neurons.size());
for (int i = 0; i < num_neurons.size(); i++)
{
neuron[i].resize(num_neurons[i], 0);
printf("the neuron in the layer %d are made\n", i + 1);
}
// print_n_value();
return;
}
void resize_error_neuron()
{
error_neuron.resize(n);
for (int i = 0; i < n; i++)
{
error_neuron[i].resize(neuron[i].size(), 0);
}
return;
}
void topo(void)
{
int num_layers;
cout << "Enter the number of layers (including the input and output layers): ";
cin >> num_layers;
n=num_layers;
vector<int> num_neurons(num_layers, 0);
cout << "Enter the number of neurons in each layer: ";
for (int i = 0; i < num_layers; i++)
{
cin >> num_neurons[i];
}
// cout << "how many layer you want(inlcuding the and the output layer input layer)\n";
// cin>>n;
// // n = 5;
// cout << "enter the no of neuron in each layer\n";
// vector<int> net(n, 0);
// for (int i = 0; i < n; i++)
// {
// cin >> net[i];
// }
// // net[0] = 1;
// // net[1] = 2;
// // net[2] = 3;
// // net[3] = 2;
// // net[4] = 1;
// // cout << "the network is 1-2-3-2-1" << endl;
// for (int i = 0; i < n; i++)
// {
// cout << net[i] << endl;
// }
making_neurons(num_neurons);
resize_error_neuron();
return;
}
void input_layer(const vector<long double>&input)
{
// cout << "input the input layer vector" << endl;
for (int i = 0; i < neuron[0].size(); i++)
{
neuron[0][i] = input[i];
}
// print_n_value();
return;
}
void resize_error_weight(vector<vector<vector<long double>>> &error_weight)
{
error_weight.resize(n - 1);
for (int i = 0; i < n - 1; i++)
{
error_weight[i].resize(neuron[i + 1].size());
for (int j = 0; j < error_weight[i].size(); j++)
{
error_weight[i][j].resize(neuron[i].size(), 0);
}
}
return;
}
void print_w_values()
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < weight[i].size(); j++)
{
// printf("the value of the weights for the neurons in the %d layer and for the %d neuron are\n", i + 1, j + 1);
for (int k = 0; k < weight[i][j].size(); k++)
{
cout << weight[i][j][k] << endl;
}
}
}
return;
}
void initialize_weights()
{
// weight.resize(neuron.size() - 1);
// for (int i = 0; i < n - 1; i++)
// {
// weight[i].resize(neuron[i + 1].size());
// for (int j = 0; j < weight[i].size(); j++)
// {
// weight[i][j].resize(neuron[i].size(), 0);
// }
// }
resize_error_weight(weight);
// std::transform(weight.begin(),weight.end(),weight.begin(),rand_());
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < weight[i].size(); j++)
{
for (int k = 0; k < weight[i][j].size(); k++)
{
weight[i][j][k] = ((long double)rand() / (RAND_MAX));
// weight[i][j][k]=(long double) (b);
}
}
}
// print_w_values();
return;
}
void print_baise_weight()
{
for (int i = 0; i < n; i++)
{
printf("the value of the baise terms for %d layer\n", i + 1);
for (int j = 0; j < weight_b[i].size(); j++)
{
cout << weight_b[i][j] << endl;
}
}
return;
}
void initialize_biases()
{
// baise.resize(n, 1);
weight_b.resize(n);
for (int i = 0; i < n - 1; i++)
{
weight_b[i].resize(n * neuron[i + 1].size(), 0);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < weight_b[i].size(); j++)
{
weight_b[i][j] = ((long double)rand() / (RAND_MAX));
// weight_b[i][j] = (long double)(b);
}
}
// print_baise_weight();
return;
}
long double activation_fn(long double demo)
{
// here the activation fn is sigmoid
// return ((1.0) / (1 + exp((-1) * demo)));
return tanh(demo);
}
void forward_propagation()
{
long double demo = 0;
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < neuron[i + 1].size(); j++)
{
demo = weight_b[i][j];
for (int k = 0; k < neuron[i].size(); k++)
{
demo += neuron[i][k] * weight[i][j][k];
}
demo = activation_fn(demo);
neuron[i + 1][j] = demo;
}
demo = 0.0;
}
// print_n_value();
return;
}
long double error_nn(const vector<long double> &target)
{
long double error = 0.0;
for (int i = 0; i < neuron[n - 1].size(); i++)
{
long double diff = neuron[n - 1][i] - target[i];
error_neuron[n - 1][i] = 0.5 * diff * diff;
error = max(error_neuron[n - 1][i],error);
}
// cout<<"the error calculated"<<endl;
return error;
}
long double dif_activation_fn(long double n_value)
{
// this function returns the value of the diffential activation
long double demo;
demo = (1 - pow(n_value, 2));
// demo = (1 - n_value) * n_value;
return demo;
// return 1- pow(n_value,2);
}
void cal_new_weight() //this fuction calculate the new weights for in the back propagration step
{
for (int i = n - 1; i >= 1; i--)
{
for (int j = 0; j < neuron[i].size(); j++)
{
for (int k = 0; k < neuron[i - 1].size(); k++)
{
long double demo = error_neuron[i][j] * (neuron[i - 1][k] * dif_activation_fn(neuron[i][j]));
weight[i - 1][j][k] -= (alpha * demo);
}
}
}
// std::cout << "new weights done" << std::endl;
return;
}
void cal_new_baise_weight() // this calculates the new baise in the back propagation step
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < neuron[i + 1].size(); j++)
{
long double demo = dif_activation_fn(neuron[i + 1][j]);
demo = (error_neuron[i + 1][j]) * (demo);
weight_b[i][j] -= alpha * demo;
}
}
return;
}
void cal_error_neuron() // this function calcutate the error in the neurons
{
long double demo = 0.0;
for (int i = n - 1; i >0; i--)
{
for (int j = 0; j < neuron[i - 1].size(); j++)
{
demo = 0;
for (int k = 0; k < neuron[i].size(); k++)
{
demo += error_neuron[i][k] * (weight[i - 1][k][j] * dif_activation_fn(neuron[i][j]));
}
error_neuron[i - 1][j] = demo;
// neuron[i - 1][j] -= (alpha * demo);
}
}
// print_n_value();
// cout << "error of the neurons r calculated" << endl;
// error_neuron[0].resize(neuron[0].size(),0);
// print_error_n();
return;
}
void backpropagation()
{
cal_error_neuron();
cal_new_weight();
cal_new_baise_weight();
return;
}
// make a error vector for the weights for the backprop
// i can have dynamic programming to store the value of the error or vactor
// dynamic gradient step value alpha
void train_network(const vector<vector<long double> >& inputs, const vector<vector<long double> >& targets,int epochs) {
// long double demo;
for(int z=0;z<epochs;z++)
{
long double demo;
cout<<endl<<endl << "Epoch: " << z+1 << endl;
for (int i = 0; i < inputs.size(); i++) {
input_layer(inputs[i]);
forward_propagation();
// print_n_value();
demo = error_nn(targets[i]);
// cout<<demo;
backpropagation();
// print_n_value();
}
cout<<"max error="<<demo<<endl;
alpha =alpha-(alpha* alpha_rate);
// epoch++;
}
}
int main()
{
srand(time(nullptr));
topo();
initialize_weights();
initialize_biases();
alpha = 0.1;
alpha_rate = 0.001;
vector<vector<long double> > inputs = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
vector<vector< long double> > targets = {{0}, {1}, {1}, {0}};
cout<<"input the number of epochs"<<endl;
int epochs;
cin>>epochs;
train_network(inputs, targets, epochs);
return 0;
}