-
Notifications
You must be signed in to change notification settings - Fork 0
/
red_neuronal.c
162 lines (140 loc) · 5.08 KB
/
red_neuronal.c
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
/*
* red_neuronal.c
*
* Created on: 17 mar. 2021
* Author: Nahuel Figueroa
*/
#include "red_neuronal.h"
#include "math.h"
#include "stdio.h"
float suma = 0;
int i, j, k, p, e, r, s, t, u, v;
float sigmoide(double x) {
float val;
val = 1 / (1 + exp(-x));
return val;
}
float red_neuronal(int examples, int capa_1, int capa_2, int capa_3, int capa_4,
float *entrada, float *w1, float *w2, float *w3, float *u2, float *u3,
float *u4, int i_capa_4, int i_example) {
suma = 0;
for (k = 0; k < capa_3; k++) {
suma = suma
+ a_3(examples, capa_1, capa_2, capa_3, &entrada[0], &w1[0],
&w2[0], &u2[0], &u3[0], k, i_example)
* (*(w3 + (capa_3 * i_capa_4) + k));
}
return sigmoide(suma + (*(u4 + i_capa_4)));
}
float a_3(int examples, int capa_1, int capa_2, int capa_3, float *entrada,
float *w1, float *w2, float *u2, float *u3, int i_capa_3, int i_example) {
suma = 0;
for (j = 0; j < capa_2; j++) {
suma = suma
+ a_2(examples, capa_1, capa_2, &entrada[0], &w1[0], &u2[0], j,
i_example) * (*(w2 + (i_capa_3 + (capa_2 * j))));
}
return sigmoide(suma + (*(u3 + i_capa_3)));
}
float a_2(int examples, int capa_1, int capa_2, float *entrada, float *w1,
float *u2, int i_capa_2, int i_example) {
suma = 0;
for (i = 0; i < capa_1; i++) {
suma = suma
+ (*(entrada + (i_example * capa_1) + i))
* (*(w1 + (i_capa_2 + (capa_1 * i))));
}
return sigmoide(suma + (*(u2 + i_capa_2)));
}
float delta_4(int examples, int capa_4, float *Y, float *S, int i_example,
int i_capa_4) {
return (*(Y + (i_example * capa_4 + i_capa_4)))
* (1 - (*(Y + (i_example * capa_4 + i_capa_4))))
* ((*(Y + (i_example * capa_4 + i_capa_4)))
- (*(S + (i_example * capa_4 + i_capa_4))));
}
float delta_3(int examples, int capa_1, int capa_2, int capa_3, int capa_4,
float *entrada, float *w1, float *w2, float *w3, float *Y, float *S,
float *u2, float *u3, int i_capa_1, int i_capa_2, int i_capa_3,
int i_capa_4, int i_example) {
return a_3(examples, capa_1, capa_2, capa_3, &entrada[0], &w1[0], &w2[0],
&u2[0], &u3[0], i_capa_3, i_example)
* (1
- a_3(examples, capa_1, capa_2, capa_3, &entrada[0], &w1[0],
&w2[0], &u2[0], &u3[0], i_capa_3, i_example))
* (*(w3 + (i_capa_3 * capa_4) + i_capa_4))
* delta_4(examples, capa_4, Y, S, i_example, i_capa_4);
}
float delta_2(int examples, int capa_1, int capa_2, int capa_3, int capa_4,
float *entrada, float *w1, float *w2, float *w3, float *Y, float *S,
float *u2, float *u3, int i_capa_1, int i_capa_2, int i_capa_3,
int i_capa_4, int i_example) {
return a_2(examples, capa_1, capa_2, &entrada[0], &w1[0], &u2[0], i_capa_2,
i_example)
* (1
- a_2(examples, capa_1, capa_2, &entrada[0], &w1[0], &u2[0],
i_capa_2, i_example))
* delta_3(examples, capa_1, capa_2, capa_3, capa_4, &entrada[0],
&w1[0], &w2[0], &w3[0], &Y[0], &S[0], &u2[0], &u3[0],
i_capa_1, i_capa_2, i_capa_3, i_capa_4, i_example);
}
void backpropagation(float margen, float alfa, int examples, int capa_1,
int capa_2, int capa_3, int capa_4, float *entrada, float *w1,
float *w2, float *w3, float *u2, float *u3, float *u4, float *S,
float *Y, float *error_calcule) {
alfa = 0.2;
for (r = 0; r < examples; r++) {
for (s = 0; s < capa_1; s++) {
for (t = 0; t < capa_2; t++) {
for (u = 0; u < capa_3; u++) {
for (v = 0; v < capa_4; v++) {
*(Y + (r * capa_4) + v) = red_neuronal(examples, capa_1,
capa_2, capa_3, capa_4, &entrada[0], &w1[0],
&w2[0], &w3[0], &u2[0], &u3[0], &u4[0], v, r);
*(error_calcule + (r * capa_4) + v) = *(Y + (r * capa_4) + v)
- *(S + (r * capa_4) + v);
(*(w3 + (u * capa_4) + v)) = (*(w3 + (u * capa_4) + v))
- alfa
* a_3(examples, capa_1, capa_2, capa_3,
&entrada[0], &w1[0], &w2[0],
&u2[0], &u3[0], u, r)
* delta_4(examples, capa_4, Y, S, r, v);
(*(u4 + v)) = (*(u4 + v))
- alfa * delta_4(examples, capa_4, Y, S, r, v);
(*(w2 + (t * capa_3) + u)) = (*(w2 + (t * capa_3) + u))
- alfa
* a_2(examples, capa_1, capa_2,
&entrada[0], &w1[0], &u2[0], t,
r)
* delta_3(examples, capa_1, capa_2,
capa_3, capa_4, &entrada[0],
&w1[0], &w2[0], &w3[0], &Y[0],
&S[0], &u2[0], &u3[0], s, t, u,
v, r);
(*(u3 + u)) = (*(u3 + u))
- alfa
* delta_3(examples, capa_1, capa_2,
capa_3, capa_4, &entrada[0],
&w1[0], &w2[0], &w3[0], &Y[0],
&S[0], &u2[0], &u3[0], s, t, u,
v, r);
(*(w1 + (s * capa_2) + t)) = (*(w1 + (s * capa_2) + t))
- alfa * (*entrada + (r * capa_1) + s)
* delta_2(examples, capa_1, capa_2,
capa_3, capa_4, &entrada[0],
&w1[0], &w2[0], &w3[0], &Y[0],
&S[0], &u2[0], &u3[0], s, t, u,
v, r);
(*(u2 + t)) = (*(u2 + t))
- alfa
* delta_2(examples, capa_1, capa_2,
capa_3, capa_4, &entrada[0],
&w1[0], &w2[0], &w3[0], &Y[0],
&S[0], &u2[0], &u3[0], s, t, u,
v, r);
}
}
}
}
}
}