aggiustamento codice
This commit is contained in:
BIN
layer_multi
BIN
layer_multi
Binary file not shown.
@@ -2,7 +2,7 @@
|
||||
#include "percettrone.h"
|
||||
#include "grafico.h"
|
||||
|
||||
int MAX_EPOCHE = 10000;
|
||||
int MAX_EPOCHE = 100000;
|
||||
|
||||
/*
|
||||
il tipo indica quali punti vogliamo disegnare nel grafico:
|
||||
@@ -19,10 +19,10 @@ void main()
|
||||
{
|
||||
srand(time(NULL));
|
||||
|
||||
allegro_init();
|
||||
/* allegro_init();
|
||||
install_keyboard();
|
||||
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
|
||||
cls(tipo, 1);
|
||||
cls(tipo, 1); */
|
||||
|
||||
int output[4];
|
||||
switch (tipo)
|
||||
@@ -106,8 +106,8 @@ void main()
|
||||
if (corrette == 4)
|
||||
{
|
||||
printf("\nEpoche necessarie: %d\n", i);
|
||||
stampa_risultati_layer_multi(p_ext_1, p_ext_2, pout);
|
||||
|
||||
//stampa_risultati_layer_multi(p_ext_1, p_ext_2, pout);
|
||||
/*
|
||||
for (int z = 0; z < i; z++)
|
||||
{
|
||||
cls(tipo, 1);
|
||||
@@ -122,12 +122,13 @@ void main()
|
||||
sleep_ms(10);
|
||||
}
|
||||
|
||||
readkey();
|
||||
readkey(); */
|
||||
break;
|
||||
}
|
||||
|
||||
printf("\nEpoca %d\n", i);
|
||||
//printf("\nEpoca %d\n", i);
|
||||
corrette = 0;
|
||||
double errore_epoca = 0.0;
|
||||
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
@@ -136,8 +137,6 @@ void main()
|
||||
double y_ext_2 = funzione_sigmoide(p_ext_2, x[j][0], x[j][1]);
|
||||
double yout = funzione_sigmoide(pout, y_ext_1, y_ext_2);
|
||||
|
||||
double errore = (output[j] - yout);
|
||||
|
||||
int previsione = -1;
|
||||
|
||||
if (yout >= soglia_funzione_attivazione)
|
||||
@@ -149,9 +148,9 @@ void main()
|
||||
previsione = 0;
|
||||
}
|
||||
|
||||
stampa_layer_uno(p_ext_1, y_ext_1, x[j][0], x[j][1], errore);
|
||||
stampa_layer_out(pout, previsione, y_ext_1, y_ext_2, errore);
|
||||
stampa_layer_uno(p_ext_2, y_ext_2, x[j][0], x[j][1], errore);
|
||||
//stampa_layer_uno(p_ext_1, y_ext_1, x[j][0], x[j][1], errore);
|
||||
//stampa_layer_out(pout, previsione, y_ext_1, y_ext_2, errore);
|
||||
//stampa_layer_uno(p_ext_2, y_ext_2, x[j][0], x[j][1], errore);
|
||||
|
||||
if (previsione == output[j])
|
||||
{
|
||||
@@ -159,22 +158,39 @@ void main()
|
||||
}
|
||||
else
|
||||
{
|
||||
double errore = (output[j] - yout);
|
||||
/* if(errore < 0)
|
||||
errore = -errore; */
|
||||
errore_epoca += errore;
|
||||
|
||||
double derivata_pout = yout * (1 - yout);
|
||||
double derivata_p1 = y_ext_1 * (1 - y_ext_1);
|
||||
double derivata_p2 = y_ext_2 * (1 - y_ext_2);
|
||||
|
||||
if(derivata_pout == 0.0)
|
||||
derivata_pout = 1;
|
||||
if(derivata_p1 == 0.0)
|
||||
derivata_p1 = 1;
|
||||
if(derivata_p2 == 0.0)
|
||||
derivata_p2 = 1;
|
||||
|
||||
|
||||
// Gradienti percettrone 1
|
||||
double gradiente_w1 = errore * yout * (1 - yout) * y_ext_1 * (1 - y_ext_1) * x[j][0] * pout.w1;
|
||||
double gradiente_w2 = errore * yout * (1 - yout) * y_ext_1 * (1 - y_ext_1) * x[j][1] * pout.w1;
|
||||
double gradiente_bias = errore * yout * (1 - yout) * y_ext_1 * (1 - y_ext_1) * pout.w1;
|
||||
double gradiente_w1 = errore * derivata_pout * derivata_p1 * x[j][0];
|
||||
double gradiente_w2 = errore * derivata_pout * derivata_p1 * x[j][1];
|
||||
double gradiente_bias = errore * derivata_pout * derivata_p1;
|
||||
correggi_pesi(&p_ext_1, gradiente_w1, gradiente_w2, gradiente_bias);
|
||||
|
||||
// Gradienti percettrone 2
|
||||
gradiente_w1 = errore * yout * (1 - yout) * y_ext_2 * (1 - y_ext_2) * x[j][0] * pout.w2;
|
||||
gradiente_w2 = errore * yout * (1 - yout) * y_ext_2 * (1 - y_ext_2) * x[j][1] * pout.w2;
|
||||
gradiente_bias = errore * yout * (1 - yout) * y_ext_2 * (1 - y_ext_2) * pout.w2;
|
||||
gradiente_w1 = errore * derivata_pout * derivata_p2 * x[j][0];
|
||||
gradiente_w2 = errore * derivata_pout * derivata_p2 * x[j][1];
|
||||
gradiente_bias = errore * derivata_pout * derivata_p2;
|
||||
correggi_pesi(&p_ext_2, gradiente_w1, gradiente_w2, gradiente_bias);
|
||||
|
||||
// Gradienti percettrone out
|
||||
gradiente_w1 = errore * yout * (1 - yout) * y_ext_1;
|
||||
gradiente_w2 = errore * yout * (1 - yout) * y_ext_2;
|
||||
gradiente_bias = errore * yout * (1 - yout);
|
||||
gradiente_w1 = errore * derivata_pout * y_ext_1;
|
||||
gradiente_w2 = errore * derivata_pout * y_ext_2;
|
||||
gradiente_bias = errore * derivata_pout;
|
||||
correggi_pesi(&pout, gradiente_w1, gradiente_w2, gradiente_bias);
|
||||
}
|
||||
|
||||
@@ -205,5 +221,7 @@ void main()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("Errore: %f\n", errore_epoca);
|
||||
}
|
||||
}
|
||||
@@ -61,9 +61,9 @@ double funzione_sigmoide(Percettrone p, double x1, double x2) {
|
||||
}
|
||||
|
||||
void correggi_pesi(Percettrone *p, double grad_w1, double grad_w2, double grad_bias) {
|
||||
(*p).bias = (*p).bias + (grad_bias * (*p).lre);
|
||||
(*p).w1 = (*p).w1 + (grad_w1 * (*p).lre);
|
||||
(*p).w2 = (*p).w2 + (grad_w2 * (*p).lre);
|
||||
p->bias += (grad_bias * p->lre);
|
||||
p->w1 += (grad_w1 * p->lre);
|
||||
p->w2 += (grad_w2 * p->lre);
|
||||
}
|
||||
|
||||
void stampa_layer_uno(Percettrone p, double y, int x1, int x2, double errore)
|
||||
|
||||
Reference in New Issue
Block a user