forked from flovera1/AI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadaline.cpp
341 lines (316 loc) · 11.2 KB
/
adaline.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
/*
* Adaline neuron.
* Note: the difference between adaline and perceptron is the way
* in which adaline change the weights, with this method, the weight is changed
* in according to the sum of the weighted sum of inputs(the net), in perceptron
* the net is passed to the function and the function's output is the one used
* to changed the weights.
* @Fernando Lovera.
* @Caracas, Venezuela.
* 17 Marzo 2014.
*/
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
//########################### Adaline
#define X1 0
#define X2 1
#define T 2
#define ITERACIONES_MAXIMAS 10000000
//###########################
#define EPSILON 0.01
#define AND 0
#define OR 1
#define XOR 2
#define ABS(X) (((X)<0)?(-(X)):(X))
class Adaline {
double tasaAprendizaje;
#define x0 1;
int numCambiosPeso;
public:
double w0, w1, w2; //weights
Adaline(double tasaAprendizaje, double w1, double w2, double umbral);
int procesarEntradaSinEntrenamiento(double x1, double x2);
void entrenarLotes(int trainingSet [][3], int tamano,double epsilon, bool cambiarTasa);//batch training
void entrenarIncremental(int trainingSet [][3], int tamano,double epsilon, bool cambiarTasa);//
double calcularO(double x1, double x2);
void cambiarPesos(double o, double target, double x1, double x2);
int getNumCambiosPeso();
};
int Adaline::getNumCambiosPeso(){
return numCambiosPeso;
}
//checked constructor
Adaline::Adaline(double tasaAprendizaje, double w1, double w2, double umbral){
this->tasaAprendizaje = tasaAprendizaje;
this->w1 = w1;
this->w2 = w2;
this->w0 = umbral;
numCambiosPeso = 0;
}
//checked. O function, weights and inputs values.
double Adaline:: calcularO(double x1, double x2){
return (x1 * this->w1 )+(x2 * this->w2) +w0;
}
void Adaline:: entrenarIncremental(int trainingSet [][3], int tamano,
double epsilon, bool cambiarTasaAprendizaje){
double error = 0;
double deltaW [3];
int numIteraciones = 0;
int o,gradiente;
double tasaAprend = this->tasaAprendizaje;
do{
numIteraciones++;
if (numIteraciones > ITERACIONES_MAXIMAS){
cout << " Se ha alcanzado el numero maximo de iteraciones" << endl;
break;
}
if (cambiarTasaAprendizaje){
tasaAprend = this->tasaAprendizaje/(numIteraciones + 1);
}
error = 0;
for (int i = 0; i<3; i++){
deltaW[i] = 0;
}
/* iterate over the training set.
* You change the weiths in every loop.
*/
for (int i = 0; i<tamano; i++){
o = calcularO(trainingSet[i][X1], trainingSet[i][X2]);
gradiente = trainingSet[i][T] - o;
error += gradiente*gradiente;
numCambiosPeso++;
w0 += tasaAprend*gradiente* x0;
w1 += tasaAprend*gradiente* trainingSet[i][0];
w2 += tasaAprend*gradiente* trainingSet[i][1];
}
cout << "Error en la iteracion: " << numIteraciones << error << endl;
} while (error>EPSILON);
}
void Adaline:: entrenarLotes(int trainingSet [][3], int tamano, double epsilon, bool cambiarTasaAprendizaje){
double error = epsilon + 1;
double errorViejo;
double deltaW [3];
double o,diferencia;
int numIteraciones = 0;
double tasaAprend = tasaAprendizaje;
do{
numIteraciones++;
if (numIteraciones > ITERACIONES_MAXIMAS){
//cout << " Se ha alcanzado el numero maximo de iteraciones" << endl;
break;
}
if (cambiarTasaAprendizaje){
tasaAprend = this->tasaAprendizaje/(numIteraciones);
}
errorViejo = error;
error = 0;
for (int i = 0; i<3; i++){
deltaW[i] = 0;
}
/** Iterar sobre el training set.
* You changed the weights after you found the delta error.
*/
for (int i = 0; i<tamano; i++){
o = calcularO(trainingSet[i][X1], trainingSet[i][X2]);
diferencia = trainingSet[i][T] - o;
error += diferencia*diferencia/2;
deltaW[0] += tasaAprend*diferencia;
deltaW[1] += tasaAprend*diferencia* trainingSet[i][X1];
deltaW[2] += ∫tasaAprend*diferencia* trainingSet[i][X2];
}
numCambiosPeso++;
w0 = w0 + deltaW[0];
w1 = w1 + deltaW[1];
w2 = w2 + deltaW[2];
} while (ABS(error-errorViejo)>epsilon);
cout << "Error: " << ABS(error) << ", diferencia: " << ABS(error-errorViejo) << " y " << numIteraciones << " iteraciones." << endl;
}
/**
* returns an int: 1 if the o is 1; 0 if the value of o is -1
*/
int Adaline:: procesarEntradaSinEntrenamiento(double x1, double x2)
{
if ((x1*w1+x2*w2) > -1*w0)
return 1;
else
return -1;
}
int main()
{
// Generar training set y pesos/Generate the training set and weights
const long max_rand = 1000000;
clock_t comienzo;
comienzo = clock();
srandom(time(NULL));
double w1 = (double) (random() % max_rand) / max_rand;
double w2 = (double) (random() % max_rand) / max_rand;
double umbral = .5;
// Generar trainign set.
int trainingSet [4][3][3];
for (int i = 0; i <3; i++){
trainingSet[0][i][X1] = 0;
trainingSet[1][i][X1] = 0;
trainingSet[2][i][X1] = 1;
trainingSet[3][i][X1] = 1;
trainingSet[0][i][X2] = 1;
trainingSet[1][i][X2] = 0;
trainingSet[2][i][X2] = 1;
trainingSet[3][i][X2] = 0;
}
for (int i =0; i<4; i++){
if (trainingSet[i][AND][X1]&&trainingSet[i][AND][X2])
trainingSet[i][AND][T] = 1;
else
trainingSet[i][AND][T] = -1;
if (trainingSet[i][OR][X1]||trainingSet[i][OR][X2])
trainingSet[i][OR][T] = 1;
else
trainingSet[i][OR][T] = -1;
if (trainingSet[i][XOR][X1] !=trainingSet[i][XOR][X2])
trainingSet[i][XOR][T] = 1;
else
trainingSet[i][XOR][T] = -1;
}
cout << "-------------------------------------------" << endl
<< "Pruebas variando las tasas de aprendizaje" << endl;
const int iteraciones = 8;
double tasaAprend [8] = { 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.99 };
//i itera sobre las operaciones: AND=0, OR=1,XOR=2 / iterate over the
for(int i = 0; i < 2; i++){
cout << endl << "Para la operacion: " << i << endl <<endl;
// Entrenamiento
int tSet[4][3];
for (int k = 0; k< 4; k++){
tSet[k][X1] = trainingSet[k][i][X1];
tSet[k][X2] = trainingSet[k][i][X2];
tSet[k][T] = trainingSet[k][i][T];
}
for (int j = 0; j<iteraciones;j++){
Adaline p = Adaline(tasaAprend[j], w1, w2, umbral);
int tSet[4][3];
for (int k = 0; k< 4; k++){
tSet[k][X1] = trainingSet[k][i][X1];
tSet[k][X2] = trainingSet[k][i][X2];
tSet[k][T] = trainingSet[k][i][T];
}
p.entrenarLotes(tSet, 4, EPSILON, false);
// Prueba.
int clasificadosCorrectamente= 0;
for(int k = 0; k< 4;k++){
if (tSet[k][T] == p.procesarEntradaSinEntrenamiento(tSet[k][X1], tSet[k][X2]))
clasificadosCorrectamente++;
}
cout << " con tasa de aprendizaje: " << tasaAprend[j]
<< " la tasa de error fue: " <<
(double) (4-clasificadosCorrectamente)/4 << endl;
}
Adaline p = Adaline(.99, w1, w2, umbral);
// Prueba.
int clasificadosCorrectamente = 0;
for(int k = 0; k< 4;k++){
if (tSet[k][T] == p.procesarEntradaSinEntrenamiento(tSet[k][X1], tSet[k][X2])){
clasificadosCorrectamente++;
}
}
cout << " con tasa de aprendizaje decayendo: la tasa de error fue: " <<
(double) (4-clasificadosCorrectamente)/4 << endl;
}
/*
// Experimento 1: Cambiar las tasas de aprendizaje.
cout << "-------------------------------------------" << endl
<< "Pruebas variando la tasa de aprendizaje." << endl;
for (int k = 0; k<8;k++) {
cout << endl << "Con tasa de aprendizaje: " << tasaAprend[k] << endl <<endl;
for(int i = 0; i < 3; i++){
Adaline a= Adaline(tasaAprend[k],w1,w2,umbral);
int trainingSet[SIZE_TRAININGSET_1][3];
for (int j = 0; j< SIZE_TRAININGSET_1; j++){
trainingSet[j][X1] = trainingSet1[j][i][X1];
trainingSet[j][X2] = trainingSet1[j][i][X2];
trainingSet[j][T] = trainingSet1[j][i][T];
}
a.entrenarLotes(trainingSet,SIZE_TRAININGSET_1,EPSILON,false);
// Prueba.
int clasificadosCorrectamente= 0;
for(int j = 0; j< SIZE_TESTSET;j++){
if (testSet[j][i][T] == a.procesarEntradaSinEntrenamiento(
testSet[j][i][X1],
testSet[j][i][X2]))
clasificadosCorrectamente++;
else cout << testSet[j][i][T] << " " << a.procesarEntradaSinEntrenamiento(
testSet[j][i][X1],
testSet[j][i][X2]) << endl;
}
cout << " Para la operacion:" << i << " la tasa de error fue: "
<< (double) (SIZE_TESTSET-clasificadosCorrectamente)/SIZE_TESTSET << endl;
}
}
// Experimento 2: hacer que la tasa de aprendizaje decaiga.
cout << "-------------------------------------------" << endl
<< "Pruebas haciendo que la tasa decaiga." << endl;
for(int i = 0; i < 3; i++){
Adaline a= Adaline(0.99,w1,w2,umbral);
int trainingSet[SIZE_TRAININGSET_1][3];
for (int j = 0; j< SIZE_TRAININGSET_1; j++){
trainingSet[j][X1] = trainingSet1[j][i][X1];
trainingSet[j][X2] = trainingSet1[j][i][X2];
trainingSet[j][T] = trainingSet1[j][i][T];
}
a.entrenarLotes(trainingSet,SIZE_TRAININGSET_1,EPSILON,true);
// Prueba.
int clasificadosCorrectamente= 0;
for(int j = 0; j< SIZE_TESTSET;j++){
if (testSet[j][i][T] == a.procesarEntradaSinEntrenamiento(
testSet[j][i][X1],
testSet[j][i][X2]))
clasificadosCorrectamente++;
}
cout << " Para la operacion:" << i << " la tasa de error fue: "
<< (double) (SIZE_TESTSET-clasificadosCorrectamente)/SIZE_TESTSET << endl;
}
// Experimento 3: probar con aprendizaje incremental y por lotes.
cout << "-------------------------------------------" << endl
<< "Pruebas con aprendizaje incremental y por lote" << endl;
for(int i = 0; i < 3; i++){
Adaline aLote= Adaline(0.99,w1,w2,umbral);
Adaline aIncremental= Adaline(0.99,w1,w2,umbral);
int trainingSet[SIZE_TRAININGSET_1][3];
for (int j = 0; j< SIZE_TRAININGSET_1; j++){
trainingSet[j][X1] = trainingSet1[j][i][X1];
trainingSet[j][X2] = trainingSet1[j][i][X2];
trainingSet[j][T] = trainingSet1[j][i][T];
}
aLote.entrenarLotes(trainingSet,SIZE_TRAININGSET_1,EPSILON,true);
aIncremental.entrenarIncremental(trainingSet,SIZE_TRAININGSET_1,EPSILON,true);
// Prueba.
int clasificadosCorrectamenteLot= 0;
int clasificadosCorrectamenteInc= 0;
for(int j = 0; j< SIZE_TESTSET;j++){
if (testSet[j][i][T] == aLote.procesarEntradaSinEntrenamiento(
testSet[j][i][X1],
testSet[j][i][X2]))
clasificadosCorrectamenteLot++;
if (testSet[j][i][T] == aIncremental.procesarEntradaSinEntrenamiento(
testSet[j][i][X1],
testSet[j][i][X2]))
clasificadosCorrectamenteInc++;
}
cout << " Para la operacion:" << i << " con aprendizaje por lote "
<< " la tasa de error fue: " <<
(double) (SIZE_TESTSET-clasificadosCorrectamenteLot)/SIZE_TESTSET << endl;
cout << "Se hicieron: " << aLote.getNumCambiosPeso() << " cambios en los pesos"
<< endl << endl;
cout << " Para la operacion:" << i << " con aprendizaje incremental "
<< " la tasa de error fue: " <<
(double) (SIZE_TESTSET-clasificadosCorrectamenteInc)/SIZE_TESTSET << endl;
cout << "Se hicieron " << aIncremental.getNumCambiosPeso() << " cambios en los pesos"
<< endl << endl;
}
printf( "Número de segundos transcurridos desde el comienzo del programa: %f s\n", (clock()-comienzo)/(double)CLOCKS_PER_SEC );
*
*/
return 0;
}