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

📄 art_ext.c

📁 自适应共振神经网络可以实现自动学习与分类具有自适应的功能
💻 C
📖 第 1 页 / 共 5 页
字号:
/* ------------------------------------------------------------------------- */
/*                           The ART Gallery                                 */
/* ------------------------------------------------------------------------- */
/* Art_Ext.c                                                                 */
/*    Version:     1.0                                                       */
/*    Written By:  Lars H. Liden         laliden@cns.bu.edu                  */
/*    Last Update: 8/02/95                                                   */
/*                                                                           */
/* The following code contains the external functions of The ART Gallery.    */
/*                                                                           */
/* Please see Art_Doc.txt for a full documentation.                          */
/*                                                                           */
/* Send all bug reports to laliden@cns.bu.edu                                */
/* ------------------------------------------------------------------------- */
#ifndef __Art_Gal_h
#include "Art_Gal.h"
#endif

/* ========================================================================= */
/*                         EXTERNAL PROCEDURES                               */
/*   The following procedures are intened to be used as functions in other   */
/*     programs.  They include some error checking of their arguments.       */
/* ========================================================================= */

/* -------------------------------------------------------------------------- */
/* InitNet                                                                    */
/*      Given network parameters, initializes network values and allocates    */
/*      spaces                                                                */
/*                                                                            */
/* Arguments:                                                                 */
/*            net         -  A pointer to a network of type netTYPE           */
/*            set         - A pointer to a set of patterns of type setTYPE    */
/*            net_type    -  ART or ARTMAP                                    */
/*            componentA  -  ART1 or FUZZYART                                 */
/*                            For net_type ART, this is the type of ART       */
/*                                network that is created.                    */
/*                            For net_type ARTMAP, this the the ArtA network  */
/*                                used for ArtMap input vectors.              */
/*            styleA      -  NONE or COMPLIMENT                               */
/*            componentB  -  ART1, FUZZYART or NONE                           */
/*                            For net-type ARTMAP, this the the ArtB network  */
/*                                used for ArtMap input vectors.              */
/*            styleB      -  NONE or COMPLIMENT                               */
/*                                                                            */
/* Return Values                                                              */
/*           0 - Network successfuly Initialized                              */
/*           1 - Invalid Network Type                                         */
/*           2 - Invalid Art Component                                        */
/*           3 - Invalid Style Type                                           */
/*           4 - Invalid number or inputs or outputs (<=0)                    */
/*           5 - Network was already initialized                              */
/*           6 - Memory Allocation Failure (out of memory)                    */
/*           7 - When net NULL                                                */
/* -------------------------------------------------------------------------- */
int VBPREFIX   InitNet(netPTR net, int net_type, 
	     int componentA, int styleA, int num_inputs,
             int componentB, int styleB, int num_outputs) {

  if (net==NULL)
    return 7;

  /* Check if already initialized                                            */
  if (net->initB==TRUE) return 5;
  
  /* Check styles                                                            */
  if ((styleA!=NONE)&&(styleA!=COMPLIMENT)) return 3;

  /* Check input size                                                        */
  if (num_inputs<=0) return 4;

  net->checkB           = FALSE;    /* Hasn't been tested against the set    */
  net->doneB            = FALSE;    /* Training hasn't occured               */
  net->type             = net_type;
  net->num_inputs       = num_inputs;

  net->num_patterns     = 0;   /* No training yet, so all zero               */
  net->num_epochs       = 0;
    
  if (net_type==ART) {
    /* First check validity of Art type                                      */
    if ((componentA!=ART1)&&(componentA!=FUZZYART)) return 2;

    net->num_outputs    = 0; 
    if (InitArt(&net->art,componentA,styleA,num_inputs)) return 6;
    net->initB            = TRUE;     /* The network has been initialized    */
    return 0;
  }
  else if (net_type==ARTMAP) {
     /* Check output size                                                    */
   if (num_outputs<=0) return 4;

    /* Check validity of Art types                                           */
    if (((componentA!=ART1)&&(componentA!=FUZZYART))||
	((componentB!=ART1)&&(componentB!=FUZZYART)&&(componentB!=NONE)))
      return 2;

    /* Check styles                                                          */
    if ((styleB!=NONE)&&(styleB!=COMPLIMENT)) return 3;

    net->num_outputs       = num_outputs;       
    if (InitMap(&net->map,componentA,styleA,componentB,styleB,
	    num_inputs,num_outputs)) return 6;
    net->initB            = TRUE;     /* The network has been initialized    */
    return 0;
  }
  else  /* Wrong network type                                                */
    return 1;
}



/* -------------------------------------------------------------------------- */
/* InitSet                                                                    */
/*           Initialize values for a new pattern set                          */
/*                                                                            */
/* Arguments:                                                                 */
/*           set          - A pointer to a set of patterns of type setTYPE    */
/*           num_inputs   - Number of input values                            */
/*           type_inputs  - ANALOG or BINARY                                  */
/*           num_outputs  - Number of output values                           */
/*           type_outputs - ANALOG, BINARY or NONE                            */
/*                                                                            */
/* Return Values                                                              */
/*           0 - Set successfuly Initialized                                  */
/*           1 - Initialization Failure - Invalid Set Types                   */
/*           2 - Output type NONE specified with non-zero number of outputs   */
/*           3 - Memory Allocation Failure (out of memory)                    */
/*           4 - When set is NULL                                             */
/* -------------------------------------------------------------------------- */
int VBPREFIX   InitSet(setPTR set, int num_inputs,  int type_inputs, 
		     int num_outputs, int type_outputs) {
  int i,j;

  if (set==NULL)
    return 4;

  /* First check that types are valid                                     */
  if ((type_inputs!=ANALOG)&&(type_inputs!=BINARY)) return 1;
  if ((type_outputs!=ANALOG)&&(type_outputs!=BINARY)&&(type_outputs!=NONE)) 
    return 1;

   
  /* If type_output is NONE, num_outputs should be 0                      */
  if ((type_outputs==NONE)&&(num_outputs!=0)) return 2;

  set->max_num_patterns = 50;           /* Allocate space for 50 patterns */
  set->num_patterns     = 0;            /* No patterns to start with      */
  set->num_inputs       = num_inputs;
  set->type_inputs      = type_inputs;
  set->style_inputs     = NONE;        /* Not compliment coded until load */
  set->num_outputs      = num_outputs;
  set->type_outputs     = type_outputs;
  set->style_outputs    = NONE;        /* Not compliment coded until load */

  /* Allocate space for patterns */
  set->pattern = (patTYPE *)malloc((size_t) 
				   (set->max_num_patterns*sizeof(patTYPE))); 
  if (set->pattern==NULL) return 3;

  /* Allocate space for input/output vectors patterns                     */
  for (i=0;i<set->max_num_patterns;i++) {
    set->pattern[i].input=(float *)malloc((size_t) (set->num_inputs*sizeof(float)));
    if (set->pattern[i].input==NULL) return 3; 
    /* If there are output patterns, allocate space                       */
    if (set->type_outputs!=NONE)  {
      set->pattern[i].output=(float *)malloc((size_t) (set->num_outputs*sizeof(float)));
      if (set->pattern[i].output==NULL) return 3;
    }
  }
  set->initB  = TRUE;  /* The set has been initialized                     */
  set->checkB = FALSE; /* The set has not been checked against the network */
  return 0;
}



/* ------------------------------------------------------------------------- */
/* TrainSet                                                                  */
/*           Given a pointer to a network, and a pointer to a pattern set,   */
/*           trains 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        */
/*         max_epoch - Maximum number of training epochs                     */
/*                                                                           */
/* 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 - Memory Allocation Failure (out of memory)                   */
/* ------------------------------------------------------------------------- */
int VBPREFIX  TrainSet(netPTR net, setPTR set, int max_epoch) {
  int      epoch;           /* Number of times passed through training set */
  int      i,j,pat;         /* Counters                                    */
  int      *output_list;    /* List of outputs                             */
  int      used,unused;     /* Number of used and unused nodes             */
  int      oldA_used;       /* Old number of ArtA nodes used               */
  int      oldB_used;       /* Old number of ArtB nodes used               */

  /* --------- Check that set was initialized first ---------------------- */
  if ((set==NULL)||(set->initB!=TRUE))
    return 1;

  /* --------- Check that network 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   */

  /* ========= If everything in order, then train the network ============ */

  /* --------------- If network is a plain Art network ------------------- */
  if (net->type == ART) {
    /* Initialize Variables */
    oldA_used             = net->art.nodes_used;
    net->art.num_reset   = 0;           /* To ensure at least on epoch    */
    epoch                = 0;
    net->doneB           = FALSE;     /* Network isn't done with training */    

    /* Do another Epoch if there were any resets */
    while ((!(net->doneB))&&(epoch!=max_epoch)) {
      epoch++;

      net->art.num_reset = 0; 
      oldA_used = net->art.nodes_used;
      
      /* Train each pattern in the set checking for mem. allocation errs  */
      for (pat=0;pat<set->num_patterns;pat++) {
	ActivateArt(&net->art,set->pattern[pat].input);
	if (TrainArtPat(&net->art,set->pattern[pat].input)) {
	  net = NULL;
	  return 7;
	}

      }

      /* Check in no more resents, if so then done training               */
      if ((net->art.num_reset==0)&&(oldA_used==net->art.nodes_used))
	net->doneB = TRUE;
    }
      
    /* Update network statistics based on new training */
    net->num_patterns += set->num_patterns;
    net->num_epochs   += epoch;
  }

  /* ------------------ If network is an ArtMap network ------------------ */
  else if (net->type == ARTMAP) {
    /* Initialize Variables */   
    oldA_used               = net->map.artA.nodes_used;
    oldB_used               = net->map.artB.nodes_used;
    epoch                   = 0;
    net->doneB              = FALSE; /* Network isn't done with training */    

    /* Do another Epoch when not done or hit max number of epochs        */
    while ((!(net->doneB))&&(epoch!=max_epoch)) {
      epoch++;

      /* Reset Counters                                                  */
      net->map.num_mismatch   = 0; 
      net->map.artA.num_reset = 0; 
      net->map.artB.num_reset = 0;

      /* Update Size                                                     */
      oldA_used = net->map.artA.nodes_used;
      oldB_used = net->map.artB.nodes_used;

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

⌨️ 快捷键说明

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