📄 art_ext.c
字号:
/* Update network statistics based on new training */
net->num_patterns += set->num_patterns;
net->num_epochs += epoch;
}
return 0;
}
/* ------------------------------------------------------------------------- */
/* ShowPat */
/* Given a pointer to a network, a pointer to a pattern set, and a */
/* pattern number, calculates network activations for that pattern */
/* number in the pattern set. Uses the current vigilence level. */
/* */
/* Arguments: */
/* net - A pointer to an initialized netowrk of type netTYPE */
/* set - A pointer to a set of patterns of type setTYPE */
/* pat_num - A number of a pattern in the set */
/* */
/* Return errs: */
/* 0 - When successful */
/* 1 - When the set wasn't initialized first or NULL */
/* 2 - When the network wasn't initialized first or NULL */
/* 3 - ANALOG inputs given to BINARY network */
/* 4 - Input Size Incompatible */
/* 5 - Output Size Incompatible */
/* 6 - No output patterns for ARTMAP network */
/* 7 - When pattern number is larger than pattern set (or < 0) */
/* ------------------------------------------------------------------------- */
int VBPREFIX ShowPat(netPTR net,setPTR set, int pat_num) {
int sum_outputs; /* Counter for number of active output */
int i,j; /* Counters */
/* --------- Check that set was initialized first ---------------------- */
if ((set==NULL)||(set->initB!=TRUE))
return 1;
/* --------- Check that netowrk was initialized first ------------------ */
if ((net==NULL)||(net->initB!=TRUE))
return 2;
/* --- Check to see if the net and set were tested for compatibility --- */
if ((net->checkB==FALSE)||(set->checkB==FALSE))
if (CheckNetSet(net,set)) /* If not check compatibilty */
return (CheckNetSet(net,set)); /* If there was an error return it */
/* -------------------- Check pattern number --------------------------- */
if ((pat_num<0)||(pat_num>set->num_patterns))
return 7;
/* ======== If everything in order, then activate the network ========== */
/* --------------- If network is a plain Art network ------------------- */
if (net->type == ART) {
/* Activate Art Network */
ActivateArt(&net->art,set->pattern[pat_num].input);
}
/* ------------------ If network is an ArtMap network ------------------ */
else if (net->type == ARTMAP) {
/* Activate Art Networks */
ActivateArt(&net->map.artA,set->pattern[pat_num].input);
ActivateArt(&net->map.artB,set->pattern[pat_num].output);
/* Check that there was a winning ArtA category */
if (net->map.artA.win_cat!=-1)
/* If so, activate Mapfield Nodes */
for (i=0;i<net->map.artB.nodes_used;i++) /* For each mapfield node */
*(net->map.map+i)=(*(*(net->map.map_weight+net->map.artA.win_cat)+i));
else
/* If not, set Mapfield Activation to zero */
for (i=0;i<net->map.artB.nodes_used;i++) /* For each mapfield node */
*(net->map.map+i)=0;
}
return 0;
}
/* ------------------------------------------------------------------------- */
/* TestNet */
/* Given a network, a pattern set and four integer pointers, tests */
/* the network on the pattern set. */
/* */
/* Arguments: */
/* net - A pointer to an initialized netowrk of type netTYPE */
/* set - A pointer to a set of patterns of type setTYPE */
/* tot_correct - A pointer to an integer */
/* tot_errors - A pointer to an integer */
/* tot_noansA - A pointer to an integer */
/* tot_noansB - A pointer to an integer */
/* */
/* Return errs: */
/* 0 - When successful */
/* 1 - When the set wasn't initialized first or NULL */
/* 2 - When the network wasn't initialized first or NULL */
/* 3 - ANALOG inputs given to BINARY network */
/* 4 - Input Size Incompatible */
/* 5 - Output Size Incompatible */
/* 6 - No output patterns for ARTMAP network */
/* 7 - When pattern number is larger than pattern set (or < 0) */
/* ------------------------------------------------------------------------- */
int VBPREFIX TestNet(netPTR net, setPTR set, int *tot_correct, int *tot_errors,
int *tot_noansA, int *tot_noansB) {
int total_correct = 0;
int total_errors = 0;
int total_noansA = 0;
int total_noansB = 0;
float sum_map;
float sum_cat;
int i,j;
int wrongB;
/* --------- Check that set was initialized first ---------------------- */
if ((set==NULL)||(set->initB!=TRUE))
return 1;
/* --------- Check that netowrk was initialized first ------------------ */
if ((net==NULL)||(net->initB!=TRUE))
return 2;
/* --- Check to see if the net and set were tested for compatibility --- */
if ((net->checkB==FALSE)||(set->checkB==FALSE))
if (CheckNetSet(net,set)) /* If not check compatibilty */
return (CheckNetSet(net,set)); /* If there was an error return it */
total_correct = 0;
total_errors = 0;
total_noansA = 0;
total_noansB = 0;
if (net->type==ARTMAP) {
/* Show each pattern checking for errors */
for (i=0;i<set->num_patterns;i++) {
ShowPat(net,set,i);
/* Check that there was a winning TypeA and ArtB category */
if (net->map.artA.win_cat==-1)
total_noansA++;
else if (net->map.artB.win_cat==-1)
total_noansB++;
else {
/* Check if correct */
wrongB=FALSE;
/* If ArtB is of type NONE check mapfield vigilence */
if (net->map.artB.type==NONE) {
sum_map = 0;
sum_cat = 0;
for (j=0;j<net->map.artB.nodes_used;j++) {
sum_map += FuzzInt((*(net->map.map+j)),
(*(net->map.artB.cat+j)));
sum_cat += FuzzUni((*(net->map.map+j)),
(*(net->map.artB.cat+j)));
}
/* Check vigilence at mapfield */
if ((sum_map/sum_cat)<net->map.vigil)
wrongB=TRUE;
}
/* If ArtB not of type NONE just compare ArtB winner */
else {
for (j=0;j<net->map.artB.nodes_used;j++) {
if ((((int)((*(net->map.map+j)))==1)&&(net->map.artB.win_cat!=j))||
(((int)((*(net->map.map+j)))==0)&&(net->map.artB.win_cat==j)))
wrongB=TRUE;
}
}
if (wrongB)
total_errors++;
else
total_correct++;
}
}
}
else
for (i=0;i<set->num_patterns;i++) {
ShowPat(net,set,i);
/* Check to see if there was a winner */
if (net->art.win_cat==-1)
total_noansA++;
else
total_correct++;
}
*tot_correct = total_correct;
*tot_errors = total_errors;
*tot_noansA = total_noansA;
*tot_noansB = total_noansB;
return 0;
}
/* ------------------------------------------------------------------------- */
/* SaveNet */
/* Given pointer to a network and a filename prefix, saves network */
/* into {filename}.net */
/* */
/* Arguments: */
/* net - A pointer to an initialized netowrk of type netTYPE */
/* file_prefix - A pointer to a charater string */
/* */
/* Return Value: */
/* 0 - When saved */
/* 1 - When network not initialized, NULL or never trained */
/* 2 - Error opening save file */
/*-------------------------------------------------------------------------- */
int VBPREFIX SaveNet(netPTR net, char *file_prefix) {
char Save_fname[84];
FILE *SaveFile;
int i,j;
if ((net==NULL)||(net->initB!=TRUE)||(net->num_epochs==0))
return 1;
/* Add ".net" to Save file prefix */
strncpy(Save_fname,file_prefix,80);
strcat(Save_fname,".net");
SaveFile=fopen(Save_fname,"w");
if (SaveFile==NULL)
return 2;
fprintf(SaveFile,"%i\n",net->type);
fprintf(SaveFile,"%i\n",net->num_inputs);
fprintf(SaveFile,"%i\n",net->num_outputs);
if (net->type == ART)
SaveArt(&net->art,SaveFile);
else /* if net->type == ARTMAP */
SaveMap(&net->map,SaveFile);
fprintf(SaveFile,"%i\n",net->num_patterns);
fprintf(SaveFile,"%i\n",net->num_epochs);
fclose(SaveFile);
return 0;
}
/* ------------------------------------------------------------------------- */
/* LoadNet */
/* Given a filename prefix, loads {filename}.net into a network */
/* structure setting appropriate initial values. */
/* */
/* Arguments: */
/* net - A pointer to an initialized netowrk of type netTYPE */
/* file_prefix - A pointer to a charater string */
/* */
/* Return Value: */
/* 0 - If successful */
/* 1 - When file not found */
/* 2 - Memory Allocation Failure (out of memory) */
/* 3 - When net NULL */
/*-------------------------------------------------------------------------- */
int VBPREFIX LoadNet(netPTR net, char *file_prefix) {
char load_fname[84];
FILE *LoadFile;
int temp_int;
float temp_flt;
int i,j;
if (net==NULL)
return 3;
/* Add ".net" to load file prefix */
strncpy(load_fname,file_prefix,80);
strcat(load_fname,".net");
errno=0;
LoadFile=fopen(load_fname,"r");
if (LoadFile==NULL) {
if (errno==ENOENT) return 1;
else return 2;
}
net->initB = FALSE; /* The network is not initialized yet */
net->doneB = FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -