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

📄 misc.c

📁 #ifdef INTEGER #include "ibp.h" #else #include "rbp.h" #endif /* built-in C functions */
💻 C
📖 第 1 页 / 共 2 页
字号:
/* **************************************************** */
/* file misc.c:  contains pattern manipulation routines */
/*               and miscellaneous other functions.     */
/*                                                      */
/* Copyright (c) 1990-1996 by Donald R. Tveter          */
/*                                                      */
/* **************************************************** */

#ifdef INTEGER
#include "ibp.h"
#else
#include "rbp.h"
#endif

/* built-in function */

extern int rand();

/* homemade functions */

#ifdef INTEGER
extern REAL unscale(), unscaleint();
extern WTTYPE scale();
extern char wtlimithit;
#endif

extern short backoutput(), cbackoutput();
extern void backinner(), cbackinner(), saveweights();
extern void printnetsize(), restoreweights();
extern WTTYPE rdr();
extern void dbd_update(), periodic_update(), qp_update();
extern REAL readchar();
extern int pg(), rclasscheck(), printoutunits();
extern UNIT *locateunit();

extern char *datafile,emptystring,freezing,informat,offby1,outstr[];
extern char probtype, ringbell, summary, *testfile;
extern char *trainfile, update, up_to_date_stats;
extern char zeroderiv, biasset;
extern char seednote, runningflag, outsumsflag, saveonminimum,outformat;
extern char trfiles[], recurinit, benchsummary, wtinitroutine;
extern short nlayers;
extern int lastadd,lastprint,lastsave,maxtries,prevnpats,readerror;
extern int readingpattern,right,saverate,totaliter,window,wrong;
extern int wtsinuse, wttotal, extraconnect, ioconnects, originallypcc;
extern long iotime;
extern SEEDNODE *seedstart;
extern WTTYPE dbdeta, qpeta, sseta, unknown, toler, toosmall;
extern WTTYPE kicksize, kickrange, initrange;
extern LAYER *last, *start;
extern DATA s[2][2], rs[2][2];
#ifdef INTEGER
extern INT32 totaldiff;
#else
extern REAL totaldiff;
#endif
extern REAL toloverall;
extern REAL minimumsofar;

void nullpatterns(list)
int list;
{ /* dispose of any patterns before reading more */
PATLIST *pl, *nextpl;
PATNODE *p;

if (start->patstart[list] != NULL)
 {
  pl = start->patstart[list];
  while (pl != NULL)
   {
    nextpl = pl->next;
    p = pl->pats;
    free(p);
    pl = nextpl;
   };
  pl = last->patstart[list];
  while (pl != NULL)
   {
    nextpl = pl->next;
    p = pl->pats;
    free(p);
    pl = nextpl;
   };
 };
start->patstart[list] = NULL;
last->patstart[list] = NULL;
s[TOL][list].npats = 0;
s[MAX][list].npats = 0;
prevnpats = 0;
}

void resetpats(list)
int list;
{
start->currentpat[list] = NULL;
last->currentpat[list] = NULL;
}

void findendofpats(layer)  /* purpose is to set all layer->currentpat */
LAYER *layer;              /* fields to end of pattern list so more   */
{                          /* patterns can be added at the end.       */
PATLIST *pl;
pl = (PATLIST *) layer->patstart[TRAIN];
while (pl->next != NULL) pl = pl->next;
layer->currentpat[TRAIN] = pl;
}

int copyhidden(u,hidden,layerno)
UNIT *u, **hidden;
int layerno;
{
if (*hidden == NULL)
 {
  sprintf(outstr,"ran out of hidden units in layer %d\n",layerno);
  pg(stdout,outstr);
  return(0);
 }
u->oj = (*hidden)->oj;
*hidden = (*hidden)->next;
return(1);
}

int loadpat()
{
  REAL val;
  int unitnumber;
  UNIT *otherunit, *hunit, *u;
  readingpattern = 1;
  hunit = start->next->units;
  u = start->units;
  while (u != NULL)
   {
    if (informat == 'r') val = rdr(GE,(REAL) DIFFCODE,'0');
    else val = scale(readchar());
    if (readerror != 0) goto errorexit;
    if (val == (REAL) ICODE)
     {
      unitnumber = readint(1,start->unitcount,'0');
      if (readerror != 0) goto errorexit;
      otherunit = locateunit(1,unitnumber);
      u->oj = otherunit->oj;
     }
    else if (val == (REAL) OCODE)
     {
      unitnumber = readint(1,last->unitcount,'0');
      if (readerror != 0) goto errorexit;
      otherunit = locateunit(nlayers,unitnumber);
      u->oj = otherunit->oj;
     }
    else if (val == (REAL) HCODE)
     {
      if (copyhidden(u,&hunit,2) == 0) goto errorexit;
     }
    else if (val == (REAL) CAPHCODE)
     {
      while (u != NULL)
       {
        if (copyhidden(u,&hunit,2) == 0) goto errorexit;
        u = u->next;
       };
      goto leaveok;
     }
    else if (val == (REAL) CAPXCODE)
     {
      while (u != NULL)
       {
        if (hunit == NULL) return(0);
        u->oj = unknown;
        hunit = hunit->next;
        u = u->next;
       };
      goto leaveok;
     }
    else u->oj = val;
    u = u->next;
   };

leaveok:
 readingpattern = 0;
 forward();
 return(1);

errorexit:
 readingpattern = 0;
 return(0);
}

void nextpat(list)
int list;
{
if (start->currentpat[list] == NULL)
 {
  start->currentpat[list] = start->patstart[list];
  last->currentpat[list] = last->patstart[list];
 }
else
 {
  start->currentpat[list] = (start->currentpat[list])->next;
  last->currentpat[list] = (last->currentpat[list])->next;
 };
}

void setinputpat(list)
int list;
{
register PATNODE *pn;
register UNIT *u;
UNIT *hunit;
PATLIST *pl;
  
hunit = start->next->units;
pl = start->currentpat[list];
pn = pl->pats;
u = (UNIT *) start->units;
while (u != NULL)
 {
  if (pn->addr == NULL)  /* load a plain number */
   {
    u->oj = pn->val;
    pn++;
   }
  else
   {
    if (pn->unitno > 0)  /* load a value from an input or output unit */
     {
      u->oj = *(pn->addr);
      pn++;
     }
    /* else load unknown or hidden unit values */
    else if (pn->unitno == CAPHCODE)
     {if (!copyhidden(u,&hunit,2)) return;}
    else if (pn->unitno == HCODE)
     {
      {if (!copyhidden(u,&hunit,2)) return;}
      pn++;
     }
    else if (pn->unitno == CAPXCODE) u->oj = unknown;
    else{sprintf(outstr,"ran out of unit codes, got: %d\n",pn->unitno);
         pg(stdout,outstr); fflush(stdout);};
   };
  u = u->next;
 };
}

void setonepat(list) /* set input pattern */
int list;
{
register UNIT *u;
register LAYER *innerlayers;

setinputpat(list);
innerlayers = start->next;
while (innerlayers->next != NULL)
 {  /* set errors on the inner layer units to 0 */
  u = (UNIT *) innerlayers->units;
  while (u != NULL)
   {
    u->error = 0;
    u = u->next;
   };
  innerlayers = innerlayers->next;
 };
}

void clear()
{
LAYER *p, *lowerlayer;
UNIT *u, *unext, *ulast, *bu;
WTNODE *w, *wnext, *wprev;
int i,j, layerno;
REAL *val;

#ifndef SYMMETRIC
biasset = 0;
#endif
for (i=TRAIN;i<=TEST;i++) for (j=TOL;j<=MAX;j++)
 {
  s[j][i].uncertain = 0;
  s[j][i].iterno = 0;
  s[j][i].right = 0;
  s[j][i].wrong = s[j][i].npats;
  s[j][i].avgerr = 0.0;
  s[j][i].pctright = 0.0;
 };
#ifdef INTEGER
wtlimithit = 0;
#endif
zeroderiv = 0;
lastadd = 0;
toosmall = 0;
totaliter = 0;
lastsave = 0;
lastprint = 0;

/* bring back unused units */
p = last;
while (p != NULL)
 {
  u = p->units;
  while (u != NULL)
   {
    u->inuse = 1;
    u = u->next;
   };
  p = p->backlayer;
 };

/* get rid of input weights from units in the hidden layer below */
/* that are going to be removed (in the next major loop) */
p = last;
while (p->backlayer != NULL)
 {
  u = p->units;
  while (u != NULL)
   {
    w = (WTNODE *) u->wtlist;
    wprev = w;
    w = w->next;  /* the first weight will never be removed! */
    while (w->next != NULL) /* last weight will never be removed! */
     {
      bu = w->backunit;
      layerno = bu->layernumber;
      lowerlayer = start;
      for (i=1;i<=(layerno - 1);i++) lowerlayer = lowerlayer->next;
      if (bu->unitnumber > lowerlayer->initialcount)
       {
        wprev->next = w->next;
        wnext = w->next;
        free(w);
        wttotal = wttotal - 1;
        w = wnext;
       }
      else
       {
        wprev = w;
        w = w->next;
       };
     };
    u = u->next;
   };
  p = p->backlayer;
 };
/* get rid of extra units and their input weights */
/*p = start->next; */
p = start;
while (p != last)
 {
  if (p->unitcount > p->initialcount)
   {
    u = p->units;
    for (i=1;i<=(p->initialcount-1);i++) u = u->next;
    ulast = u;
    u = u->next;
    ulast->next = NULL;
    while (u != NULL)
     {
      unext = u->next;
      w = (WTNODE *) u->wtlist;
      while (w != NULL)
       {
        wnext = w->next;
        free(w);
        wttotal = wttotal - 1;
        w = wnext;
       };
      free(u);
      u = unext;
     };
   };
  p->unitcount = p->initialcount;
  p = p->next;
 };
/* clear the input layer units */
u = start->units;
while (u != NULL)
 {
  u->oj = 0;
  u = u->next;
 };
/* re-initialize the weights and such */
wttotal = 0;
p = start->next;
while (p != NULL)
 {
  u = (UNIT *) p->units;
  while (u != NULL)
   {
    u->oj = 0;
    w = (WTNODE *) u->wtlist;
    while (w != NULL)
     {
#ifdef SYMMETRIC
      if (w->next != NULL)
       { /* skip threshold weight */
        *(w->weight) = 0;
        *(w->olddw) = 0;
       };
#else
      w->weight = 0;
      w->olddw = 0;
      w->slope = 0;
      wttotal = wttotal + 1;
      if (w->inuse == 1 || w->inuse == 0 || w->inuse == -1) w->inuse = 1;
      else w->inuse = 2;
#endif
      w = w->next;
     };
    u = u->next;
   };
  p = p->next;
 };
wtsinuse = wttotal;
}

void stats()
{
 if ((update == 'c' || update == 'C') && up_to_date_stats == '-')
  {
   s[TOL][TRAIN].uncertain = 1;
   s[MAX][TRAIN].uncertain = 1;
  };
 s[TOL][TRAIN].off = -1;
 s[MAX][TRAIN].off = -1;
 right = s[TOL][TRAIN].npats - wrong;
 s[TOL][TRAIN].right = right;
 s[TOL][TRAIN].wrong = wrong;
 s[TOL][TRAIN].iterno = totaliter;
 s[MAX][TRAIN].iterno = totaliter;
 s[TOL][TRAIN].avgerr =
    unscaleint(totaldiff) / (REAL) ((right + wrong) * last->unitcount);
 s[MAX][TRAIN].avgerr = s[TOL][TRAIN].avgerr;
 s[TOL][TRAIN].pctright = 100.0 *
    (REAL) right / (REAL) (right + wrong);
 if (probtype == 'c') s[MAX][TRAIN].pctright = s[MAX][TRAIN].right
    * 100.0 / (s[MAX][TRAIN].right + s[MAX][TRAIN].wrong);
}

int printstats(writefile,list,fileid,s)
FILE *writefile;
int list, fileid;
DATA s[2][2];
{
char *f;
int patcount;

if (fileid == 1)
 {
  if (list == TEST) f = testfile;
  else if (*trainfile == emptystring) f = datafile;
  else f = trfiles;
  patcount = s[TOL][list].npats;
  if (list == TRAIN) {if (pg(writefile,trfiles)) return(1);}
  else
   {
    pg(writefile,"tf ");
    pg(writefile,testfile);
    if (pg(writefile,"\n")) return(1);
   };
 };

lastprint = totaliter;
if (writefile != stdout) pg(writefile,"*");
sprintf(outstr,"%5d",s[TOL][list].iterno); pg(writefile,outstr);
if (s[TOL][list].off == -1) pg(writefile," -1 "); else pg(writefile,"    ");
pg(writefile,"(TOL) ");
sprintf(outstr,"%6.2f %% ",s[TOL][list].pctright); pg(writefile,outstr);
if (s[TOL][list].uncertain == 1) pg(writefile,"?? ");
sprintf(outstr,"(%1d right  ",s[TOL][list].right);pg(writefile,outstr);
sprintf(outstr,"%1d wrong)",s[TOL][list].wrong); pg(writefile,outstr);
sprintf(outstr,"  %7.5f err/unit\n",s[TOL][list].avgerr);
if (pg(writefile,outstr)) return(1);
if (probtype == 'g') return(0);

lastprint = totaliter;
if (writefile != stdout) pg(writefile,"*");
sprintf(outstr,"%5d",s[MAX][list].iterno); pg(writefile,outstr);
if (s[MAX][list].off == -1) pg(writefile," -1 "); else pg(writefile,"    ");
pg(writefile,"(MAX) ");
sprintf(outstr,"%6.2f %% ",s[MAX][list].pctright); pg(writefile,outstr);
if (s[TOL][list].uncertain == 1) pg(writefile,"?? ");
sprintf(outstr,"(%1d right  ",s[MAX][list].right); pg(writefile,outstr);
sprintf(outstr,"%1d wrong)",s[MAX][list].wrong); pg(writefile,outstr);
sprintf(outstr,"  %7.5f err/unit\n",s[MAX][list].avgerr);
if (pg(writefile,outstr)) return(1); else return(0);
}

void maxerrors()
{
register UNIT *u;
register short unitnumber;
register largest;
register short largestunit, answer;
register PATNODE *targetptr;
PATLIST *pl;

⌨️ 快捷键说明

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