⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 art_ext.c

📁 自适应共振神经网络可以实现自动学习与分类具有自适应的功能
💻 C
📖 第 1 页 / 共 5 页
字号:

            /**< Train each pattern in the set checking for mem. allocation errs */
            for (pat = 0; pat < set->num_patterns; pat++) 
            {
                if (TrainMapPat(&net->map, &set->pattern[pat])) 
                {
                    net = NULL;
                    return 7;
                }
            }
            /**< Check if done training                                          */
            if  ((net->map.num_mismatch == 0)&&
                (net->map.artA.num_reset == 0)&&
                (net->map.artB.num_reset == 0)&&
                (oldA_used == net->map.artA.nodes_used)&&
                (oldB_used == net->map.artB.nodes_used))
            {
                net->doneB = TRUE;
            }
        }

        /**< Update network statistics based on new training                   */
        net->num_patterns  += set->num_patterns;
        net->num_epochs    += epoch;
    }


    return 0;
}

/** \fn int   ShowPat(netPTR net,setPTR set, int pat_num)
 *            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. 
 *                                                                          
 * \param net A pointer to an initialized netowrk of type netTYPE
 * \param set A pointer to a set of patterns of type setTYPE
 * \param pat_num number of a pattern in the set
 * \retval 0 When successful
 * \retval 1 When the set wasn't initialized first or NULL
 * \retval 2 When the network wasn't initialized first or NULL
 * \retval 3 NODE_ANALOG inputs given to NODE_BINARY network
 * \retval 4 Input Size Incompatible
 * \retval 5 Output Size Incompatible
 * \retval 6 No output patterns for ARTMAP network
 * \retval 7 When pattern number is larger than pattern set (or < 0)
 */
int   ShowPat(netPTR net,setPTR set, int pat_num) 
{
    int     i;                 /* 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 - NODE_ANALOG inputs given to NODE_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  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 ART_TYPE_NONE check mapfield vigilence              */
                if (net->map.artB.type == ART_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 ART_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 
    {
        char  Save_fname[84] = "result.tst";
        int bSave = TRUE;
        FILE  *SaveFile;

        SaveFile=fopen(Save_fname,"w");

        if (SaveFile==NULL)
        {
            bSave = FALSE;
        }

        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++;
            }
            if (bSave) fprintf(SaveFile, "%d\n", net->art.win_cat);
        }
        if (bSave) fclose(SaveFile);
    }
    *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  SaveNet(netPTR net, char *file_prefix) 
{
    char  Save_fname[84];
    FILE  *SaveFile;

    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;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -