Updating Neuron structure and adding another functions
This commit is contained in:
parent
1e8e1d0254
commit
05ea70638f
13 changed files with 102 additions and 20 deletions
5
Makefile
5
Makefile
|
@ -3,15 +3,16 @@ default : myprogram
|
|||
all : myprogram
|
||||
|
||||
clean :
|
||||
rm *.o myprogram
|
||||
rm -f *.o myprogram
|
||||
|
||||
myprogram : main.o activations.o randomness.o training.o network.o neurons.o
|
||||
myprogram : randomness.o activations.o neurons.o network.o training.o main.o
|
||||
gcc -Wall $^ -o myprogram -lm
|
||||
|
||||
%.o : %.c
|
||||
gcc -Wall $< -c
|
||||
|
||||
send :
|
||||
git pull
|
||||
git add .
|
||||
git commit
|
||||
git push
|
||||
|
|
BIN
activations.o
Normal file
BIN
activations.o
Normal file
Binary file not shown.
17
main.c
17
main.c
|
@ -9,19 +9,26 @@
|
|||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
//init_randomness();
|
||||
init_randomness();
|
||||
|
||||
printf("Bonjour et bienvenu ;)\n");
|
||||
int i;
|
||||
/*for(i=1 ; i<= 30 ; i++)
|
||||
/*int i;
|
||||
for(i=1 ; i<= 30 ; i++)
|
||||
{
|
||||
printf("%dth generated fload = %f\n", i, random_float(0.0 , 1.0));
|
||||
}*/
|
||||
}
|
||||
printf("sigmoid(%f) = %f\n",0.5, sigmoid(0.5));
|
||||
printf("sigmoid_derivative(%f) = %f\n",0.8, sigmoid_derivative(0.8));
|
||||
|
||||
/*float a=5.0;
|
||||
float a=5.0;
|
||||
printf("%f\n", (float)exp((double)a));*/
|
||||
|
||||
//int n_neurons[] = {3,10,52,52,6};
|
||||
int n_neurons[] = {15,120,220,120};
|
||||
char *activations[] = {"sigmoid","sigmoid","sigmoid","sigmoid"};
|
||||
Network *net = init_network(n_neurons, 4, activations);
|
||||
print_network(net);
|
||||
destroy_network(net);
|
||||
|
||||
return 0;
|
||||
}
|
BIN
main.o
Normal file
BIN
main.o
Normal file
Binary file not shown.
BIN
myprogram
Executable file
BIN
myprogram
Executable file
Binary file not shown.
32
network.c
32
network.c
|
@ -55,7 +55,34 @@ Network *init_network(int n_neurons_per_layer[], int n_layers, char *activation_
|
|||
|
||||
void print_network(Network *network)
|
||||
{
|
||||
|
||||
int i, n_params=0;
|
||||
printf("#>>==========================================<<#\n");
|
||||
printf(">> Number of layers : %d\n", network->n_layers);
|
||||
printf("------------------------------------------------\n");
|
||||
for(i=0 ; i<network->n_layers ; i++)
|
||||
{
|
||||
if(i==0)
|
||||
{
|
||||
printf(">> Input layer\n");
|
||||
printf("size : %d\n", network->neurons_per_layer[i]);
|
||||
}else if(i==network->n_layers-1)
|
||||
{
|
||||
printf(">> Output layer\n");
|
||||
printf("size : %d\n", network->neurons_per_layer[i]);
|
||||
}else
|
||||
{
|
||||
printf(">> Hidden layer %d\n", i);
|
||||
printf("size : %d\n", network->neurons_per_layer[i]);
|
||||
}
|
||||
printf("------------------------------------------------\n");
|
||||
}
|
||||
printf("Number of parameters : ");
|
||||
for(i=1 ; i<network->n_layers ; i++)
|
||||
{
|
||||
n_params += network->neurons_per_layer[i] * (network->neurons_per_layer[i-1] + 1);
|
||||
}
|
||||
printf("%d\n", n_params);
|
||||
printf("#>>==========================================<<#\n");
|
||||
}
|
||||
|
||||
void destroy_network(Network *network)
|
||||
|
@ -67,8 +94,9 @@ void destroy_network(Network *network)
|
|||
while(network->layers_first_neurons[i] != NULL)
|
||||
{
|
||||
temp = network->layers_first_neurons[i];
|
||||
network->layers_first_neurons[i] = temp->same_layer_next_neuron;
|
||||
network->layers_first_neurons[i] = network->layers_first_neurons[i]->same_layer_next_neuron;
|
||||
destroy_neuron(temp);
|
||||
}
|
||||
}
|
||||
free(network);
|
||||
}
|
BIN
network.o
Normal file
BIN
network.o
Normal file
Binary file not shown.
41
neurons.c
41
neurons.c
|
@ -5,6 +5,28 @@
|
|||
#include "neurons.h"
|
||||
#include "activations.h"
|
||||
|
||||
Weight *init_weight_list(int n_weights)
|
||||
{
|
||||
Weight *first, *last;
|
||||
int i;
|
||||
for(i=1 ; i<=n_weights ; i++)
|
||||
{
|
||||
Weight *current = (Weight*)malloc(sizeof(Weight));
|
||||
current->value = random_float(0.0 , 1.0);
|
||||
current->next = NULL;
|
||||
if(i==1)
|
||||
{
|
||||
first = current;
|
||||
last = current;
|
||||
}else
|
||||
{
|
||||
last->next = current;
|
||||
last = current;
|
||||
}
|
||||
}
|
||||
return first;
|
||||
}
|
||||
|
||||
Neuron *init_neuron(int n_weights, char *activation_function)
|
||||
{
|
||||
Neuron *neuron = (Neuron*)malloc(sizeof(Neuron));
|
||||
|
@ -13,12 +35,7 @@ Neuron *init_neuron(int n_weights, char *activation_function)
|
|||
neuron->weights = NULL;
|
||||
}else
|
||||
{
|
||||
neuron->weights = (float*)malloc(n_weights*sizeof(float));
|
||||
int i;
|
||||
for(i=1 ; i<=n_weights ; i++)
|
||||
{
|
||||
neuron->weights[i] = random_float(0.0 , 1.0);
|
||||
}
|
||||
neuron->weights = init_weight_list(n_weights);
|
||||
}
|
||||
neuron->bias = random_float(0.0 , 1.0);
|
||||
neuron->output = 0.0;
|
||||
|
@ -39,9 +56,15 @@ Neuron *init_neuron(int n_weights, char *activation_function)
|
|||
|
||||
void destroy_neuron(Neuron *neuron)
|
||||
{
|
||||
if(neuron->weights != NULL)
|
||||
Weight *temp = NULL;
|
||||
while(neuron->weights != NULL)
|
||||
{
|
||||
free(neuron->weights);
|
||||
temp = neuron->weights;
|
||||
neuron->weights = neuron->weights->next;
|
||||
free(temp);
|
||||
}
|
||||
if(neuron != NULL)
|
||||
{
|
||||
free(neuron);
|
||||
}
|
||||
free(neuron);
|
||||
}
|
10
neurons.h
10
neurons.h
|
@ -1,10 +1,17 @@
|
|||
#ifndef NEURONS_H
|
||||
#define NEURONS_H
|
||||
|
||||
typedef struct weight Weight;
|
||||
struct weight
|
||||
{
|
||||
float value;
|
||||
Weight *next;
|
||||
};
|
||||
|
||||
typedef struct neuron Neuron;
|
||||
struct neuron
|
||||
{
|
||||
float *weights; //weights associated to the neuron
|
||||
Weight *weights; //weights associated to the neuron
|
||||
float bias; //neuron's bias
|
||||
float output; //output of the neuron
|
||||
float (*activation)(float);
|
||||
|
@ -13,6 +20,7 @@ struct neuron
|
|||
Neuron *same_layer_next_neuron;
|
||||
};
|
||||
|
||||
|
||||
Neuron *init_neuron(int n_weights, char *activation_function);
|
||||
void destroy_neuron(Neuron *neuron);
|
||||
|
||||
|
|
BIN
neurons.o
Normal file
BIN
neurons.o
Normal file
Binary file not shown.
BIN
randomness.o
Normal file
BIN
randomness.o
Normal file
Binary file not shown.
17
training.c
17
training.c
|
@ -1,7 +1,22 @@
|
|||
#include <stdio.h>
|
||||
/*#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "randomness.h"
|
||||
#include "network.h"
|
||||
#include "neurons.h"
|
||||
#include "activations.h"
|
||||
|
||||
void forward(Network *network, float sample[])
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void errors_backpropagate(Network *network, float label[])
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void apply_backpropagate(Network *network, float sample[])
|
||||
{
|
||||
|
||||
}*/
|
BIN
training.o
Normal file
BIN
training.o
Normal file
Binary file not shown.
Loading…
Reference in a new issue