wip: set_neurons : transfert training

This commit is contained in:
2025-12-19 15:33:47 +01:00
parent 3c1e2a18ed
commit 3b446bada3
8 changed files with 602 additions and 11 deletions
+144 -3
View File
@@ -39,9 +39,98 @@ void free_config_layers(config_layers *pconf){
free(pconf);
}
long int cmp_config_layers(config_layers *c1, config_layers *c2){
long int diff_nb=c1->nb_layers - c2->nb_layers;
if(diff_nb) return diff_nb;
for(long int i=0; i<c1->nb_layers; ++i){
long int diff_sz_layers = c1->sz_layers[i] - c2->sz_layers[i];
if(diff_sz_layers) return diff_sz_layers;
for(long int j=0; j<c1->sz_layers[i]; ++j){
long int diff_dim = c1->array_dim_in_layers[i][j] - c2->array_dim_in_layers[i][j];
if(diff_dim) return diff_dim;
}
}
return 0;
}
config_layers * create_config_layers_from_m_list_ptr_DIMENSION(struct main_list_ptr_DIMENSION *m_l_dim){
config_layers * pconf=malloc(sizeof(struct config_layers));
pconf->nb_layers=m_l_dim->size;
//printf("debug: pconf->nb_layers=%ld\n",pconf->nb_layers);
pconf->sz_layers=malloc(pconf->nb_layers * sizeof(size_t));
pconf->array_dim_in_layers=malloc((pconf->nb_layers)*sizeof(size_t*));
for(struct list_ptr_DIMENSION *local_l_dim=m_l_dim->begin_list; local_l_dim; local_l_dim=local_l_dim->next){
size_t i = local_l_dim->index;
pconf->sz_layers[i]=local_l_dim->value->size;
//printf("debug: pconf->sz_layers[%ld]=%ld\n",i,pconf->sz_layers[i]);
pconf->array_dim_in_layers[i]=malloc((pconf->sz_layers[i])*sizeof(size_t));
for(size_t j=0; j< pconf->sz_layers[i];++j){
pconf->array_dim_in_layers[i][j] = local_l_dim->value->perm[j];
//printf("debug: pconf->array_dim_in_layers[%ld][%ld]=%ld\n",i,j,pconf->array_dim_in_layers[i][j]);
}
}
return pconf;
}
config_layers * create_config_layers_from_m_list_dimension(struct main_list_dimension * m_l_dim){
config_layers * pconf=malloc(sizeof(struct config_layers));
pconf->nb_layers=m_l_dim->size;
printf("debug: pconf->nb_layers=%ld\n",pconf->nb_layers);
pconf->sz_layers=malloc(pconf->nb_layers * sizeof(size_t));
pconf->array_dim_in_layers=malloc((pconf->nb_layers)*sizeof(size_t*));
for(struct list_dimension *local_l_dim=m_l_dim->begin_list; local_l_dim; local_l_dim=local_l_dim->next){
size_t i = local_l_dim->index;
char msg[50]; sprintf(msg, "dim[%ld] ",i);
printDebug_dimension(&(local_l_dim->value), msg);
pconf->sz_layers[i]=local_l_dim->value.size;
printf("debug: pconf->sz_layers[%ld]=%ld\n",i,pconf->sz_layers[i]);
pconf->array_dim_in_layers[i]=malloc((pconf->sz_layers[i])*sizeof(size_t));
for(size_t j=0; j< pconf->sz_layers[i];++j){
pconf->array_dim_in_layers[i][j] = local_l_dim->value.perm[j];
printf("debug: pconf->array_dim_in_layers[%ld][%ld]=%ld\n",i,j,pconf->array_dim_in_layers[i][j]);
}
}
return pconf;
}
void print_config_layers(config_layers * pconf){
for(size_t i=0;i<pconf->nb_layers; ++i){
printf("debug: pconf->sz_layers[%ld]=%ld\n",i,pconf->sz_layers[i]);
for(size_t j=0; j< pconf->sz_layers[i];++j){
printf(" [%ld][%ld]=%ld | ",i,j,pconf->array_dim_in_layers[i][j]);
}
printf("debug: pconf->nb_layers=%ld\n",pconf->nb_layers);
}
}
bool randomizeInitWeight=true;
#define GEN_NEURONS_F_(type)\
config_layers * create_config_layers_from_weight_in_neurons_##type(neurons_##type *base){\
config_layers *pconf=malloc(sizeof(struct config_layers));\
neurons_##type *tmp=base->next_layer;\
pconf->nb_layers=0;\
while(tmp){ ++(pconf->nb_layers); tmp=tmp->next_layer;}\
tmp=base->next_layer;\
pconf->sz_layers=malloc((pconf->nb_layers)*sizeof(size_t));\
pconf->array_dim_in_layers=malloc((pconf->nb_layers)*sizeof(size_t*));\
printf("debug: pconf->nb_layers=%ld\n",pconf->nb_layers);\
size_t layer=0;\
while(tmp){\
pconf->sz_layers[layer]=tmp->weight_in->dim->size;\
printf("debug: pconf->sz_layers[%ld]=%ld\n",layer,pconf->sz_layers[layer]);\
pconf->array_dim_in_layers[layer]=malloc((pconf->sz_layers[layer])*sizeof(size_t));\
for(size_t j=0;j<pconf->sz_layers[layer];++j){\
pconf->array_dim_in_layers[layer][j]=tmp->weight_in->dim->perm[j];\
}\
++layer; tmp=tmp->next_layer;\
}\
return pconf;\
}\
\
void do_not_update_learnig_rate_##type(neurons_##type *N){}\
\
@@ -1034,14 +1123,66 @@ size_t learning_cloneuronset_##type(cloneuronset_##type *clnrnst, data_set_##typ
} \
\
\
GEN_LIST_ALL(ptr_set_NEURONS_##type)\
GEN_FUNC_PTR_LIST_FREE(ptr_set_NEURONS_##type){\
ptr_set_NEURONS_##type p_s_nn = (struct set_neurons_##type *)arg;\
struct set_neurons_##type * create_set_neurons_##type(config_layers *pconf, struct neurons_##type *base, ssize_t score, size_t dateid){\
struct set_neurons_##type *p_set_n=malloc(sizeof(struct set_neurons_##type));\
p_set_n->pconf=pconf;\
p_set_n->base=base;\
p_set_n->score=score;\
p_set_n->dateid=dateid;\
return p_set_n;\
}\
\
void free_set_neurons_##type(struct set_neurons_##type *p_s_nn){\
free_config_layers(p_s_nn->pconf);\
free_neurons_##type(p_s_nn->base);\
free(p_s_nn);\
}\
\
GEN_LIST_ALL(ptr_set_NEURONS_##type)\
GEN_FUNC_PTR_LIST_FREE(ptr_set_NEURONS_##type){\
ptr_set_NEURONS_##type p_s_nn = (struct set_neurons_##type *)arg;\
free_set_neurons_##type(p_s_nn);\
}\
\
ssize_t sum_score_set_neurons_##type(struct main_list_ptr_set_NEURONS_##type *m_set_nrns){\
ssize_t sum_score=0;\
for(struct list_ptr_set_NEURONS_##type *local_set_n=m_set_nrns->begin_list; local_set_n ; local_set_n = local_set_n->next ){\
sum_score+=(local_set_n->value->score);\
}\
return sum_score;\
}\
void put_meaning_of_weitgh_in_set_neurons_##type(struct set_neurons_##type *dst_nrns, struct main_list_ptr_set_NEURONS_##type * m_set_nrns)\
{\
ssize_t sum_score=0;\
for(struct list_ptr_set_NEURONS_##type *local_set_n=m_set_nrns->begin_list; local_set_n ; local_set_n = local_set_n->next ){\
sum_score+=(local_set_n->value->score);\
if(cmp_config_layers(dst_nrns->pconf, local_set_n->value->pconf)){\
printf("debug: config_layers not match in inex = %ls \n",local_set_n->index);\
return;\
}\
}\
struct neurons_##type *tmp_dst=dst_nrns->base;\
while(tmp_dst){\
for(size_t i=0; i<tmp_dst->weight_in->dim->size;++i){\
tmp_dst->weight_in->x[i]=0;\
}\
tmp_dst=tmp_dst->next_layer;\
}\
for(struct list_ptr_set_NEURONS_##type *local_set_n=m_set_nrns->begin_list; local_set_n ; local_set_n = local_set_n->next ){\
local_set_n->value->cur_neurons=local_set_n->value->base;\
tmp_dst=dst_nrns->base;\
while(tmp_dst){\
for(size_t i=0; i<tmp_dst->weight_in->dim->size;++i){\
tmp_dst->weight_in->x[i] +=(((local_set_n->value->score) * (local_set_n->value->cur_neurons->weight_in->x[i]))/sum_score);\
}\
tmp_dst=tmp_dst->next_layer;\
local_set_n->value->cur_neurons=local_set_n->value->cur_neurons->next_layer;\
}\
}\
}\
\
\
+10 -1
View File
@@ -20,7 +20,10 @@ typedef struct config_layers config_layers;
config_layers *create_config_layers(size_t nb_layers, size_t *sz_layers, size_t **array_dim_in_layers);
config_layers *create_config_layers_from_OneD(size_t nb_layers, size_t *array_dim_in_layers);
void free_config_layers(config_layers *pconf);
long int cmp_config_layers(config_layers *c1, config_layers *c2);
config_layers * create_config_layers_from_m_list_ptr_DIMENSION(struct main_list_ptr_DIMENSION *m_l_dim);
config_layers * create_config_layers_from_m_list_dimension(struct main_list_dimension *m_l_dim);
void print_config_layers(config_layers * pconf);
#define GEN_NEURON_(type)\
\
@@ -53,6 +56,8 @@ struct neurons_##type {/* layer */\
};\
typedef struct neurons_##type neurons_##type;\
\
config_layers * create_config_layers_from_weight_in_neurons_##type(neurons_##type *base);\
\
struct func_act_##type {\
type (*func_act)(type x); /* function activation */\
type (*deriv_func_act)(type x); /* derivate func act */\
@@ -131,10 +136,14 @@ size_t learning_cloneuronset_##type(cloneuronset_##type *clnrnst, data_set_##typ
struct set_neurons_##type{\
struct config_layers *pconf;\
struct neurons_##type *base;\
struct neurons_##type *cur_neurons;\
ssize_t score;\
size_t dateid;\
};\
typedef struct set_neurons_##type * ptr_set_NEURONS_##type;\
\
struct set_neurons_##type * create_set_neurons_##type(config_layers *pconf, struct neurons_##type *base, ssize_t score, size_t dateid);\
\
GENERATE_LIST_ALL(ptr_set_NEURONS_##type)\
GEN_HEAD_PTR_LIST(ptr_set_NEURONS_##type)\
\
+193 -2
View File
@@ -447,9 +447,200 @@ j=0;\
\
\
\
\
\
#define EXTRACT_FILE_TO_TENSOR_ATTRIBUTE_NNEURONS_PCONF(type, neuronDst, attribute, file_name_input, m_l_dim) \
do{\
int fd_input;\
fd_input=open(file_name_input, O_RDONLY);\
if ( fd_input == -1 ) {\
fprintf( stderr, "Cannot open file: %s for reading\n",file_name_input );\
exit( -1 );\
}\
size_t buf_size=820;/*need to be more than the nb of char representation of the type*/\
char *input=malloc(buf_size + 1);\
char *recInput=malloc(buf_size + 1);\
memset(recInput,0, buf_size + 1);\
char *iinput=malloc(buf_size * 2);\
/*bool size_unknown=false, broken=false*/; \
bool Done=false;\
int retread = 0, curIn=0, lastNonNumber=0, lenRecIn=0;\
\
list_perm_in_dim *l_p=NULL;\
dimension *dim=NULL;\
size_t ss;\
char *ttmp=NULL;\
char *ppEnd=NULL;\
bool bracketsDown=false/*, endTensor = false*/;\
size_t j=0;\
neurons_##type * tmpNN = neuronDst;\
tensor_##type * T=NULL;\
while(tmpNN /*&& !endTensor*/){\
bracketsDown = false;\
Done = false;\
/* T = tmpNN->attribute;\
if(T == NULL){\
Done = true;\
}*/\
T = tmpNN->attribute;\
while((T == NULL) && (tmpNN!=NULL)){\
tmpNN = tmpNN->next_layer;\
if(tmpNN)\
T = tmpNN->attribute;\
}\
/*printf("debug : dd ttmp = %s, T == NULL?=%d %s\n",ttmp,(T==NULL),#attribute);*/\
if(T == NULL){\
Done = true;\
} \
j=0;\
while(!Done /*&& !endTensor*/){\
if(ttmp == NULL || *ttmp=='\0'){\
for(curIn=0; curIn<lenRecIn; ++curIn){\
iinput[curIn]=recInput[lenRecIn-curIn-1];\
}\
retread = read(fd_input, input, buf_size) ;\
/*endTensor = (retread != buf_size);*/\
/*printf("debug: ************************* ------>input = |%s|, retread=%d, input[ret-1]={%c}\n", input,retread,input[retread-1]);*/\
lenRecIn = 0;\
for(lastNonNumber=retread-1; lastNonNumber>=0; --lastNonNumber){ \
if(((input[lastNonNumber] >='0') && (input[lastNonNumber] <='9'))||(input[lastNonNumber] =='-')||(input[lastNonNumber] =='.')||(input[lastNonNumber] =='E')||(input[lastNonNumber] =='e')){\
recInput[lenRecIn++]=input[lastNonNumber];\
}\
else break; \
}\
recInput[lenRecIn]='\0';\
/*printf("recInput = |%s|\n", recInput);*/\
for(int ii=0; ii<=lastNonNumber; ++ii){\
iinput[curIn++]=input[ii];\
}\
\
iinput[curIn]='\0';\
/*printf("iinput = |%s|\nDone=%d\n", iinput,Done);*/\
ttmp=iinput;\
}\
while(!Done && (*ttmp != '\0') /*&& !endTensor*/){\
/*printf("debug : >> ttmp = %s, bracketsDown=%d\n",ttmp, bracketsDown);*/\
if(*ttmp=='[') {\
bracketsDown=false;\
}\
ppEnd=ttmp;\
if( !bracketsDown){\
while(*ttmp!='\0' && *ppEnd!=']' ){\
if(*ttmp=='['){\
/*printf("debug : [[ ttmp = %s\n",ttmp);\
if(dim)printDebug_dimension(dim,"[DIM]");*/\
if(l_p != NULL){\
free_dimension(dim);\
free_list_perm_in_dim(l_p);\
l_p=NULL;\
}\
/*if(dim)printDebug_dimension(dim,"{DIM}");*/\
}\
ss = strtoul(ttmp, &ppEnd, 10);\
while(ttmp == ppEnd && *ttmp!='\0' && ppEnd[0] !=']'){\
\
if(*ttmp=='['){\
/*printf("debug : [[ ttmp = %s\n",ttmp);\
if(dim)printDebug_dimension(dim,"[DIM]");*/\
if(l_p != NULL){\
free_dimension(dim);\
free_list_perm_in_dim(l_p);\
l_p=NULL;\
}\
/*if(dim)printDebug_dimension(dim,"{DIM}");*/\
}\
\
/*printf("debug : aa ttmp = %s\n",ttmp);*/\
ttmp++;\
ss = strtoul(ttmp, &ppEnd, 10);\
/*printf("debug : bb ttmp = %s\n",ttmp);*/\
}\
if(ppEnd !=ttmp ){\
append_in_list_perm(&l_p,ss);\
}\
ttmp=ppEnd;\
}\
/*printf("debug : cc ttmp = %s\n",ttmp);*/\
if( *ttmp ==']'){\
dim=create_dim_from_list_perm(l_p);\
push_back_list_ptr_DIMENSION(m_l_dim, clone_dim(dim));\
/*push_back_list_dimension(m_l_dim, *dim);*/\
bracketsDown = true;\
printf("debug: dim ptr: %p sizeof(*dim)=%ld sizeof(dimension)=%ld\n",dim,sizeof(*dim), sizeof(dimension));\
if(dim){printDebug_dimension(dim,"{DIM}");}\
j=0;\
\
}\
\
}\
/*printf("debug : <<---->> ttmp = %s, bracketsDown=%d T==NULL? =%d, done?=%d\n",ttmp, bracketsDown, (T==NULL), Done);*/\
if(!Done && bracketsDown){\
/*printf("debug : ee ttmp = %s, T==NULL ? = %d\n %ld vs %ld\n",ttmp,(T==NULL),T->dim->rank,dim->rank);\
printDebug_dimension(dim," DIM");*/\
if((T->dim->rank == dim->rank)){\
\
\
\
type x;\
while(strlen(ttmp) && (*ttmp!='[') && (j<dim->rank)){ \
x = strto_##type(ttmp, &ppEnd);\
while(ttmp == ppEnd && strlen(ttmp) && *ttmp!='[' ){\
/*printf("debug : dd ttmp = %s\n",ttmp);*/\
ttmp++;\
x = strto_##type(ttmp, &ppEnd);\
/*printf("debug : ww ttmp = %s\n",ttmp);*/\
}\
if((*ttmp!='[') && (ttmp != ppEnd)){\
T->x[j++]=x;\
/*printf("debug : x=%lf ===> %d\n",x,(j==dim->rank));*/\
}\
else if ( *ttmp =='[') {\
bracketsDown = false;\
Done=true;\
break;\
}\
ttmp=ppEnd;\
Done=(j==dim->rank);\
/*endTensor=(j==dim->rank);*/\
}\
if(Done) break;\
if(j == dim->rank ){\
Done = true;\
}\
}else {\
/*endTensor = true;*/\
/*Done = true;\
bracketsDown = false;*/\
break;\
}\
}\
if(Done){\
/*printf("debug : done=%d , l_p==NULL?=%d, endTensor=%d\n",Done, (l_p==NULL), endTensor);*/\
if(l_p != NULL){\
free_dimension(dim);\
dim=NULL;\
free_list_perm_in_dim(l_p);\
l_p=NULL;\
}\
}\
\
}\
if(Done) break;\
\
}\
tmpNN = tmpNN->next_layer;\
}\
free(input);\
free(iinput);\
free(recInput);\
close(fd_input);\
if(dim) free_dimension(dim);\
if(l_p) free_list_perm_in_dim(l_p);\
\
}while(0);\
\
\
\
#endif /* NNEURONE_T_FILE_H__C_ */