106 lines
3.0 KiB
C
106 lines
3.0 KiB
C
#include <stdlib.h>
|
|
#include <math.h>
|
|
|
|
double LRE = 0.2;
|
|
|
|
typedef struct {
|
|
double w1;
|
|
double w2;
|
|
double bias;
|
|
double lre;
|
|
} Percettrone;
|
|
|
|
// Inputs
|
|
int x[4][2] = {
|
|
{0, 0},
|
|
{0, 1},
|
|
{1, 0},
|
|
{1, 1},
|
|
};
|
|
|
|
// Dichiarazione dei metodi
|
|
Percettrone crea_percettrone();
|
|
double randomico();
|
|
double randomico_positivo();
|
|
double funzione_sigmoide(Percettrone, double, double);
|
|
void correggi_pesi(Percettrone*, double, double, double);
|
|
|
|
void stampa_layer_uno(Percettrone, double, int, int, double);
|
|
void stampa_layer_out(Percettrone, int, double, double, double);
|
|
void stampa_risultati_layer_singolo(Percettrone);
|
|
void stampa_risultati_layer_multi(Percettrone, Percettrone, Percettrone);
|
|
|
|
Percettrone crea_percettrone() {
|
|
|
|
Percettrone percettrone;
|
|
|
|
percettrone.w1 = randomico();
|
|
percettrone.w2 = randomico();
|
|
percettrone.bias = randomico();
|
|
percettrone.lre = LRE;
|
|
|
|
return percettrone;
|
|
}
|
|
|
|
double randomico() {
|
|
// Genero numeri nell'intervallo [-1,1]
|
|
return ((double)(rand() % 101 * 0.01 * 2 ) -1);
|
|
}
|
|
|
|
double randomico_positivo() {
|
|
// Genero numeri nell'intervallo [-1,3]
|
|
return ((double)rand() / RAND_MAX) * 10.0f - 1.0f;
|
|
}
|
|
|
|
double funzione_sigmoide(Percettrone p, double x1, double x2) {
|
|
double funzione = (x1 * p.w1) + (x2 * p.w2) + p.bias;
|
|
double potenza_e = exp(-funzione);
|
|
double risultato = 1 / ( 1 + potenza_e);
|
|
|
|
return risultato;
|
|
}
|
|
|
|
void correggi_pesi(Percettrone *p, double grad_w1, double grad_w2, double grad_bias) {
|
|
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)
|
|
{
|
|
printf("\n");
|
|
printf("\tW1: %f\n", p.w1);
|
|
printf("X1: %d --------> \n", x1);
|
|
printf("\t\t( bias: %f ) -------> Y: %f, errore: %f\n", p.bias, y, errore);
|
|
printf("X2: %d --------> \n", x2);
|
|
printf("\tW2: %f\n", p.w2);
|
|
printf("\n\n");
|
|
}
|
|
|
|
void stampa_layer_out(Percettrone p, int y, double x1, double x2, double errore) {
|
|
printf("\t\t\t\t\t\tW1: %f\n", p.w1);
|
|
printf("\t\t\t\t\tX1: %f --------> \n", x1);
|
|
printf("\t\t\t\t\t\t\t( bias: %f ) -------> Y: %d, errore: %f\n", p.bias, y, errore);
|
|
printf("\t\t\t\t\tX2: %f --------> \n", x2);
|
|
printf("\t\t\t\t\t\tW2: %f\n", p.w2);
|
|
}
|
|
|
|
void stampa_risultati_layer_singolo(Percettrone p) {
|
|
printf("\nPercettrone:\n");
|
|
printf("\t W1: %f, W2: %f, bias: %f\n", p.w1, p.w2, p.bias);
|
|
}
|
|
|
|
void stampa_risultati_layer_multi(Percettrone p1, Percettrone p2, Percettrone pout) {
|
|
printf("\nPercettroni 1, 2 e out:\n");
|
|
printf("\np_ext_1.w1 = %f;", p1.w1);
|
|
printf("\np_ext_1.w2 = %f;", p1.w2);
|
|
printf("\np_ext_1.bias = %f;\n", p1.bias);
|
|
|
|
printf("\np_ext_2.w1 = %f;", p2.w1);
|
|
printf("\np_ext_2.w2 = %f;", p2.w2);
|
|
printf("\np_ext_2.bias = %f;\n", p2.bias);
|
|
|
|
printf("\npout.w1 = %f;", pout.w1);
|
|
printf("\npout.w2 = %f;", pout.w2);
|
|
printf("\npout.bias = %f;\n", pout.bias);
|
|
} |