preparo per cifar
This commit is contained in:
Binary file not shown.
+1
-1
@@ -4,7 +4,7 @@
|
||||
|
||||
void main() {
|
||||
//input, layers, perc iniz, perc fin
|
||||
ReteNeurale rete = inizializza_rete_neurale(N_INPUTS, 3, 128, 10);
|
||||
ReteNeurale rete = inizializza_rete_neurale(N_INPUTS, 2, 32, 10);
|
||||
//ReteNeurale rete = *caricaReteNeurale(file_pesi);
|
||||
//stampa_pesi_rete(rete);
|
||||
|
||||
|
||||
+47
-82
@@ -2,6 +2,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
|
||||
char *file_pesi = "rete_mnist.bin";
|
||||
|
||||
@@ -19,13 +20,11 @@ typedef unsigned char byte;
|
||||
|
||||
float LRE = 0.01;
|
||||
float soglia_sigmoide = 0.5;
|
||||
#define TOLLERANZA 100
|
||||
#define TOLLERANZA 99.5
|
||||
|
||||
//Metto 0 per sigmoide, 1 per ReLU, 2 per gradino
|
||||
// Metto 0 per sigmoide, 1 per ReLU, 2 per gradino
|
||||
#define FUNZIONE_ATTIVAZIONE 1
|
||||
|
||||
#define INIZIALIZZAZIONE_CASUALE 0
|
||||
|
||||
typedef struct
|
||||
{
|
||||
float *pesi;
|
||||
@@ -45,83 +44,62 @@ typedef struct
|
||||
int size;
|
||||
} ReteNeurale;
|
||||
|
||||
//Inizializzazioni
|
||||
// Inizializzazioni
|
||||
Percettrone inzializza_percettrone(int);
|
||||
Layer inizializza_layer(int, int);
|
||||
ReteNeurale inizializza_rete_neurale(int, int, int, int);
|
||||
|
||||
//Previsioni
|
||||
// Previsioni
|
||||
float attivazione(Percettrone p, float *);
|
||||
float derivata_attivazione(float);
|
||||
void softmax(float *, int);
|
||||
int previsione_softmax(float *, int);
|
||||
|
||||
//Forward
|
||||
// Forward
|
||||
float **elabora_attivazioni(ReteNeurale *, Istanza);
|
||||
|
||||
//Discesa del gradiente stocastico
|
||||
// Discesa del gradiente stocastico
|
||||
float **elabora_gradienti(ReteNeurale *, byte, float **);
|
||||
void discesa_gradiente(ReteNeurale *, float **, float **);
|
||||
float calcola_gradiente_disceso(ReteNeurale *, int, int, float **);
|
||||
|
||||
//Correzioni
|
||||
// Correzioni
|
||||
void aggiorna_pesi(ReteNeurale *, float **, float **, Istanza);
|
||||
void correggi_pesi_percettrone_float(Percettrone *, int, float **, float);
|
||||
void correggi_pesi_percettrone_byte(Percettrone *, Istanza, float, int);
|
||||
|
||||
//Addestramento
|
||||
// Addestramento
|
||||
byte addestra(ReteNeurale *, Dataset);
|
||||
|
||||
//Import/Export
|
||||
// Import/Export
|
||||
void salvaReteNeurale(const char *, ReteNeurale *);
|
||||
ReteNeurale *caricaReteNeurale(const char *);
|
||||
|
||||
//Stampa
|
||||
// Stampa
|
||||
void stampa_pesi_rete(ReteNeurale *);
|
||||
|
||||
/*############# INIZIALIZZAZIONI #########################*/
|
||||
|
||||
// INIZIALIZZAZIONE HE
|
||||
Percettrone inizializza_percettrone(int n_pesi)
|
||||
{
|
||||
Percettrone p;
|
||||
p.pesi = (float *)malloc(sizeof(float) * n_pesi);
|
||||
|
||||
if(INIZIALIZZAZIONE_CASUALE == 1) {
|
||||
for (int i = 0; i < n_pesi; i++) {
|
||||
p.pesi[i] = 0.0;//(float)(rand() / RAND_MAX * 2) -1;
|
||||
}
|
||||
// Deviazione standard
|
||||
float he_std = sqrt(2.0 / n_pesi);
|
||||
|
||||
p.bias = 0.0;//(float)(rand() / RAND_MAX * 2) - 1;
|
||||
} else { // INIZIALIZZAZIONE HE
|
||||
//Deviazione standard
|
||||
float he_std = sqrt(2.0 / n_pesi);
|
||||
for (int i = 0; i < n_pesi; i++)
|
||||
for (int i = 0; i < n_pesi; i++)
|
||||
{
|
||||
// Teorema centrale del limite
|
||||
float somma = 0.0;
|
||||
for (int i = 0; i < 12; i++)
|
||||
{
|
||||
// Box-Muller
|
||||
/* float u1 = (float)rand() / RAND_MAX;
|
||||
float u2 = (float)rand() / RAND_MAX;
|
||||
float z = sqrt(-2.0 * log(u1)) * cos(2.0 * M_PI * u2);
|
||||
p.pesi[i] = z * he_std; */
|
||||
|
||||
// Teorema centrale del limite
|
||||
float somma = 0.0;
|
||||
for (int i = 0; i < 12; i++) {
|
||||
somma += (float)rand() / RAND_MAX;
|
||||
}
|
||||
p.pesi[i] = (somma - 6.0) * he_std;
|
||||
|
||||
//Marsaglia
|
||||
/* float u, v, s;
|
||||
do {
|
||||
u = 2.0f * (float)rand() / RAND_MAX - 1.0f;
|
||||
v = 2.0f * (float)rand() / RAND_MAX - 1.0f;
|
||||
s = u*u + v*v;
|
||||
} while (s >= 1.0f || s == 0.0f);
|
||||
float factor = sqrtf(-2.0f * logf(s) / s);
|
||||
p.pesi[i] = u * factor; */
|
||||
somma += (float)rand() / RAND_MAX;
|
||||
}
|
||||
p.pesi[i] = (somma - 6.0) * he_std;
|
||||
|
||||
p.bias = 0.0; // Bias a zero per He initialization
|
||||
p.bias = 0.0; // Bias a zero per inizializzazione He
|
||||
}
|
||||
|
||||
p.size = n_pesi;
|
||||
@@ -198,9 +176,9 @@ float attivazione(Percettrone p, float *valori)
|
||||
sommatoria += p.bias;
|
||||
float risultato;
|
||||
|
||||
if(FUNZIONE_ATTIVAZIONE == 0)
|
||||
if (FUNZIONE_ATTIVAZIONE == 0)
|
||||
risultato = 1.0 / (1.0 + exp(-sommatoria));
|
||||
else if(FUNZIONE_ATTIVAZIONE == 1)
|
||||
else if (FUNZIONE_ATTIVAZIONE == 1)
|
||||
risultato = sommatoria > 0 ? sommatoria : 0.0;
|
||||
else
|
||||
risultato = sommatoria > 0 ? 1.0 : 0.0;
|
||||
@@ -212,9 +190,9 @@ float derivata_attivazione(float valore)
|
||||
{
|
||||
float derivata;
|
||||
|
||||
if(FUNZIONE_ATTIVAZIONE == 0)
|
||||
if (FUNZIONE_ATTIVAZIONE == 0)
|
||||
derivata = valore * (1.0 - valore);
|
||||
else if(FUNZIONE_ATTIVAZIONE == 1)
|
||||
else if (FUNZIONE_ATTIVAZIONE == 1)
|
||||
derivata = valore > 0 ? 1.0 : 0.0;
|
||||
else
|
||||
derivata = 0.0;
|
||||
@@ -230,15 +208,15 @@ void softmax(float *input, int size)
|
||||
if (input[i] > max)
|
||||
max = input[i];
|
||||
|
||||
float sum = 0.0f;
|
||||
float somma = 0.0;
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
input[i] = expf(input[i] - max);
|
||||
sum += input[i];
|
||||
somma += input[i];
|
||||
}
|
||||
|
||||
for (int i = 0; i < size; i++)
|
||||
input[i] /= sum;
|
||||
input[i] /= somma;
|
||||
}
|
||||
|
||||
int previsione_softmax(float *livello_percettroni, int size)
|
||||
@@ -247,8 +225,7 @@ int previsione_softmax(float *livello_percettroni, int size)
|
||||
softmax(livello_percettroni, size);
|
||||
int max = 0;
|
||||
|
||||
for (int i = 1; i < size; i++)
|
||||
{
|
||||
for (int i = 1; i < size; i++) {
|
||||
if (livello_percettroni[i] > livello_percettroni[max])
|
||||
max = i;
|
||||
}
|
||||
@@ -264,28 +241,25 @@ float **elabora_attivazioni(ReteNeurale *rete, Istanza istanza)
|
||||
{
|
||||
float **attivazioni = (float **)malloc(sizeof(float *) * rete->size);
|
||||
float *inputs = (float *)malloc(sizeof(float) * N_INPUTS);
|
||||
for (int i = 0; i < N_INPUTS; i++)
|
||||
{
|
||||
|
||||
for (int i = 0; i < N_INPUTS; i++) {
|
||||
inputs[i] = (float)istanza.dati[i] / 255.0;
|
||||
}
|
||||
|
||||
attivazioni[0] = (float *)malloc(sizeof(float) * rete->layers[0].size);
|
||||
for (int indice_percettrone = 0; indice_percettrone < rete->layers[0].size; indice_percettrone++)
|
||||
{
|
||||
attivazioni[0][indice_percettrone] = attivazione(rete->layers[0].percettroni[indice_percettrone], inputs);
|
||||
}
|
||||
|
||||
free(inputs);
|
||||
|
||||
for (int indice_layer = 1; indice_layer < rete->size; indice_layer++)
|
||||
for (int indice_layer = 0; indice_layer < rete->size; indice_layer++)
|
||||
{
|
||||
attivazioni[indice_layer] = (float *)malloc(sizeof(float) * rete->layers[indice_layer].size);
|
||||
|
||||
for (int indice_percettrone = 0; indice_percettrone < rete->layers[indice_layer].size; indice_percettrone++)
|
||||
{
|
||||
attivazioni[indice_layer][indice_percettrone] = attivazione(rete->layers[indice_layer].percettroni[indice_percettrone], attivazioni[indice_layer - 1]);
|
||||
if(indice_layer == 0)
|
||||
attivazioni[indice_layer][indice_percettrone] = attivazione(rete->layers[indice_layer].percettroni[indice_percettrone], inputs);
|
||||
else
|
||||
attivazioni[indice_layer][indice_percettrone] = attivazione(rete->layers[indice_layer].percettroni[indice_percettrone], attivazioni[indice_layer - 1]);
|
||||
}
|
||||
}
|
||||
|
||||
free(inputs);
|
||||
return attivazioni;
|
||||
}
|
||||
|
||||
@@ -293,7 +267,7 @@ float **elabora_attivazioni(ReteNeurale *rete, Istanza istanza)
|
||||
################# DISCESA DEL GRADIENTE STOCASTICO ################################
|
||||
*/
|
||||
|
||||
float **elabora_gradienti(ReteNeurale *rete_neurale, byte output_corretto, float **attivazioni)
|
||||
float **elabora_gradienti(ReteNeurale *rete_neurale, byte output, float **attivazioni)
|
||||
{
|
||||
float **gradienti = (float **)malloc(sizeof(float *) * rete_neurale->size);
|
||||
|
||||
@@ -302,19 +276,14 @@ float **elabora_gradienti(ReteNeurale *rete_neurale, byte output_corretto, float
|
||||
gradienti[indice_layer] = (float *)malloc(sizeof(float) * rete_neurale->layers[indice_layer].size);
|
||||
}
|
||||
|
||||
//ULTIMO LAYER
|
||||
// ULTIMO LAYER
|
||||
for (int indice_percettrone = 0; indice_percettrone < rete_neurale->layers[rete_neurale->size - 1].size; indice_percettrone++)
|
||||
{
|
||||
if (indice_percettrone == output_corretto)
|
||||
{
|
||||
if (indice_percettrone == output)
|
||||
gradienti[rete_neurale->size - 1][indice_percettrone] = 1 - attivazioni[rete_neurale->size - 1][indice_percettrone];
|
||||
}
|
||||
else
|
||||
{
|
||||
gradienti[rete_neurale->size - 1][indice_percettrone] = 0 - attivazioni[rete_neurale->size - 1][indice_percettrone];
|
||||
}
|
||||
}
|
||||
|
||||
//RESTANTI LAYER
|
||||
discesa_gradiente(rete_neurale, attivazioni, gradienti);
|
||||
|
||||
return gradienti;
|
||||
@@ -415,17 +384,13 @@ byte addestra(ReteNeurale *rete_neurale, Dataset set)
|
||||
|
||||
float **attivazioni = elabora_attivazioni(rete_neurale, set.istanze[indice_set]);
|
||||
|
||||
float *attivazioni_softmax = (float *)malloc(sizeof(float) * rete_neurale->layers[rete_neurale->size - 1].size);
|
||||
memcpy(attivazioni_softmax, attivazioni[rete_neurale->size - 1], rete_neurale->layers[rete_neurale->size - 1].size * sizeof(float));
|
||||
|
||||
float **gradienti = elabora_gradienti(rete_neurale, output_corretto, attivazioni);
|
||||
int previsto = previsione_softmax(attivazioni_softmax, rete_neurale->layers[rete_neurale->size - 1].size);
|
||||
int previsto = previsione_softmax(attivazioni[rete_neurale->size - 1], rete_neurale->layers[rete_neurale->size - 1].size);
|
||||
|
||||
if (previsto == output_corretto)
|
||||
{
|
||||
if (previsto == output_corretto) {
|
||||
corrette++;
|
||||
}
|
||||
|
||||
|
||||
aggiorna_pesi(rete_neurale, attivazioni, gradienti, set.istanze[indice_set]);
|
||||
|
||||
for (int i = 0; i < rete_neurale->size; i++)
|
||||
@@ -435,7 +400,7 @@ byte addestra(ReteNeurale *rete_neurale, Dataset set)
|
||||
for (int i = 0; i < rete_neurale->size; i++)
|
||||
free(attivazioni[i]);
|
||||
free(attivazioni);
|
||||
free(attivazioni_softmax);
|
||||
//free(attivazioni_softmax);
|
||||
}
|
||||
|
||||
float percentuale = ((corrette * 100.0) / set.size);
|
||||
|
||||
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
+3
-10
@@ -165,18 +165,11 @@ void evento_click_bottone(int indice_set)
|
||||
|
||||
int prevedi(int indice_set)
|
||||
{
|
||||
double *sigmoidi_softmax = elabora_sigmoidi(rete_neurale, set->istanze[indice_set])[rete_neurale->size-1];
|
||||
float *attivazioni_softmax = elabora_attivazioni(rete_neurale, set->istanze[indice_set])[rete_neurale->size-1];
|
||||
|
||||
byte output_corretto = set->istanze[indice_set].classificazione;
|
||||
|
||||
int previsto = previsione_softmax(sigmoidi_softmax, rete_neurale->layers[rete_neurale->size - 1].size);
|
||||
int previsto = previsione_softmax(attivazioni_softmax, rete_neurale->layers[rete_neurale->size - 1].size);
|
||||
|
||||
return previsto;
|
||||
}
|
||||
|
||||
/* byte get_out_corretto(byte categoria) {
|
||||
if(categoria == CATEGORIA)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
} */
|
||||
}
|
||||
Reference in New Issue
Block a user