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
5: XNOR
*/
int tipo = 0;
int tipo = 2;
void main()
{
@@ -77,7 +77,8 @@ void main()
Percettrone pout = crea_percettrone();
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.bias = 0.729138;
@@ -87,7 +88,7 @@ void main()
pout.w1 = -0.004388;
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
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 yout = funzione_sigmoide(pout, y_ext_1, y_ext_2);
double errore = -(output[j] - yout);
double errore = (output[j] - yout);
int previsione = -1;
if (yout >= soglia_funzione_attivazione)
@@ -158,15 +160,15 @@ void main()
else
{
// Gradienti percettrone 1
double gradiente_w1 = errore * yout * (1 - yout) * y_ext_1 * (1 - y_ext_1) * x[j][0];
double gradiente_w2 = errore * yout * (1 - yout) * y_ext_1 * (1 - y_ext_1) * x[j][1];
double gradiente_bias = errore * yout * (1 - yout) * y_ext_1 * (1 - y_ext_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;
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];
gradiente_w2 = errore * yout * (1 - yout) * y_ext_2 * (1 - y_ext_2) * x[j][1];
gradiente_bias = errore * yout * (1 - yout) * y_ext_2 * (1 - y_ext_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;
correggi_pesi(&p_ext_2, gradiente_w1, gradiente_w2, gradiente_bias);
// Gradienti percettrone out

Binary file not shown.

View File

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

View File

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

View File