add some functions to convert input char to dimension and tensor

This commit is contained in:
2024-02-25 00:53:57 +01:00
parent ef107c11db
commit c1409546ef
18 changed files with 688 additions and 128 deletions
+136 -89
View File
@@ -7,39 +7,51 @@
#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 );\
size_t contractNB= ((nr->weight_in)->dim)->size - ((nr->input)->dim)->size ;\
/*print_tensor_msg_##type((nr->weight_in)," weight_in calc");*/\
/*print_tensor_msg_##type((nr->input)," input calc");*/\
nr->TensorContraction(&(nr->net), nr->input, nr->weight_in, contractNB, nr->nb_thread );\
/*print_tensor_msg_##type((nr->net)," net calc");*/\
}\
\
void calc_out_neurons_##type(neurons_##type *nr, type (*f)(type x) ){\
void calc_out_neurons_##type(neurons_##type *nr){\
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]);\
(nr->output)->x[i]=(nr->f_act)((nr->net)->x[i]);\
}\
}\
void calc_delta_neurons_##type(neurons_##type *nr, type (*df)(type x)){\
void calc_delta_neurons_##type(neurons_##type *nr){\
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]);\
(nr->delta_out)->x[i]=(nr->d_f_act)((nr->net)->x[i])*(nr->dL)((nr->target)->x[i],(nr->output)->x[i]);\
}\
/*print_tensor_msg_##type(nr->delta_out," nr delta_out calc delta_out last layer");*/\
}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);\
/*print_tensor_msg_##type((nr->next_layer)->weight_in," nxt weight_in calc delta_out");*/\
/*print_tensor_msg_##type((nr->next_layer)->delta_out," nxt delta_out calc delta_out");*/\
nr->TensorContraction(&temp_w_d, ((nr->next_layer)->weight_in), (nr->next_layer)->delta_out,cntrctnb,nr->nb_thread);\
/*print_tensor_msg_##type(temp_w_d," nxt tmp calc delta_out");*/\
\
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] ;\
(nr->delta_out)->x[i]=(nr->d_f_act)((nr->net)->x[i]) * temp_w_d->x[i] ;\
}\
/*print_tensor_msg_##type(nr->delta_out," nr delta_out calc delta_out");*/\
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);\
nr->TensorProduct(&(tmp_e_w), nr->input, nr->delta_out, nr->nb_thread);\
/*print_tensor_msg_##type(nr->input," nr input update wei");*/\
/*print_tensor_msg_##type(nr->delta_out," nr delta_out update wei");*/\
/*print_tensor_msg_##type(tmp_e_w," tmp_e_w update wei");*/\
\
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] ;\
}\
/*print_tensor_msg_##type(nr->weight_in," weight_in updated ");*/\
free_tensor_##type(tmp_e_w);\
}\
void init_in_out_all_networks_##type(neurons_##type *nr, tensor_##type *in, tensor_##type *out){\
@@ -71,100 +83,107 @@ void link_layers_##type(neurons_##type *nPrev, neurons_##type *nNext ){\
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;\
\
\
void setup_networks_alloutputs_##type(neurons_##type **base_nr, size_t **tab_in_layers, size_t *sz_layers, size_t nb_layers){\
neurons_##type *tmp_l=NULL, *ttmp_l=NULL;\
for(size_t l=0; l<nb_layers; ++l){\
tmp_l = malloc(sizeof(neurons_##type)); \
if(l==0){\
*base_nr=malloc(sizeof(neurons_##type)); \
tmp_l = *base_nr;\
*base_nr = tmp_l ;\
}else{\
ttmp_l->next_layer = malloc(sizeof(neurons_##type));\
tmp_l = ttmp_l->next_layer;\
ttmp_l->next_layer = tmp_l ;\
}\
/*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->id_layer= l;\
tmp_l->input = NULL; \
tmp_l->net = NULL; \
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->weight_in = NULL; \
tmp_l->weight_out = NULL; \
tmp_l->delta_out = NULL; \
tmp_l->bias = NULL; \
tmp_l->prev_layer = ttmp_l;\
tmp_l->next_layer = NULL;\
\
if(ttmp_l != NULL){\
dimension *dim=init_copy_dim(tab_in_layers[l-1],sz_layers[l-1]);\
increment_dim_var(dim);\
tmp_l->input = CREATE_TENSOR_##type(dim);\
for(size_t i=0;i<((tmp_l->input)->dim)->rank;++i) (tmp_l->input)->x[i]=(type)l;\
\
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; l<nb_layers-1; ++l){\
if(l==0){\
*base_nr=malloc(sizeof(neurons_##type)); \
tmp_l = *base_nr;\
}else{\
ttmp_l->next_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);\
if(l>1 ){\
dimension *dim_out = (ttmp_l->output)->dim;\
for(size_t i=0;i<dim_out->rank; ++i) (ttmp_l->output)->x[i]=(type)(l-1);\
ttmp_l->net = CREATE_TENSOR_FROM_CPY_DIM_##type(dim_out);\
for(size_t i=0;i<dim_out->rank; ++i) (ttmp_l->net)->x[i]=(type)(l-1);\
ttmp_l->delta_out = CREATE_TENSOR_FROM_CPY_DIM_##type(dim_out); \
for(size_t i=0;i< dim_out->rank; ++i) (ttmp_l->delta_out)->x[i]=(type)(l-1);\
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);\
}\
if(l==nb_layers-1) {\
dimension *dim_out=init_copy_dim(tab_in_layers[l],sz_layers[l]);\
tmp_l->output = CREATE_TENSOR_##type(dim_out);\
for(size_t i=0;i<((tmp_l->output)->dim)->rank;++i) (tmp_l->output)->x[i]=(type)l;\
tmp_l->target = CREATE_TENSOR_FROM_CPY_DIM_##type(dim_out);\
for(size_t i=0;i<((tmp_l->target)->dim)->rank;++i) (tmp_l->target)->x[i]=(type)(l);\
tmp_l->net = CREATE_TENSOR_FROM_CPY_DIM_##type(dim_out);\
for(size_t i=0;i<((tmp_l->net)->dim)->rank;++i) (tmp_l->net)->x[i]=(type)(l);\
tmp_l->delta_out = CREATE_TENSOR_FROM_CPY_DIM_##type(dim_out); \
for(size_t i=0;i<((tmp_l->delta_out)->dim)->rank;++i) (tmp_l->delta_out)->x[i]=(type)(l);\
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);\
\
}\
\
}\
\
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_all_layers_functions_##type(neurons_##type *base, \
void (*TensorContraction)(tensor_##type **MM, tensor_##type *M0, tensor_##type *M1, size_t contractionNumber, size_t nbthread),/* nbthread is ignored if not required ! */\
void (*TensorProduct)(tensor_##type **MM, tensor_##type *M0, tensor_##type *M1, size_t nbthread),/* nbthread is ignored if not required ! */\
type (*dL)(type t, type o),\
type (*L)(type t, type o),\
type (*f_act)(type x),\
type (*d_f_act)(type x)\
){\
neurons_##type *temp = base;\
while(temp){\
temp->TensorContraction = TensorContraction;\
temp->TensorProduct= TensorProduct;\
temp->L=L;\
temp->dL=dL;\
temp->f_act=f_act;\
temp->d_f_act=d_f_act;\
temp=temp->next_layer;\
}\
}\
\
void setup_all_layers_params_##type(neurons_##type *base,\
size_t nb_thread,\
type learning_rate){\
\
neurons_##type *temp = base;\
while(temp){\
temp->nb_thread=nb_thread;\
temp->learning_rate=learning_rate;\
temp=temp->next_layer;\
}\
}\
\
\
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; i<nb_layers;++i) sz_layers[i]=1;\
@@ -173,7 +192,7 @@ void setup_networks_OneD_##type(neurons_##type **base_nr, size_t *tab_in_layers,
ttab_in_layers[i]=malloc(sizeof(size_t));\
ttab_in_layers[i][0]=tab_in_layers[i];\
}\
setup_networks_allinputs_##type(base_nr, ttab_in_layers, sz_layers, nb_layers);\
setup_networks_alloutputs_##type(base_nr, ttab_in_layers, sz_layers, nb_layers);\
\
for(size_t i=0; i<nb_layers;++i) {\
free(ttab_in_layers[i]);\
@@ -182,8 +201,8 @@ void setup_networks_OneD_##type(neurons_##type **base_nr, size_t *tab_in_layers,
free(sz_layers);\
}\
void init_in_out_all_networks_OneD_##type(neurons_##type *nr, type *in, size_t sz_in, type *out, size_t sz_out){\
if(((nr->input)->dim)->rank == sz_in){\
for(size_t i=0;i<sz_in;++i) (nr->input)->x[i]=in[i];\
if(((nr->output)->dim)->rank == sz_in){\
for(size_t i=0;i<sz_in;++i) (nr->output)->x[i]=in[i];\
}\
neurons_##type *tmp=nr;\
while(tmp->next_layer) tmp=tmp->next_layer;\
@@ -195,13 +214,14 @@ void init_in_out_all_networks_OneD_##type(neurons_##type *nr, type *in, size_t s
}\
}\
void print_neurons_msg_##type(neurons_##type *nr, char *msg){\
size_t l=0;\
while(nr){\
printf("%s, layer %ld\n",msg,l++); \
printf("%s, layer %ld\n",msg,nr->id_layer); \
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");\
if(nr->bias) print_tensor_msg_##type(nr->bias," bias "); else printf(" bias NULL\n");\
PR_LINE;\
if(nr->output) print_tensor_msg_##type(nr->output," output "); else printf(" output NULL\n");\
PR_LINE;\
if(nr->net) print_tensor_msg_##type(nr->net," net "); else printf(" net NULL\n");\
PR_LINE;\
@@ -217,6 +237,33 @@ void print_neurons_msg_##type(neurons_##type *nr, char *msg){\
nr=nr->next_layer;\
}\
}\
\
void free_neurons_##type(neurons_##type *base){\
neurons_##type *temp = base, *ttemp;\
while(temp){\
if(temp->input) free_tensor_##type(temp->input);\
if(temp->output) {\
if(temp->next_layer == NULL) free((temp->output)->x);\
free_dimension((temp->output)->dim);free(temp->output);\
}\
if(temp->bias) {free_dimension((temp->bias)->dim);free(temp->bias);}\
if(temp->net) free_tensor_##type(temp->net);\
if(temp->weight_in) free_tensor_##type(temp->weight_in);\
if(temp->weight_out) free_tensor_##type(temp->weight_out);\
if(temp->delta_out) free_tensor_##type(temp->delta_out);\
if(temp->target) free_tensor_##type(temp->target);\
ttemp = temp;\
temp = ttemp->next_layer;\
free(ttemp);\
}\
}\
type error_out_##type(neurons_##type *base){\
while(base->next_layer) base=base->next_layer;\
type sum=0;\
for(size_t i=0; i< ((base->target)->dim)->rank; ++i) sum += base->L((base->target)->x[i], (base->output)->x[i]);\
return sum / (((base->target)->dim)->rank);\
}\
GEN_NEURONS_F_(TYPE_FLOAT)
GEN_NEURONS_F_(TYPE_DOUBLE)
+25 -6
View File
@@ -22,9 +22,12 @@ struct neurons_##type {/* layer */\
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 ! */\
void (*TensorContraction)(tensor_##type **MM, tensor_##type *M0, tensor_##type *M1, size_t contractionNumber, size_t nbthread);/* nbthread is ignored if not required ! */\
void (*TensorProduct)(tensor_##type **MM, tensor_##type *M0, tensor_##type *M1, size_t nbthread);/* nbthread is ignored if not required ! */\
type (*dL)(type t, type o);\
type (*L)(type t, type o);\
type (*f_act)(type x);\
type (*d_f_act)(type x);\
};\
typedef struct neurons_##type neurons_##type;\
\
@@ -33,15 +36,31 @@ struct func_act_##type {\
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 calc_out_neurons_##type(neurons_##type *nr);\
void calc_delta_neurons_##type(neurons_##type *nr);\
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 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_alloutputs_##type(neurons_##type **base_nr, size_t **tab_in_layers, size_t *sz_layers, size_t nb_layers);\
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);\
\
void free_neurons_##type(neurons_##type *base);\
\
void setup_all_layers_functions_##type(neurons_##type *base, \
void (*TensorContraction)(tensor_##type **MM, tensor_##type *M0, tensor_##type *M1, size_t contractionNumber, size_t nbthread),/* nbthread is ignored if not required ! */\
void (*TensorProduct)(tensor_##type **MM, tensor_##type *M0, tensor_##type *M1, size_t nbthread),/* nbthread is ignored if not required ! */\
type (*dL)(type t, type o),\
type (*L)(type t, type o),\
type (*f_act)(type x),\
type (*d_f_act)(type x)\
);\
void setup_all_layers_params_##type(neurons_##type *base,\
size_t nb_thread,\
type learning_rate);\
type error_out_##type(neurons_##type *base);\
GEN_NEURON_(TYPE_FLOAT)
GEN_NEURON_(TYPE_DOUBLE)
+1 -1
View File
@@ -14,7 +14,7 @@ NEURODIR=$(PWD)/..
DIMDIR=$(PWD)/../../dimension_t
INCLUDE_DIR=$(PWD)/../src
CFLAGS=-I$(INCLUDE_DIR) -I$(YPERMDIR)/src -I$(YTESTDIR)/include_ytest/include -I$(DIMDIR)/src -I$(TENSDIR)/src #"-D DEBUG=1"
LDFLAGS=-L$(YTESTDIR) -lytest -lOpenCL
LDFLAGS=-L$(YTESTDIR) -lytest -lOpenCL -lm
#SRC_DIR=$(ROOT_DIR)/src
#SRC=$(wildcard */*/*.c)
+55 -4
View File
@@ -2,6 +2,8 @@
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
// for sleep !
#ifdef __linux__
#include <unistd.h>
@@ -19,16 +21,65 @@
#define VALGRIND_ 1
TEST(init_One){
float L(float t, float o){
return (o - t) * (o - t)/2;
}
float DL(float t, float o){
return (o - t);
}
neurons_TYPE_FLOAT *bn=NULL;
setup_networks_OneD_TYPE_FLOAT(&bn, (size_t[]){3,4,2},3);
print_neurons_msg_TYPE_FLOAT(bn,"bn");
float f(float x){
return 1/(1+exp((double)(-x)));
}
float df(float x){
return exp(-x)/ ((1+exp(-x)) * (1+exp(-x)));
}
TEST(init_One){
//endian=false;
neurons_TYPE_FLOAT *bn=NULL, *tmp=NULL, *ttmp=NULL;
setup_networks_OneD_TYPE_FLOAT(&bn, (size_t[]){3,5,2},3);
init_in_out_all_networks_OneD_TYPE_FLOAT(bn,(float[]){1.2,0.5,1.3},3,(float[]){0.1,0.8},2);
setup_all_layers_functions_TYPE_FLOAT(bn,
tensorContractnProdThread_TYPE_FLOAT,
tensorProdThread_TYPE_FLOAT,
DL,
L,
f,
df);
setup_all_layers_params_TYPE_FLOAT(bn, 2, 0.7);
//print_neurons_msg_TYPE_FLOAT(bn,"bn");
tmp=bn->next_layer;
while(tmp){
calc_out_neurons_TYPE_FLOAT(tmp);
ttmp = tmp;
tmp = tmp->next_layer;
}
while(ttmp != bn){
calc_delta_neurons_TYPE_FLOAT(ttmp);
update_weight_neurons_TYPE_FLOAT(ttmp);
ttmp = ttmp->prev_layer;
}
print_neurons_msg_TYPE_FLOAT(bn,"bn");
LOG(" error : %f\n", error_out_TYPE_FLOAT(bn));
free_neurons_TYPE_FLOAT(bn);
}
int main(int argc, char **argv){