-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrnaBatch.c
More file actions
352 lines (287 loc) · 10.9 KB
/
rnaBatch.c
File metadata and controls
352 lines (287 loc) · 10.9 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
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
/**
* Réalisation d'une architecture orientée IA basé sur le
* RNA. Mettre en oeuvre des threads pour executer plusiers RNA
* et le faire sur un application sur un process : 2 entrées/ 1 sortie
* sur les portes logiques (AND)
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <pthread.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
// --- pour enregistrer les resultats quelque part
#define RESULT_FILE "resultats_rna_batch.txt"
// --- INITIALISATION DU RN ---
#define ENTREE 2
#define CACHEE 10 // nombre de neurones
#define SORTIE 1
#define EPOCHS 10000
// base de la structure du réseau neuronal
typedef struct {
float w_ce[CACHEE][ENTREE]; // poids d'entrée cachée
float w_cs[CACHEE]; // poids caché de sortie
float b_c[CACHEE]; // biais caché
float b_s; // biais de sortie
// w_cc n'est pas ici car on a un réseau peu profond
} RNA;
// --- INITIALISATION DU DATA ---
// La table d'entrées
float inputs[4][ENTREE] = {
{0.0, 0.0},
{0.0, 1.0},
{1.0, 0.0},
{1.0, 1.0}
};
// TARGETS/ Sortie
static const float sortie_AND[4] = {0,0,0,1};
float const sortie_OR[4] = {0,1,1,1};
float const sortie_XOR[4] = {0,1,1,0};
// --- INITIALISATION DES THREADS ---
typedef struct {
RNA reseau;
float (*inputs)[ENTREE]; // nos datas a input E1 et E2
const float *sortie; // target la réponse après l'apprentissage
char *pl; // pour porte logique
unsigned int seed;// pour le random
float eta; // pas d'apprentissage lr
// int epochs;
int thread_id; // les threads
} ThreadPL;
// mutex
pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t file_mutex = PTHREAD_MUTEX_INITIALIZER;
// ------------------------------------------
// --- FONCTION D'ACTIVATION ---
// TODO : La fonction a dérivée
static inline double sigmoid(double x) {
return 1.0 / (1.0 + exp(-x));
}
// Fonction D'activation "Comment allumer un neurone"
// double fonctionActivation(double x) {
// sigmoid(x);
// }
static inline double deriveSigmoid(double y) {
return y * (1.0 - y);
}
// --- ETAPE DE PROPAGATION AVANT (FORWARD)
double initialisationDesPoids(unsigned int *seed) {
double r = (double) rand_r(seed) / (double) RAND_MAX;
return (r*1.0) - 0.5;
}
void init_reseau(RNA *reseau, unsigned int *seed) {
for (int i = 0; i < CACHEE; i++) {
for (int j = 0; j < ENTREE; j++) {
reseau->w_ce[i][j] = initialisationDesPoids(seed);
}
reseau->b_c[i] = initialisationDesPoids(seed);
reseau->w_cs[i] = initialisationDesPoids(seed);
}
reseau->b_s = initialisationDesPoids(seed);
}
double propAvant(RNA *reseau, float entree[ENTREE], double cs[CACHEE]) {
for (int i = 0; i < CACHEE; i++) {
double somme = 0.0;
for (int j = 0; j < ENTREE; j++) {
somme += reseau->w_ce[i][j] * entree[j];
}
somme += reseau->b_c[i];
cs[i] = sigmoid(somme);
}
double somme_prime = 0.0;
for (int i = 0; i < CACHEE; i++) {
somme_prime += reseau->w_cs[i] * cs[i];
}
somme_prime += reseau->b_s;
return sigmoid(somme_prime);
}
// --- ETAPE DE PROPAGATION ARRIERE (BACKWARD)
// Fonction cout (Loss): il faut qu'on sorte le minimale (erreur décroissante tout
// au long de l'apprentissage)
// fonction quadratique
static inline double MSE(double target, double output) {
return 0.5 * pow((target-output), 2);
}
static inline double GradientMSE(double target, double y_pred) {
return (y_pred - target);
}
// void propArriere(RNA *reseau, float entree[ENTREE], double cs[CACHEE],
// double s_pred, float target, float eta) {
// double delta_s = GradientMSE(target, s_pred) * deriveSigmoid(s_pred);
// double delta_c[CACHEE];
// for (int i = 0; i < CACHEE; i++) {
// delta_c[i] = delta_s * reseau->w_cs[i] * deriveSigmoid(cs[i]);
// }
// for (int i = 0; i < CACHEE; i++) {
// reseau->w_cs[i] -= eta* delta_s * cs[i];
// for (int j = 0; j < ENTREE; j++) {
// reseau->w_ce[i][j] -= eta * delta_c[i] * entree[j];
// }
// reseau->b_c[i] -= eta * delta_c[i];
// }
// reseau->b_s -= eta * delta_s;
// }
void propArriere_fullbatch(RNA *reseau, float entree[ENTREE], double cs[CACHEE],
double s_pred, float target, double delta_w_ce[CACHEE][ENTREE],
double delta_w_cs[CACHEE], double delta_b_c[CACHEE], double *delta_b_s) {
double delta_s = GradientMSE(target, s_pred) * deriveSigmoid(s_pred);
double delta_c[CACHEE];
for (int i = 0; i < CACHEE; i++) {
delta_c[i] = delta_s * reseau->w_cs[i] * deriveSigmoid(cs[i]);
delta_w_cs[i] += delta_s * cs[i];
delta_b_c[i] += delta_c[i];
for (int j = 0; j < ENTREE; j++){
delta_w_ce[i][j] += delta_c[i] * entree[j];
}
}
*delta_b_s += delta_s;
}
void Maj_poids_fullbatch(RNA *reseau, double delta_w_ce[CACHEE][ENTREE],
double delta_w_cs[CACHEE], double delta_b_c[CACHEE], double delta_b_s, float eta, int batch_size) {
for(int i = 0; i < CACHEE; i++) {
reseau->w_cs[i] -= eta * delta_w_cs[i] / batch_size;
reseau->b_c[i] -= eta * delta_b_c[i] / batch_size;
for(int j = 0; j < ENTREE; j++) {
reseau->w_ce[i][j] -= eta * delta_w_ce[i][j] / batch_size;
}
}
reseau->b_s -= eta * delta_b_s / batch_size;
}
void train_fullbatch(RNA *reseau, const float targets[4], int epochs, float eta, int thread_id){
double cs[CACHEE];
for(int e = 0; e < epochs; e++) {
// init des gradient
double delta_w_ce[CACHEE][ENTREE] = {0};
double delta_w_cs[CACHEE] = {0};
double delta_b_c[CACHEE] = {0};
double delta_b_s = 0.0;
double mse_epoch = 0.0;
for(int i = 0; i < 4; i++) {
double y_pred = propAvant(reseau, inputs[i], cs);
double diff = targets[i] - y_pred;
mse_epoch += diff * diff;
propArriere_fullbatch(reseau, inputs[i], cs, y_pred, targets[i],
delta_w_ce, delta_w_cs, delta_b_c, &delta_b_s);
}
Maj_poids_fullbatch(reseau, delta_w_ce, delta_w_cs, delta_b_c, delta_b_s, eta, 4);
mse_epoch = 0.5 * (mse_epoch / 4.0);
if ((e % 1000) == 0) {
pthread_mutex_lock(&print_mutex);
printf("[Train][Thread %d] epoch %5d MSE = %.6f\n", thread_id, e, mse_epoch);
pthread_mutex_unlock(&print_mutex);
usleep(50000); // 50 ms
}
}
}
// void train(RNA *reseau, const float targets[4], int epochs, float eta, int thread_id) {
// double cs[CACHEE];
// for(int e = 0; e < epochs; e++) {
// double mse_epoch = 0.0;
// for(int i = 0; i < 4; i++) {
// double y_pred = propAvant(reseau, inputs[i], cs);
// double diff = targets[i] - y_pred;
// mse_epoch += diff * diff;
// propArriere(reseau, inputs[i], cs, y_pred, targets[i], eta);
// }
// //mse_epoch /= 4.0;
// mse_epoch = 0.5 * (mse_epoch / 4.0);
// if ((e % 1000) == 0) {
// pthread_mutex_lock(&print_mutex);
// printf("[Train][Thread %d] epoch %5d MSE = %.6f\n", thread_id, e, mse_epoch);
// pthread_mutex_unlock(&print_mutex);
// usleep(50000); // 50 ms
// }
// }
// }
double test_network(const RNA *reseau, const float targets[4], const char *porte) {
double cs[CACHEE];
double mse = 0.0;
int correct = 0;
pthread_mutex_lock(&print_mutex);
printf("---- Test %s ----\n", porte);
for (int s = 0; s < 4; ++s) {
double y = propAvant((RNA *)reseau, inputs[s], cs);
printf("in=(%.1f, %.1f) -> y=%.6f target=%.1f\n",
inputs[s][0], inputs[s][1], y, targets[s]);
double d = targets[s] - y;
mse += d * d;
int pred = (y >= 0.5) ? 1 : 0;
if (pred == (int)targets[s]) correct++;
}
mse = 0.5 * (mse / 4.0);
printf("MSE(avg, 0.5 factor)=%.6f accuracy=%d/4\n\n", mse, correct);
pthread_mutex_unlock(&print_mutex);
return mse;
}
void save_result(const char *porte, float eta, unsigned int seed, int epochs, int nb_cachee, double mse) {
// pour sauvegarder une trace
pthread_mutex_lock(&file_mutex);
FILE *f = fopen(RESULT_FILE, "a");
if (f == NULL) {
perror("Ouverture : Pb ouverture fichier resultats");
return;
} else {
time_t now = time(NULL);
struct tm *t = localtime(&now);
char buffer[64];
strftime(buffer, sizeof(buffer), "%d/%m/%Y %H:%M:%S", t);
fprintf(f, "[%s]\n", buffer);
fprintf(f, "Porte: %s\n", porte);
fprintf(f, "ETA=%.3f\n", eta);
fprintf(f, "Epochs=%d\n", EPOCHS);
fprintf(f, "Seed=%u\n", seed);
fprintf(f, "Neurones_cache=%d\n", CACHEE);
fprintf(f, "MSE_final=%.6f\n", mse);
fprintf(f, "-------------------------\n");
fclose(f);
}
pthread_mutex_unlock(&file_mutex);
}
void *thread_func(void *arg) {
ThreadPL *tp = (ThreadPL *)arg;
/* initialisation du réseau avec seed */
init_reseau(&tp->reseau, &tp->seed);
pthread_mutex_lock(&print_mutex);
printf("Thread %d: porte=%s seed=%u eta=%.3f \n neurones cache=%d epochs=%d\n",
tp->thread_id, tp->pl, tp->seed, tp->eta, CACHEE, EPOCHS);
pthread_mutex_unlock(&print_mutex);
/* entraîner */
//train(&tp->reseau, tp->sortie, EPOCHS, tp->eta, tp->thread_id);
train_fullbatch(&tp->reseau, tp->sortie, EPOCHS, tp->eta, tp->thread_id);
/* tester */
double mse = test_network(&tp->reseau, tp->sortie, tp->pl);
save_result(tp->pl, tp->eta, tp->seed, EPOCHS, CACHEE, mse);
return NULL;
}
// ---
int main() {
// pour le random
srand(time(NULL));
// les threads pour chaque porte
pthread_t threads[3];
ThreadPL params[3];
//code pour faire les portes d'un coup
const float *targets[3] = {sortie_AND, sortie_OR, sortie_XOR};
char *porte[3] = {"AND", "OR", "XOR"};
for(int i = 0; i < 3; i++) {
params[i].inputs = inputs;
params[i].sortie = targets[i];
params[i].pl = porte[i];
params[i].seed = rand();
params[i].eta = 0.05;
params[i].thread_id = i;
if(pthread_create(&threads[i], NULL, thread_func, ¶ms[i])) {
fprintf(stderr, "Erreur création thread %d\n", i);
perror("pthread_create");
return EXIT_FAILURE;
}
}
for(int th = 0; th < 3; th++){
pthread_join(threads[th], NULL);
}
pthread_mutex_destroy(&print_mutex);
return EXIT_SUCCESS;
}