niente, non converge nemmeno con mnist
This commit is contained in:
Binary file not shown.
@@ -19,19 +19,21 @@
|
||||
#define PERCETTRONI_LAYER_4 1
|
||||
#define INPUT_LAYER_4 PERCETTRONI_LAYER_3
|
||||
|
||||
#define MAX_EPOCHE 1000
|
||||
#define MAX_EPOCHE 10
|
||||
|
||||
//Scelgo quale categoria voglio identificare. La 7 sono i cavalli. La rete mi dirà per ogni immagine se è un cavallo o no
|
||||
#define CATEGORIA 7
|
||||
|
||||
byte get_out_corretto(byte);
|
||||
void stampa_layer_indirizzo(Layer*);
|
||||
void stampa_tempo(time_t[], int);
|
||||
|
||||
void main() {
|
||||
time_t tempo_epoche[MAX_EPOCHE];
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
Dataset *set_appoggio = get_dataset("mnist/t10k-images.idx3-ubyte", "mnist/t10k-labels.idx1-ubyte");
|
||||
Dataset *set_appoggio = get_dataset(file_immagini, file_label);
|
||||
|
||||
if(set_appoggio == NULL)
|
||||
return;
|
||||
@@ -39,7 +41,7 @@ void main() {
|
||||
free(set_appoggio);
|
||||
|
||||
ReteNeurale rete_neurale;
|
||||
ReteNeurale *puntatore_rete = caricaReteNeurale("rete_cifar_pesi.bin");
|
||||
ReteNeurale *puntatore_rete = caricaReteNeurale(file_pesi);
|
||||
if(puntatore_rete == NULL) {
|
||||
rete_neurale = inizializza_rete_neurale(NUM_LAYERS);
|
||||
//inizializzo layer 0
|
||||
@@ -51,7 +53,7 @@ void main() {
|
||||
//inizializzo layer 3
|
||||
rete_neurale.layers[3] = inizializza_layer(PERCETTRONI_LAYER_3, INPUT_LAYER_3);
|
||||
//inizializzo layer ULTIMO
|
||||
//rete_neurale.layers[4] = inizializza_layer(PERCETTRONI_LAYER_4, INPUT_LAYER_4);
|
||||
rete_neurale.layers[4] = inizializza_layer(PERCETTRONI_LAYER_4, INPUT_LAYER_4);
|
||||
} else {
|
||||
rete_neurale = *puntatore_rete;
|
||||
free(puntatore_rete);
|
||||
@@ -62,26 +64,12 @@ void main() {
|
||||
|
||||
//ADDESTRAMENTO
|
||||
for(int i = 0; i < MAX_EPOCHE; i++) {
|
||||
time(&tempo_epoche[i]);
|
||||
|
||||
if(i == 0)
|
||||
printf("Epoca %d\n", i);
|
||||
else {
|
||||
time(&tempo_epoche[i]);
|
||||
double tempo_trascorso_epoca = difftime(tempo_epoche[i], tempo_epoche[i-1]);
|
||||
double tempo_trascorso_totale = difftime(tempo_epoche[i], tempo_epoche[0]);
|
||||
int minuti_epoca = (int)tempo_trascorso_epoca / 60;
|
||||
int secondi_epoca = (int)tempo_trascorso_epoca % 60;
|
||||
int minuti_totali = (int)tempo_trascorso_totale / 60;
|
||||
int secondi_totali = (int)tempo_trascorso_totale % 60;
|
||||
printf("Epoca %d\n", i);
|
||||
printf("Tempo dall'epoca precedente: %d:%d\n", minuti_epoca, secondi_epoca);
|
||||
printf("Tempo dall'inizio: %d:%d\n", minuti_totali, secondi_totali);
|
||||
}
|
||||
|
||||
stampa_tempo(tempo_epoche, i);
|
||||
int corrette = 0;
|
||||
|
||||
for(int indice_set = 0; indice_set < set.size -1; indice_set++) {
|
||||
for(int indice_set = 0; indice_set < set.size; indice_set++) {
|
||||
|
||||
double **sigmoidi = (double **)malloc(sizeof(double*) * NUM_LAYERS);
|
||||
|
||||
@@ -93,14 +81,10 @@ void main() {
|
||||
sigmoidi[j] = funzioni_attivazione_layer_double(rete_neurale.layers[j], sigmoidi[j-1]);
|
||||
}
|
||||
|
||||
//printf("\timmagine: %d post sigmoidi\n", indice_set);
|
||||
|
||||
byte output_corretto = get_out_corretto(set.istanze[indice_set].categoria);
|
||||
|
||||
|
||||
//Se prevede male
|
||||
if(previsione(sigmoidi[NUM_LAYERS-1][0]) != output_corretto) {
|
||||
|
||||
double **gradienti = (double**)malloc(sizeof(double*) * NUM_LAYERS);
|
||||
|
||||
for(int indice_layer = 0; indice_layer < NUM_LAYERS; indice_layer++) {
|
||||
@@ -116,14 +100,12 @@ void main() {
|
||||
{
|
||||
corrette++;
|
||||
}
|
||||
|
||||
//printf("\timmagine: %d post correzioni\n", indice_set);
|
||||
}
|
||||
|
||||
printf("\tRisposte corrette: %d\n", corrette);
|
||||
}
|
||||
|
||||
salvaReteNeurale("rete_cifar_pesi.bin", &rete_neurale);
|
||||
salvaReteNeurale(file_pesi, &rete_neurale);
|
||||
}
|
||||
|
||||
//Questa funzione ritorna 1 se la categoria è quella che voglio individuare, altrimenti 0
|
||||
@@ -144,3 +126,18 @@ void stampa_layer_indirizzo(Layer *layer) {
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void stampa_tempo(time_t tempo_epoche[], int i) {
|
||||
|
||||
time(&tempo_epoche[i]);
|
||||
if(i > 0) {
|
||||
double tempo_trascorso_epoca = difftime(tempo_epoche[i], tempo_epoche[i-1]);
|
||||
double tempo_trascorso_totale = difftime(tempo_epoche[i], tempo_epoche[0]);
|
||||
int minuti_epoca = (int)tempo_trascorso_epoca / 60;
|
||||
int secondi_epoca = (int)tempo_trascorso_epoca % 60;
|
||||
int minuti_totali = (int)tempo_trascorso_totale / 60;
|
||||
int secondi_totali = (int)tempo_trascorso_totale % 60;
|
||||
printf("Tempo dall'epoca precedente: %d:%d\n", minuti_epoca, secondi_epoca);
|
||||
printf("Tempo dall'inizio: %d:%d\n", minuti_totali, secondi_totali);
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -9,6 +9,11 @@ Byte 16 in poi: Dati delle immagini (ogni immagine è composta da 28x28 = 784 by
|
||||
Le immagini sono 28x28 pixel.
|
||||
I dati sono byte non firmati (valori da 0 a 255).
|
||||
Il file contiene 60.000 immagini (per il training set),
|
||||
|
||||
Label
|
||||
Byte 0-3: Numero magico (0x00000801).
|
||||
Byte 4-7: Numero di label (60.000).
|
||||
Byte 8 in poi: 60.000 byte, ognuno dei quali rappresenta l'etichetta di un'immagine.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
@@ -59,7 +64,7 @@ Dataset *get_dataset(char *path_mnist, char *path_categoria)
|
||||
int numero_righe = 0;
|
||||
|
||||
//Leggo male il file, cambiare in base alle dichiarazioni sopra
|
||||
if(fread(istanze[numero_righe].immagine, sizeof(byte), 16, file) == 16)
|
||||
if(fread(istanze[numero_righe].immagine, sizeof(byte), 16, file) == 16 && fread(&istanze[numero_righe].categoria, sizeof(byte), 8, categorie) == 8)
|
||||
while (fread(istanze[numero_righe].immagine, sizeof(byte), N_PIXEL, file) == N_PIXEL)
|
||||
{
|
||||
if(fread(&istanze[numero_righe].categoria, sizeof(byte), 1, categorie) == 1) {
|
||||
|
||||
@@ -2,11 +2,15 @@
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
char *file_pesi = "rete_pesi.bin";
|
||||
char *file_immagini = "mnist/t10k-images.idx3-ubyte";
|
||||
char *file_label = "mnist/t10k-labels.idx1-ubyte";
|
||||
|
||||
// Siccome il char è un byte che rappresenta il valore tra 0 e 255. Per evitare confusioni definisco il tipo "byte" come in Java
|
||||
typedef unsigned char byte;
|
||||
|
||||
|
||||
double LRE = 0.1;
|
||||
double LRE = 0.2;
|
||||
double soglia_sigmoide = 0.5;
|
||||
|
||||
typedef struct {
|
||||
@@ -47,7 +51,7 @@ ReteNeurale *caricaReteNeurale(const char*);
|
||||
//Questa funzione genera un valore reale random compreso nell'intervallo [-1, 1]
|
||||
double randomico() {
|
||||
// Genero numeri nell'intervallo [-1,1]
|
||||
return ((double)(rand() % 101 * 0.01 * 2 ) -1);
|
||||
return (double)((rand() / RAND_MAX) * -1);//((double)(rand() % 101 * 0.01 * 2 ) -1);
|
||||
}
|
||||
|
||||
//Questa funzione inizializza il percettrone allocando la memoria in base al numero dei pesi che voglio ed inizializza il loro valore usando randomico()
|
||||
@@ -130,6 +134,7 @@ double *funzioni_attivazione_layer_byte(Layer layer, byte *inputs) {
|
||||
|
||||
for(int i = 0; i < layer.size; i++) {
|
||||
funzioni[i] = sigmoide_byte(layer.percettroni[i], inputs, layer.percettroni[i].size);
|
||||
//printf("\tsigmoide layer input %f\n", funzioni[i]);
|
||||
}
|
||||
|
||||
return funzioni;
|
||||
@@ -142,6 +147,7 @@ double *funzioni_attivazione_layer_double(Layer layer, double *inputs) {
|
||||
|
||||
for(int i = 0; i < layer.size; i++) {
|
||||
funzioni[i] = sigmoide_double(layer.percettroni[i], inputs, layer.percettroni[i].size);
|
||||
//printf("\tsigmoide layer %d: %f\n", i, funzioni[i]);
|
||||
}
|
||||
|
||||
return funzioni;
|
||||
@@ -162,9 +168,9 @@ void correggi_layer_interni(ReteNeurale *rete, double **gradienti, double **sigm
|
||||
for(int indice_percettrone = 0; indice_percettrone < rete->layers[indice_layer].size; indice_percettrone++) {//Numero percettroni
|
||||
|
||||
for(int indice_peso = 0; indice_peso < rete->layers[indice_layer].percettroni[indice_percettrone].size; indice_peso++) {//Numero pesi
|
||||
gradienti[indice_layer][indice_percettrone] = gradienti[rete->size-1][0] * (sigmoidi[indice_layer][indice_percettrone] * (1 - sigmoidi[indice_layer][indice_percettrone]));
|
||||
rete->layers[indice_layer].percettroni[indice_percettrone].pesi[indice_peso] += (gradienti[indice_layer][indice_percettrone] * LRE * sigmoidi[indice_layer-1][indice_percettrone]);
|
||||
//rete->layers[indice_layer].percettroni[indice_percettrone].pesi[indice_peso] += (gradienti[rete->size-1][0] * LRE * sigmoidi[indice_layer-1][indice_percettrone]);
|
||||
//gradienti[indice_layer][indice_percettrone] = gradienti[rete->size-1][0] * (sigmoidi[indice_layer][indice_percettrone] * (1 - sigmoidi[indice_layer][indice_percettrone]));
|
||||
//rete->layers[indice_layer].percettroni[indice_percettrone].pesi[indice_peso] += (gradienti[indice_layer][indice_percettrone] * LRE * sigmoidi[indice_layer-1][indice_percettrone]);
|
||||
rete->layers[indice_layer].percettroni[indice_percettrone].pesi[indice_peso] += (gradienti[rete->size-1][0] * LRE * sigmoidi[indice_layer-1][indice_percettrone]);
|
||||
}
|
||||
rete->layers[indice_layer].percettroni[indice_percettrone].bias += (gradienti[indice_layer][indice_percettrone] * LRE);
|
||||
//printf("bias: %f\n", rete->layers[indice_layer].percettroni[indice_percettrone].bias);
|
||||
@@ -179,9 +185,9 @@ void correggi_layer_input(Layer *layer, double **gradienti, double **sigmoidi, b
|
||||
for(int indice_percettrone = 0; indice_percettrone < layer->size; indice_percettrone++) {//Numero percettroni
|
||||
for(int indice_peso = 0; indice_peso < layer->percettroni->size; indice_peso++) { //Numero pesi
|
||||
|
||||
gradienti[indice_layer][indice_percettrone] = gradienti[n_layers-1][0] * (sigmoidi[indice_layer][indice_percettrone] * (1 - sigmoidi[indice_layer][indice_percettrone]));
|
||||
layer->percettroni[indice_percettrone].pesi[indice_peso] += (gradienti[indice_layer][indice_percettrone] * LRE * inputs[indice_peso]);
|
||||
//layer->percettroni[indice_percettrone].pesi[indice_peso] += (gradienti[n_layers-1][0] * LRE * inputs[indice_peso]);
|
||||
//gradienti[indice_layer][indice_percettrone] = gradienti[n_layers-1][0] * (sigmoidi[indice_layer][indice_percettrone] * (1 - sigmoidi[indice_layer][indice_percettrone]));
|
||||
//layer->percettroni[indice_percettrone].pesi[indice_peso] += (gradienti[indice_layer][indice_percettrone] * LRE * inputs[indice_peso]);
|
||||
layer->percettroni[indice_percettrone].pesi[indice_peso] += (gradienti[n_layers-1][0] * LRE * inputs[indice_peso]);
|
||||
}
|
||||
layer->percettroni[indice_percettrone].bias += (gradienti[n_layers-1][0] * LRE);
|
||||
}
|
||||
|
||||
BIN
rete_neurale
BIN
rete_neurale
Binary file not shown.
BIN
rete_pesi.bin
Normal file
BIN
rete_pesi.bin
Normal file
Binary file not shown.
BIN
visualizzatore
BIN
visualizzatore
Binary file not shown.
@@ -33,13 +33,13 @@ void main()
|
||||
init_allegro();
|
||||
|
||||
//get_dataset("cifar-10-batches/test_batch.bin");
|
||||
set = get_dataset("mnist/t10k-images.idx3-ubyte", "mnist/t10k-labels.idx1-ubyte");
|
||||
set = get_dataset(file_immagini, file_label);
|
||||
if (set == NULL) {
|
||||
printf("Errore nel caricare il dataset\n");
|
||||
return;
|
||||
}
|
||||
|
||||
rete_neurale = caricaReteNeurale("rete_cifar_pesi.bin");
|
||||
rete_neurale = caricaReteNeurale(file_pesi);
|
||||
if (rete_neurale == NULL) {
|
||||
printf("Errore nel caricare il modello\n");
|
||||
return;
|
||||
@@ -94,7 +94,7 @@ void init_allegro() {
|
||||
void carica_immagine(int indice_set)
|
||||
{
|
||||
// Stampa informazioni sull'immagine
|
||||
printf("Immagine indice: %d, categoria: %d\n", indice_set, set->istanze[indice_set].categoria);
|
||||
printf("Immagine indice: %d, valore: %d. è un 7? %d\n", indice_set, set->istanze[indice_set].categoria, prevedi(indice_set));
|
||||
|
||||
// Itera su ogni pixel dell'immagine
|
||||
for (int y = 0; y < IMAGE_HEIGHT; y++)
|
||||
|
||||
Reference in New Issue
Block a user