modifica formula multilayer mettendo i pesi di pout nel calcolo del gradiente e cambiando il segno del calcolo dell'errore

This commit is contained in:
2025-02-01 13:00:24 +01:00
parent 45a5b07bda
commit d79bd87b73
6 changed files with 22 additions and 24 deletions

Binary file not shown.

View File

@@ -13,7 +13,7 @@ int MAX_EPOCHE = 10000;
4: NOR 4: NOR
5: XNOR 5: XNOR
*/ */
int tipo = 0; int tipo = 2;
void main() void main()
{ {
@@ -77,7 +77,8 @@ void main()
Percettrone pout = crea_percettrone(); Percettrone pout = crea_percettrone();
int colore_blu = makecol(0, 0, 255); int colore_blu = makecol(0, 0, 255);
p_ext_1.w1 = 1.332870; //XOR
/* p_ext_1.w1 = 1.332870;
p_ext_1.w2 = -0.628797; p_ext_1.w2 = -0.628797;
p_ext_1.bias = 0.729138; p_ext_1.bias = 0.729138;
@@ -87,7 +88,7 @@ void main()
pout.w1 = -0.004388; pout.w1 = -0.004388;
pout.w2 = 0.090205; pout.w2 = 0.090205;
pout.bias = -0.067931; pout.bias = -0.067931; */
// Contatore per fermare il percettrone, se vale 4 significa che ha indovinato tutte e 4 le combinazioni // Contatore per fermare il percettrone, se vale 4 significa che ha indovinato tutte e 4 le combinazioni
int corrette = 0; int corrette = 0;
@@ -135,7 +136,8 @@ void main()
double y_ext_2 = funzione_sigmoide(p_ext_2, x[j][0], x[j][1]); 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 yout = funzione_sigmoide(pout, y_ext_1, y_ext_2);
double errore = -(output[j] - yout); double errore = (output[j] - yout);
int previsione = -1; int previsione = -1;
if (yout >= soglia_funzione_attivazione) if (yout >= soglia_funzione_attivazione)
@@ -158,15 +160,15 @@ void main()
else else
{ {
// Gradienti percettrone 1 // Gradienti percettrone 1
double gradiente_w1 = errore * yout * (1 - yout) * y_ext_1 * (1 - y_ext_1) * x[j][0]; 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]; 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); double gradiente_bias = errore * yout * (1 - yout) * y_ext_1 * (1 - y_ext_1) * pout.w1;
correggi_pesi(&p_ext_1, gradiente_w1, gradiente_w2, gradiente_bias); correggi_pesi(&p_ext_1, gradiente_w1, gradiente_w2, gradiente_bias);
// Gradienti percettrone 2 // Gradienti percettrone 2
gradiente_w1 = errore * yout * (1 - yout) * y_ext_2 * (1 - y_ext_2) * x[j][0]; 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]; 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); gradiente_bias = errore * yout * (1 - yout) * y_ext_2 * (1 - y_ext_2) * pout.w2;
correggi_pesi(&p_ext_2, gradiente_w1, gradiente_w2, gradiente_bias); correggi_pesi(&p_ext_2, gradiente_w1, gradiente_w2, gradiente_bias);
// Gradienti percettrone out // Gradienti percettrone out

Binary file not shown.

View File

@@ -2,7 +2,7 @@
#include "percettrone.h" #include "percettrone.h"
#include "grafico.h" #include "grafico.h"
int MAX_TRY = 10000; int MAX_TRY = 500000;
/* /*
il tipo indica quali punti vogliamo disegnare nel grafico: il tipo indica quali punti vogliamo disegnare nel grafico:
@@ -13,7 +13,7 @@ int MAX_TRY = 10000;
4: NOR 4: NOR
5: XNOR 5: XNOR
*/ */
int tipo = 0; int tipo = 2;
// Soglia sigmoide // Soglia sigmoide
double soglia_funzione_attivazione = 0.5; double soglia_funzione_attivazione = 0.5;
@@ -26,7 +26,7 @@ void main()
allegro_init(); allegro_init();
install_keyboard(); install_keyboard();
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0); set_gfx_mode(GFX_AUTODETECT_WINDOWED, 1920, 1080, 0, 0);
cls(tipo, 1); cls(tipo, 1);
int colore_rosso = makecol(255, 0, 0); int colore_rosso = makecol(255, 0, 0);
@@ -67,8 +67,8 @@ void main()
//printf("\nCiclo %d\n", i); //printf("\nCiclo %d\n", i);
Punto input; Punto input;
input.x = randomico(); input.x = randomico_positivo();
input.y = randomico(); input.y = randomico_positivo();
double y_ext_1 = funzione_sigmoide(p_ext_1, input.x, input.y); double y_ext_1 = funzione_sigmoide(p_ext_1, input.x, input.y);
@@ -84,10 +84,6 @@ void main()
previsione = 0; previsione = 0;
} }
/* Punto output;
output.x = y_ext_1;
output.y = y_ext_2; */
printf("Inputs: %f:%f -> previsione: %d\n", input.x, input.y, previsione); printf("Inputs: %f:%f -> previsione: %d\n", input.x, input.y, previsione);
if (previsione == 1) if (previsione == 1)

View File

@@ -48,8 +48,8 @@ double randomico() {
} }
double randomico_positivo() { double randomico_positivo() {
// Genero numeri nell'intervallo [0,1] // Genero numeri nell'intervallo [-1,3]
return ((double)(rand() % 101 * 0.01)); return ((double)rand() / RAND_MAX) * 10.0f - 1.0f;
} }
double funzione_sigmoide(Percettrone p, double x1, double x2) { double funzione_sigmoide(Percettrone p, double x1, double x2) {
@@ -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) { void correggi_pesi(Percettrone *p, double grad_w1, double grad_w2, double grad_bias) {
(*p).bias = (*p).bias - (grad_bias * (*p).lre); (*p).bias = (*p).bias + (grad_bias * (*p).lre);
(*p).w1 = (*p).w1 - (grad_w1 * (*p).lre); (*p).w1 = (*p).w1 + (grad_w1 * (*p).lre);
(*p).w2 = (*p).w2 - (grad_w2 * (*p).lre); (*p).w2 = (*p).w2 + (grad_w2 * (*p).lre);
} }
void stampa_layer_uno(Percettrone p, double y, int x1, int x2, double errore) void stampa_layer_uno(Percettrone p, double y, int x1, int x2, double errore)

View File