Skip to content

Commit

Permalink
basics done
Browse files Browse the repository at this point in the history
  • Loading branch information
EmpereurMarcAurele committed Jul 20, 2017
1 parent 730bfcd commit 5631092
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
12 changes: 7 additions & 5 deletions fill_network.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "neural.h"

void fill_layer_out(int *tab, t_layer *layer)
void fill_layer_out(double *tab, t_layer *layer)
{
t_neural *tmp = layer->start;
int i = 0;
while (tmp)
{
tmp->target = (double)tab[i];
tmp->target = tab[i];
tmp = tmp->next;
i++;
}
Expand All @@ -23,25 +23,27 @@ void fill_layer_hi(t_layer *layer)
{
int r = rand() % 9 + 1;
tmp->link[i] = (double)r / 10;
printf("link %f ", tmp->link[i]);
i++;
}
printf("\n");
tmp = tmp->next;
}
}

void fill_layer_in(int *tab, t_layer *layer)
void fill_layer_in(double *tab, t_layer *layer)
{
t_neural *tmp = layer->start;
int i = 0;
while (tmp)
{
tmp->value = (double)tab[i];
tmp->value = tab[i];
tmp = tmp->next;
i++;
}
}

void fill_network(int *inputs, int *target, t_network *network)
void fill_network(double *inputs, double *target, t_network *network)
{
fill_layer_in(inputs, network->input_l);
fill_layer_hi(network->hidden_l);
Expand Down
11 changes: 7 additions & 4 deletions neural.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ t_neural *init_node(t_layer *layer, t_neural *new, char type)
/*type == 1 ? new->type = IN_LAYER : type == 2 ? new->type = HID_LAYER : new->type = OUT_LAYER;*/
new->type = 0;
new->w_sum = 0;
int r = rand() % 99 + 1;
int r = rand() % 9 + 1;
new->link = NULL;
new->w_out = (double)r / 10;
new->value = 0;
Expand Down Expand Up @@ -56,19 +56,22 @@ t_network *init_network(int nb_in, int nb_hi, int nb_out, t_network *network)
int main(int ac, char **av)
{
t_network *network = NULL;;
int inputs[IN_NUM] = {0,0,0,0,0,1,1,1,0,1,0,0};
int targets[OUT_NUM] = {0,1,0,1};
double inputs[IN_NUM] = {0,0,0,0,0,1,1,1,0,1,0,0};
double targets[OUT_NUM] = {0,1,0,1};
if (ac <= 1)
{printf("need 1 arg\n");return (0);}
int nb_it = atoi(av[1]);
if (nb_it < 100)
nb_it = 1000;
struct timeval t1;
gettimeofday(&t1, NULL);
srand(t1.tv_usec * t1.tv_sec);
/*network initalisation*/
network = init_network(IN_NUM, 3, OUT_NUM, network);
fill_network(inputs, targets, network);
print_layer(network->input_l);
print_layer(network->hidden_l);
print_layer(network->output_l);
fill_network(inputs, targets, network);

/*training begin*/
run_network(network, nb_it);
Expand Down
7 changes: 4 additions & 3 deletions neural.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <fcntl.h>

#define IN_NUM 12
Expand Down Expand Up @@ -51,10 +52,10 @@ t_network *init_network(int, int, int, t_network *);
/* print.c */
void print_layer(t_layer *);
/* fill_network.c */
void fill_layer_out(int *, t_layer *);
void fill_layer_out(double *, t_layer *);
void fill_layer_hi(t_layer *);
void fill_layer_in(int *, t_layer *);
void fill_network(int *,int *, t_network *);
void fill_layer_in(double *, t_layer *);
void fill_network(double *,double *, t_network *);
/* run_network.c */
void get_input_w(t_network *);
void run_network(t_network *, int);
Expand Down
5 changes: 3 additions & 2 deletions print.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
void print_layer(t_layer *layer)
{
t_neural *tmp;

tmp = layer->start;
while (tmp)
{
printf("w_in = %f\n", tmp->w_out);
printf("target = %f ", tmp->target);
printf("value = %f ; w_in = %f\n", tmp->value, tmp->w_out);
tmp = tmp->next;
}
printf("\n");
Expand Down
21 changes: 20 additions & 1 deletion run_network.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
#include "neural.h"

void get_hidden_w(t_network *network)
{
t_neural *node_out = network->output_l->start;
int i = 0;
while (node_out)
{
t_neural *node_hi = network->hidden_l->start;
while (node_hi)
{
node_out->w_sum += node_hi->link[i] * node_hi->value;
node_hi = node_hi->next;
}
node_out->value = sigmoid_func(node_out->w_sum);
node_out = node_out->next;
i++;
}
}

void get_input_w(t_network *network)
{
t_neural *node_hi = network->hidden_l->start;
Expand All @@ -9,7 +27,7 @@ t_neural *node_hi = network->hidden_l->start;
t_neural *node_in = network->input_l->start;
while (node_in)
{
int r = rand() % 99 + 1;
int r = rand() % 9 + 1;
node_in->w_out = (double)r / 10;
node_hi->w_sum += (node_in->w_out * node_in->value);
node_in = node_in->next;
Expand All @@ -25,6 +43,7 @@ void run_network(t_network *network, int nb_it)
while (i < nb_it)
{
get_input_w(network);
get_hidden_w(network);
i++;
}
}

0 comments on commit 5631092

Please sign in to comment.