Skip to content

Commit

Permalink
linked list works
Browse files Browse the repository at this point in the history
  • Loading branch information
EmperorOfCosmos committed Jul 19, 2017
1 parent de8ab2e commit e5d7976
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 33 deletions.
88 changes: 55 additions & 33 deletions neural.c
Original file line number Diff line number Diff line change
@@ -1,47 +1,63 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fcntl.h>

#include "neural.h"

#define IN_NUM 3
#define OUT_NUM 2
#define PATTERN_NUM 5

#define INPUT_LAYER 1
#define HIDDEN_LAYER 2
#define OUTPUT_LAYER 3
#define IN_LAYER 1
#define HID_LAYER 2
#define OUT_LAYER 3

typedef struct s_network t_network;
struct s_network
t_neural *init_node(t_layer *layer, t_neural *new, char type)
{
s_neural *input_l;
s_neural *hidden_l;
s_neural *output_l;
new = malloc(sizeof(t_neural));
/*type == 1 ? new->type = IN_LAYER : type == 2 ? new->type = HID_LAYER : new->type = OUT_LAYER;*/
new->type = 0;
new->w_sum = 0;
new->w_in = 1 / rand();
new->w_out = 1 / rand();
new->value = 0;
if(layer->start == NULL)
{
new->prev = NULL;
layer->start = new;
layer->tmp = new;
}
else
{
new->prev = layer->tmp;
layer->tmp->next = new;
layer->tmp = new;

}
new->next = NULL;
layer->end = new;
return (new);
}

typedef struct s_neural t_neural;
struct s_neural
t_layer *init_layer(int nb_node, char type)
{
char type;/* input?hidden?output?*/
double w_sum;
double w_in;
double w_out;
s_neural *prev;
s_neural *next;
}

void init_layer(int nb_node)
{

t_layer *newlayer;
newlayer = malloc(sizeof(t_layer));
newlayer->start = NULL;
newlayer->end = NULL;
int i = 0;
while (i < nb_node)
{
t_neural *new;
new = init_node(newlayer, new, type);
i++;
}
return (newlayer);
}

void init_network(int nb_in, int nb_hi, int nb_out, t_network *network)
t_network *init_network(int nb_in, int nb_hi, int nb_out, t_network *network)
{
network = sizeof(t_network);
network->input_l = init_layer(nb_in);
network->hidden_l = init_layer(nb_hi);
network->output_l = init_layer(nb_out);
network = malloc(sizeof(t_network));
network->input_l = init_layer(nb_in, IN_LAYER);
network->hidden_l = init_layer(nb_hi, HID_LAYER);
network->output_l = init_layer(nb_out, OUT_LAYER);
return (network);
}

int main(int ac, char **av)
Expand All @@ -51,7 +67,13 @@ int main(int ac, char **av)
int targets[PATTERN_NUM][OUT_NUM] = { {0,0}, {0,1}, {0,0}, {1,0}, {1,1} };
int num_training = 10000;

network = init_network(2, 3, 1);/*init_network(nb_in, nb_hi, nb_out)*/
/*network initalisation*/
network = init_network(2, 3, 1, network);
print_layer(network->input_l);
print_layer(network->hidden_l);
print_layer(network->output_l);
//fill_network(inputs, targets, network);

train_network(inputs, outputs, num_training);
/*training begin*/
//train_network(inputs, outputs, num_training);
}
45 changes: 45 additions & 0 deletions neural.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef NEURAL_H_

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include <fcntl.h>

typedef struct s_neural t_neural;
struct s_neural
{
char type;/* input?hidden?output?*/
double w_sum;
double w_in;
double w_out;
double value;
t_neural *prev;
t_neural *next;
};

typedef struct s_layer t_layer;
struct s_layer
{
t_neural *start;
t_neural *tmp;
t_neural *end;
};

typedef struct s_network t_network;
struct s_network
{
t_layer *input_l;
t_layer *hidden_l;
t_layer *output_l;
};

/*PROTOTYPES*/
/* main.c */
t_neural *init_node(t_layer *, t_neural *, char);
t_layer *init_layer(int, char);
t_network *init_network(int, int, int, t_network *);
/* print.c */
void print_layer(t_layer *);

#endif /*NEURAL_H_*/
13 changes: 13 additions & 0 deletions print.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "neural.h"

void print_layer(t_layer *layer)
{
t_neural *tmp;

tmp = layer->start;
while (tmp)
{
printf("w_in = %f\n", tmp->w_in);
tmp = tmp->next;
}
}

0 comments on commit e5d7976

Please sign in to comment.