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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
#include "fonctions.h"
#include<iostream>
#include <fstream>
#include <sstream>
#include<string>
#include <stdlib.h>
#include <time.h>
#include<cmath>
#include"../Neurone/Neurone.h"
#include"../Neurone/NeuroneB.h"
#include"../Layer/Layer.h"
#include"../Reseau/Reseau.h"
#include"../Common/fonctions.h"
#include"../Settings/Settings.h"
/**
* Affichage de vecteur DOUBLE
* @method printVec
* @param vec Vecteur à afficher
*/
void printVec (std::vector<double> vec){
for (unsigned int i=0;i<vec.size();i++){
std::cout<<vec[i]<<",";
}
}
/**
* Affichage de vecteur INT
* @method printVec
* @param vec Vecteur à afficher
*/
void printVec (std::vector<int> vec){
for (unsigned int i=0;i<vec.size();i++){
std::cout<<vec[i]<<",";
}
}
/**
* Affichage de vecteur de vecteur de DOUBLE
* @method printVec
* @param vec Vecteur à afficher
*/
void printVec (std::vector<std::vector<double> > vec){
for (unsigned int i=0;i<vec.size();i++){
for (unsigned int j=0;j<vec[i].size();j++){
std::cout<<vec[i][j]<<",";
}
std::cout<<std::endl<<"----------"<<std::endl;
}
}
/**
* Affichage de vecteur de vecteur de vecteur de DOUBLE
* @method printVec
* @param vec Vecteur à afficher
*/
void printVec (std::vector<std::vector<std::vector<double> > > vec){
std::cout<<"<";
for(unsigned int k =0; k < vec.size();k++){
std::cout<<"<";
for (unsigned int i=0;i<vec[k].size();i++){
std::cout<<"<";
for (unsigned int j=0;j<vec[k][i].size();j++){
std::cout<<vec[k][i][j];
if ( j != vec[k][i].size()-1){
std::cout<<",";
}
}
std::cout<<">";
if ( i != vec[k].size()-1){
std::cout<<",";
}
}
std::cout<<">"<<(k!=vec.size() ? " ":">")<<std::endl;
}
}
/**
* Recupération des données dans un fichier
* @method getInput
* @param fileName Nom du fichier
*/
std::vector< std::vector <double> > getInput(char const * fileName){
/**
* Counter
*/
std::ifstream in(fileName);
std::string ligne;
int nbLignes = 0;
while(std::getline(in, ligne)){
nbLignes++;
}
in.close();
/**
* Parser
*/
std::ifstream fichier(fileName, std::ios::in);
if(fichier){
std::vector< std::vector <double> > stock(nbLignes); // Tableau de chaine de caractère qui va contenir tout le fichier ligne par ligne
std::cout << "\033[1;36m file " << "\033[1;31m" << fileName << "\033[1;36m opened with success , nbLignes= " <<nbLignes<<"\033[0m"<< std::endl;
std::string s;
double temp = 1;
std::string ligne;
for (int j = 0; j < nbLignes; j++) {
fichier>>ligne;
std::stringstream ss(ligne);
/** strinsplit à partir du caractère ',' */
while (getline(ss, s, ',')) {
temp = atof(s.c_str());
stock[j].push_back(temp);
}
}
swapVec(&stock);
fichier.close();
return stock;
}
else {
std::cerr << "\033[0;31m ERROR [ "<< "Can't open file" << " IN FILE " << __FILE__ << " AT LINE " << __LINE__ << " ] \n \033[0m";
std::vector< std::vector <double> > stock(nbLignes);
return stock;
}
}
/**
* Extraction de la derniére colonne d'un tableau bi-dimensionnel (vecteur de vecteur)
* @method extractResult
* @param input Vecteur de vecteur
*/
std::vector <double> extractResult(std::vector< std::vector <double> > * input){
std::vector<double> res(input->size());
unsigned int pos =((*input)[0].size())-1;
for(unsigned int i=0;i<input->size();i++){
res[i]=(*input)[i][pos];
(*input)[i].pop_back();
}
return res;
}
/**
* Echange de vecteur aléatoire avec synchronisation
* @method swapVec
* @param input Vecteur de vecteur à échanger
* @param result Autre vecteur à synchroniser
*/
void swapVec (std::vector<std::vector<double> > * input, std::vector <double> * result){
unsigned int r;
for (unsigned int i=0;i<input->size();i++){
do{
r = rand()%input->size();
}while(r == i);
(*input)[i].swap((*input)[r]);
iter_swap(result->begin() + i ,result->begin() + r);
}
}
/**
* Echange de vecteur aléatoire
* @method swapVec
* @param input Vecteur de vecteur à échanger
*/
void swapVec (std::vector<std::vector<double> > * input){
unsigned int r;
for (unsigned int i=0;i<input->size();i++){
do{
r = rand()%input->size();
}while(r == i);
(*input)[i].swap((*input)[r]);
}
}
unsigned int int_to_int(unsigned k) {
if (k == 0) return 0;
if (k == 1) return 1; /* schlagué */
return (k % 2) + 10 * int_to_int(k / 2);
}
/**
* Génération d'entrées pour OU et ET
* @method generateInput
* @param n Nombre d'entrées
* @param estet True si génération de ET sinon False pour OU
*/
std::vector<std::vector<double> > generateInput(int n, bool estet){
int nbtest = (int)pow(2,n);
std::vector< std::vector < double > > testtab(nbtest, std::vector<double>(n,0));
for (int i =0; i < nbtest; i ++){
int tmp = i;
tmp = int_to_int(tmp);
for (int j =0; j < n; j ++){
testtab[i][j]= (tmp/(int)pow(10,n-1-j)==1?1 : 0) ;
tmp = tmp- (testtab[i][j]==1? (int)pow(10,n-1-j): 0);
}
if ( !estet && i > 0){
testtab[i].push_back(1);
}
else if ( !estet) {
testtab[i].push_back(0);
}
std::cout << "Serie I :";
for (int k =0; k < n; k ++){
std::cout<<testtab[i][k];
}
if (estet && i == nbtest-1){
testtab[i].push_back(1);
}
else if ( estet){
testtab[i].push_back(0);
}
std::cout<<" o : "<<testtab[i][testtab[i].size()-1]<<std::endl;
}
return testtab;
}
/**
* @method findIndMax
* @param vec Vecteur
* @return Indice du max
*/
unsigned int findIndMax(std::vector<double> vec){
double max = vec[0];
unsigned int indMax=0;
for(unsigned int i=0;i<vec.size();i++){
if(vec[i]>max){
max=vec[i];
indMax = i;
}
}
return indMax;
}
/**
* Récupération d'entrée dans un fichier
* @method getInputUltime
* @param fileName Nom du fichier
* @param pourcentage Pourcentage à récuperer pour le training
*/
std::vector < std::vector <std::vector <double> > > getInputUltime(char const * fileName,float pourcentage){
/**
* Counter
*/
std::ifstream in(fileName);
std::string ligne;
int nbLignes = 0;
while(std::getline(in, ligne)){
nbLignes++;
}
in.close();
/**
* Parser
*/
std::ifstream fichier(fileName, std::ios::in);
if(fichier){
std::vector< std::vector <double> > stock(nbLignes); // Tableau de chaine de caractère qui va contenir tout le fichier ligne par ligne
std::cout << "\033[1;36m file " << "\033[1;31m" << fileName << "\033[1;36m opened with success , nbLignes= " <<nbLignes<<"\033[0m"<< std::endl;
std::string s;
double temp = 1;
std::string ligne;
for (int j = 0; j < nbLignes; j++) {
fichier>>ligne;
std::stringstream ss(ligne);
/** strinsplit à partir du caractère ',' */
while (getline(ss, s, ',')) {
temp = atof(s.c_str());
stock[j].push_back(temp); // On stock chaque information dans une case
}
}
swapVec(&stock);
fichier.close();
int tailleTraining = (int)((pourcentage/100) * nbLignes);
int tailleTesting = nbLignes - tailleTraining;
std::vector< std::vector <std::vector <double> > > result(2);
for(int i=0;i<tailleTraining;i++){
result[0].push_back(stock[i]);
}
for(int i=0;i<tailleTesting;i++){
result[1].push_back(stock[i+tailleTraining]);
}
return result;
}
else {
std::cerr << "\033[0;31m ERROR [ "<< "Can't open file" << " IN FILE " << __FILE__ << " AT LINE " << __LINE__ << " ] \n \033[0m";
exit(2);
std::vector< std::vector < std::vector <double> > > result(2);
return result;
}
}
/**
* Affichage de l'architecture (cf. PYTHON)
* @method displayArchi
* @param info Vecteur d'information sur l'architecture
*/
void displayArchi(std::vector<int> info){
std::string endline = "network.draw()";
std::string begin = "network = DrawNN( [";
std::string dyNeuron= " ";
for (unsigned int i = 0; i <= info.size()-2; i++){
dyNeuron = std::to_string(info[i]) + ",";
begin+=dyNeuron;
}
dyNeuron = std::to_string(info[info.size()-1]);
begin += dyNeuron +"] )";
system("sed -i '$ d' Archi.py");
system("sed -i '$ d' Archi.py");
std::ofstream fichier("Archi.py", std::ios::app);
if(fichier)
{
fichier<< begin <<"\n";
fichier<<endline;
fichier.close();
}
else {
std::cerr << "\033[0;31m ERROR [ "<< "Can't open file" << " IN FILE " << __FILE__ << " AT LINE " << __LINE__ << " ] \n \033[0m";
}
system("python Archi.py");
}
void startLauncher(std::string file, bool arch, int ep, double eta,unsigned int gradient,FonctionActivation::EnumFonctionActivation fct, float auto_off) {
std::string newfile= "./TrainingSets/"+file;
const char * fileName = newfile.c_str();
Settings Settings(fileName);
std::vector<int> archi = (*Settings.getArchi());
std::cout << "Affichage de l'architecture du reseau, Fermez pour commencer" << std::endl;
if(arch == true){
displayArchi(archi);
}
system("clear");
Reseau * rezo = new Reseau(archi.size(),archi,1.0,eta,fct);
std::vector<std::vector <std::vector <double> > > trainingettesting = getInputUltime(fileName,80);
std::vector<std::vector<double> > training = trainingettesting[0];
std::vector<std::vector<double> > testing = trainingettesting[1];
int epoch = ep;
std::vector<double> res(Settings.getDifferentOutputs()->size());
int temp;
std::vector<std::vector<std::vector<double> > > testData(training.size());
std::cout<< "\033[1;33m"<<std::endl;
std::cout<<"--------------------------------"<<std::endl;
for(unsigned int k=0;k<Settings.getDifferentOutputs()->size();k++){
std::vector<double> mapVect(Settings.getDifferentOutputs()->size(),0);
std::cout<<(*Settings.getDifferentOutputs())[k]<<" has been mapped to {" ;
for(unsigned int z=0;z<mapVect.size();z++){
if(z==k)std::cout<<"1," ;
else std::cout<<"0," ;
}
std::cout<<"}"<<std::endl;
}
std::cout<<"--------------------------------"<<std::endl;
std::cout<<"\033[0m"<<std::endl;
for (unsigned int i = 0; i < training.size(); i++){
temp=(int)training[i][training[i].size()-1];
training[i].pop_back();
for(unsigned int j=0;j<Settings.getDifferentOutputs()->size();j++){
if(temp==(*Settings.getDifferentOutputs())[j]){
res[j]=1;
}
else res[j]=0;
}
testData[i] = {training[i],res};
}
std::cout << "Learning Starting..."<<std::endl;
double pourcentage;
int deb = time(NULL);
std::vector<std::vector<double> > expectedRes(testing.size());
for (unsigned int i = 0; i < testing.size(); i++){
temp=(int)testing[i][testing[i].size()-1];
for(unsigned int j=0;j<Settings.getDifferentOutputs()->size();j++){
if(temp==(*Settings.getDifferentOutputs())[j]){
res[j]=1;
}
else res[j]=0;
}
expectedRes[i]=res;
}
std::vector<double> result (Settings.getDifferentOutputs()->size());
int tmp = 0;
int nberr = 0;
int anciennenberr=0;
for(int i=0;i<epoch;i++){
pourcentage = (((double)i/(double)epoch)*100);
swapVec(&testData[0]);
rezo->learn(testData,gradient);
int deltat = time(NULL) - deb;
if (auto_off) {
tmp = 0;
nberr = 0;
anciennenberr=0;
for(unsigned int i=0;i<testing.size();i++){
result= rezo->fire_all(testing[i]);
std::vector<double> print (Settings.getDifferentOutputs()->size(),0);
print[findIndMax(result)]=1;
anciennenberr=nberr;
bool err = false;
for(unsigned int l =0;l<print.size();l++){
if (print[l]!=expectedRes[i][l]) err=true;
}
nberr += err;
tmp++;
}
std::cout<< "\033[1;32m >> " << (int)pourcentage << "% | " << i << " epoch | "<< deltat <<" secondes | Error "<< ((float)nberr/tmp)*100 <<"% \r"<< std::flush;
if (((float)nberr/tmp)*100<auto_off) break;
}
else{
std::cout<< "\033[1;32m >> " << (int)pourcentage << "% | " << i << " epoch | "<< deltat <<" secondes \r"<< std::flush;
}
}
int delta = time(NULL)-deb;
std::cout << "\r\033[1;36m 100% \033[0m";
std::cout << "<\033[1;33m DONE \033[0m> "<< std::endl;
std::cout << "\033[1;36m Learning Completed \033[0m" << std::endl;
std::cout << "Displaying last recorded weights..." << std::endl;
rezo->printWeight();
tmp = 0;
nberr = 0;
anciennenberr=0;
/** Nouveau test avec des valeurs inconnues pour le résaux neuronal */
for(unsigned int i=0;i<testing.size();i++){
std::cout<<"Test; input : ";
printVec(testing[i]);
std::cout<<" attendu : ";
printVec(expectedRes[i]);
std::cout<<" recu : ";
result= rezo->fire_all(testing[i]);
std::vector<double> print (Settings.getDifferentOutputs()->size(),0);
print[findIndMax(result)]=1;
anciennenberr=nberr;
bool err = false;
for(unsigned int l =0;l<print.size();l++){
if (print[l]!=expectedRes[i][l]) err=true;
}
nberr += err;
printVec(print);
if ( nberr > anciennenberr){
std::cout<< "\033[1;31m /!\\"<<"\033[0m";
}
std::cout<<std::endl;
tmp++;
}
std::cout<<"\033[1;33mNombre d'erreur : \033[0m \033[1;32m"<<((float)nberr/tmp)*100<<"\% \033[0m --->("<<nberr<<" / "<<tmp<<")"<<std::endl;
std::cout<<"\033[1;33mTemps d'apprentissage : \033[1;33m"<<delta<<" secondes \033[0m"<<std::endl;
}
|