From 8bdd15f9a225190648a0e720c6337ee372a10025 Mon Sep 17 00:00:00 2001 From: fanasina Date: Thu, 22 Feb 2024 10:05:50 +0100 Subject: [PATCH] add neuron repository --- neuron_t/src/neuron_t/neuron_t.c | 222 +++++++++++++++++++++++++++++++ neuron_t/src/neuron_t/neuron_t.h | 50 +++++++ 2 files changed, 272 insertions(+) create mode 100644 neuron_t/src/neuron_t/neuron_t.c create mode 100644 neuron_t/src/neuron_t/neuron_t.h diff --git a/neuron_t/src/neuron_t/neuron_t.c b/neuron_t/src/neuron_t/neuron_t.c new file mode 100644 index 0000000..dff8a1f --- /dev/null +++ b/neuron_t/src/neuron_t/neuron_t.c @@ -0,0 +1,222 @@ +#include "neuron_t/neuron_t.h" + + +#define PR_LINE printf("===================================================== \n"); + + +#define GEN_NEURONS_F_(type)\ + \ +void calc_net_neurons_##type(neurons_##type *nr){\ + size_t contractNB= ((nr->input)->dim)->size - ((nr->net)->dim)->size ;\ + nr->TensorContraction_##type(&(nr->net), nr->weight_in,nr->input, contractNB, nr->nb_thread );\ +}\ +\ +void calc_out_neurons_##type(neurons_##type *nr, type (*f)(type x) ){\ + calc_net_neurons_##type(nr);\ + for(size_t i = 0; i<(nr->net)->dim->rank; ++i){\ + (nr->output)->x[i]=f((nr->net)->x[i]);\ + }\ +}\ +void calc_delta_neurons_##type(neurons_##type *nr, type (*df)(type x)){\ + if(nr->next_layer == NULL){\ + for(size_t i = 0; i<(nr->net)->dim->rank; ++i){\ + (nr->delta_out)->x[i]=df((nr->net)->x[i])*(nr->dL)((nr->target)->x[i],(nr->output)->x[i]);\ + }\ + }else{\ + tensor_##type *temp_w_d;\ + size_t cntrctnb=(((nr->next_layer)->weight_in)->dim)->size-(((nr->next_layer)->delta_out)->dim)->size ;\ + nr->TensorContraction_##type(&temp_w_d, ((nr->next_layer)->weight_in), (nr->next_layer)->delta_out,cntrctnb,nr->nb_thread);\ + \ + for(size_t i = 0; i<(nr->net)->dim->rank; ++i){\ + (nr->delta_out)->x[i]=df((nr->net)->x[i]) * temp_w_d->x[i] ;\ + }\ + free_tensor_##type(temp_w_d);\ + }\ +}\ +void update_weight_neurons_##type(neurons_##type *nr){\ + tensor_##type *tmp_e_w;\ + nr->TensorProduct_##type(&(tmp_e_w), nr->delta_out, nr->input, nr->nb_thread);\ + \ + for(size_t i = 0; i<(nr->weight_in)->dim->rank; ++i){\ + (nr->weight_in)->x[i]= (nr->weight_in)->x[i] - nr->learning_rate *tmp_e_w->x[i] ;\ + }\ + free_tensor_##type(tmp_e_w);\ +}\ +void init_in_out_all_networks_##type(neurons_##type *nr, tensor_##type *in, tensor_##type *out){\ + if(((nr->input)->dim)->rank == (in->dim)->rank)\ + for(size_t i=0; i<((in)->dim)->rank; ++i) {\ + (nr->input)->x[i] = in->x[i]; \ + }\ + neurons_##type *tmp=nr;\ + while(tmp->next_layer) tmp=tmp->next_layer;\ + if(((tmp->target)->dim)->rank == (out->dim)->rank)\ + for(size_t i=0; i<((out)->dim)->rank; ++i) {\ + (tmp->target)->x[i] = out->x[i]; \ + }\ + \ +}\ +void link_layers_##type(neurons_##type *nPrev, neurons_##type *nNext ){\ + nPrev->next_layer = nNext;\ + nNext->prev_layer = nPrev;\ + size_t pivot, partPiv;\ + if(endian){\ + pivot=0; partPiv = 1;\ + split_tensor_##type(nNext->input, &(nPrev->output), &(nNext->bias), pivot, partPiv);\ + }else{\ + pivot = ((nNext->input)->dim)->size - 1;\ + partPiv = ((nNext->input)->dim)->perm[pivot] - 1 ;\ + \ + split_tensor_##type(nNext->input, &(nNext->bias), &(nPrev->output), pivot, partPiv);\ + }\ + for(size_t i=0;i<((nNext->bias)->dim)->rank;++i) (nNext->bias)->x[i]=1;\ +}\ +\ +void setup_networks_all_dim_inputs_##type(neurons_##type **base_nr, dimension **dim_in_layers, size_t nb_layers){\ + neurons_##type *tmp_l, *ttmp_l=NULL;\ + for(size_t l=0; lnext_layer = malloc(sizeof(neurons_##type));\ + tmp_l = ttmp_l->next_layer;\ + }\ + /*dimension *dim=init_copy_dim(tab_in_layers[l],sz_layers[l]);\ + tensor_##type *input=CREATE_TENSOR_##type(dim);*/\ + tensor_##type *input=CREATE_TENSOR_##type(dim_in_layers[l]);\ + tmp_l->input = input;\ + \ + tmp_l->net = NULL; /* output tensor_prodContract */\ + tmp_l->output = NULL; \ + tmp_l->target = NULL; \ + tmp_l->weight_in = NULL; /* weight link in */\ + tmp_l->bias = NULL; /* bias */\ + tmp_l->weight_out = NULL; /* weight link out */\ + tmp_l->prev_layer = ttmp_l;\ + tmp_l->next_layer = NULL;\ + \ + if(ttmp_l != NULL){\ + link_layers_##type(ttmp_l,tmp_l);\ + dimension *dim_out = (ttmp_l->output)->dim;\ + ttmp_l->net = CREATE_TENSOR_FROM_CPY_DIM_##type(dim_out);\ + if(l == nb_layers - 1) ttmp_l->target = CREATE_TENSOR_FROM_CPY_DIM_##type(dim_out);\ + ttmp_l->delta_out = CREATE_TENSOR_FROM_CPY_DIM_##type(dim_out); /* NULL; */ /* delta */\ + dimension *d_w_in; \ + add_dimension(&d_w_in, (ttmp_l->input)->dim, ((ttmp_l->output)->dim)); \ + ttmp_l->weight_in = CREATE_TENSOR_##type(d_w_in);\ + init_random_x_##type(ttmp_l->weight_in,0,1,5000);\ + }\ +\ + ttmp_l = tmp_l;\ +\ + }\ +}\ +\ +\ +void setup_networks_allinputs_##type(neurons_##type **base_nr, size_t **tab_in_layers, size_t *sz_layers, size_t nb_layers){\ + neurons_##type *tmp_l, *ttmp_l=NULL;\ + for(size_t l=0; lnext_layer = malloc(sizeof(neurons_##type));\ + tmp_l = ttmp_l->next_layer;\ + }\ + dimension *dim=init_copy_dim(tab_in_layers[l],sz_layers[l]);\ + tensor_##type *input=CREATE_TENSOR_##type(dim);\ + tmp_l->input = input;\ + \ + tmp_l->net = NULL; /* output tensor_prodContract */\ + tmp_l->output = NULL; \ + tmp_l->target = NULL; \ + tmp_l->weight_in = NULL; /* weight link in */\ + tmp_l->bias = NULL; /* bias */\ + tmp_l->weight_out = NULL; /* weight link out */\ + tmp_l->prev_layer = ttmp_l;\ + tmp_l->next_layer = NULL;\ + \ + if(ttmp_l != NULL){\ + link_layers_##type(ttmp_l,tmp_l);\ + dimension *dim_out = (ttmp_l->output)->dim;\ + ttmp_l->net = CREATE_TENSOR_FROM_CPY_DIM_##type(dim_out);\ + ttmp_l->delta_out = CREATE_TENSOR_FROM_CPY_DIM_##type(dim_out); /* NULL; */ /* delta */\ + dimension *d_w_in; \ + add_dimension(&d_w_in, (ttmp_l->input)->dim, ((ttmp_l->output)->dim)); \ + ttmp_l->weight_in = CREATE_TENSOR_##type(d_w_in);\ + init_random_x_##type(ttmp_l->weight_in,0,1,5000);\ + }\ +\ + ttmp_l = tmp_l;\ + \ + if(l == nb_layers - 2) {\ + dimension *dim=init_copy_dim(tab_in_layers[l+1],sz_layers[l+1]);\ + tensor_##type *input=CREATE_TENSOR_##type(dim);\ + tmp_l->output= CREATE_TENSOR_FROM_CPY_DIM_##type(dim);\ + tmp_l->net = CREATE_TENSOR_FROM_CPY_DIM_##type(dim);\ + tmp_l->target = CREATE_TENSOR_FROM_CPY_DIM_##type(dim);\ + dimension *d_w_in; \ + add_dimension(&d_w_in, (tmp_l->input)->dim, ((tmp_l->output)->dim)); \ + tmp_l->weight_in = CREATE_TENSOR_##type(d_w_in);\ + init_random_x_##type(tmp_l->weight_in,0,1,5000);\ + }\ + \ +\ + }\ +}\ +\ +void setup_networks_OneD_##type(neurons_##type **base_nr, size_t *tab_in_layers, size_t nb_layers){\ + size_t *sz_layers=malloc(nb_layers*sizeof(size_t));\ + for(size_t i=0; iinput)->dim)->rank == sz_in){\ + for(size_t i=0;iinput)->x[i]=in[i];\ + }\ + neurons_##type *tmp=nr;\ + while(tmp->next_layer) tmp=tmp->next_layer;\ + \ + if(((tmp->target)->dim)->rank == sz_out){\ + for(size_t i=0; i< sz_out; ++i) {\ + (tmp->target)->x[i] = out[i]; \ + }\ + }\ +}\ +void print_neurons_msg_##type(neurons_##type *nr, char *msg){\ + size_t l=0;\ + while(nr){\ + printf("%s, layer %ld\n",msg,l++); \ + PR_LINE;\ + if(nr->input) print_tensor_msg_##type(nr->input," input "); else printf(" input NULL\n");\ + PR_LINE;\ + if(nr->output) print_tensor_msg_##type(nr->input," input "); else printf(" output NULL\n");\ + PR_LINE;\ + if(nr->net) print_tensor_msg_##type(nr->net," net "); else printf(" net NULL\n");\ + PR_LINE;\ + if(nr->weight_in) print_tensor_msg_##type(nr->weight_in," weight_in "); else printf(" weight_in NULL\n");\ + PR_LINE;\ + if(nr->weight_out) print_tensor_msg_##type(nr->weight_out," weight_out "); else printf(" weight_out NULL\n");\ + PR_LINE;\ + if(nr->delta_out) print_tensor_msg_##type(nr->delta_out," delta_out "); else printf(" delta_out NULL\n");\ + PR_LINE;\ + if(nr->target) print_tensor_msg_##type(nr->target," target "); else printf(" target NULL\n");\ + PR_LINE;\ +\ + nr=nr->next_layer;\ + }\ +}\ + +GEN_NEURONS_F_(TYPE_FLOAT) +GEN_NEURONS_F_(TYPE_DOUBLE) diff --git a/neuron_t/src/neuron_t/neuron_t.h b/neuron_t/src/neuron_t/neuron_t.h new file mode 100644 index 0000000..3d7613e --- /dev/null +++ b/neuron_t/src/neuron_t/neuron_t.h @@ -0,0 +1,50 @@ +#ifndef __NEURON_T_C__H +#define __NEURON_T_C__H + +#include + +//#include "tools_t/tools_t.h" +#include "tensor_t/tensor_t.h" + +#define GEN_NEURON_(type)\ +\ +struct neurons_##type {/* layer */\ + size_t id_layer;\ + size_t nb_thread;\ + type learning_rate;\ + tensor_##type *input; \ + tensor_##type *net; /* output tensor_prodContract */\ + tensor_##type *output; \ + tensor_##type *target; \ + tensor_##type *weight_in; /* weight link in */\ + tensor_##type *bias; /* bias */\ + tensor_##type *weight_out; /* weight link out */\ + tensor_##type *delta_out; /* delta */\ + struct neurons_##type *prev_layer;\ + struct neurons_##type *next_layer;\ + void (*TensorContraction_##type)(tensor_##type **MM, tensor_##type *M0, tensor_##type *M1, size_t contractionNumber, size_t nbthread);/* nbthread is ignored if not required ! */\ + void (*TensorProduct_##type)(tensor_##type **MM, tensor_##type *M0, tensor_##type *M1, size_t nbthread);/* nbthread is ignored if not required ! */\ + type (*dL)(type t, type o);\ +};\ +typedef struct neurons_##type neurons_##type;\ +\ +struct func_act_##type {\ + type (*func_act)(type x); /* function activation */\ + type (*deriv_func_act)(type x); /* derivate func act */\ +};\ +/*void calc_net_neurons_##type(neurons_##type *nr);*/\ +void calc_out_neurons_##type(neurons_##type *nr, type (*f)(type x) );\ +void calc_delta_neurons_##type(neurons_##type *nr, type (*df)(type x));\ +void update_weight_neurons_##type(neurons_##type *nr);\ +void setup_networks_##type(neurons_##type **base_nr, size_t **tab_in_layers, size_t *tab_sz_layers, size_t nb_layers);\ +void init_in_out_all_networks_##type(neurons_##type *nr, tensor_##type *in, tensor_##type *out);\ +\ +void setup_networks_OneD_##type(neurons_##type **base_nr, size_t *tab_in_layers, size_t nb_layers);\ +void init_in_out_all_networks_OneD_##type(neurons_##type *nr, type *in, size_t sz_in, type *out, size_t sz_out);\ +void print_neurons_msg_##type(neurons_##type *nr, char * msg);\ + +GEN_NEURON_(TYPE_FLOAT) +GEN_NEURON_(TYPE_DOUBLE) + + +#endif /*__NEURON_T_C__H*/