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

📄 bp.c

📁 bp神经网络,反向传播神经网络!感觉还不错的
💻 C
📖 第 1 页 / 共 3 页
字号:
lastprint = 0;
lastsave = 0;
probtype = 'g';
noise = 0;
offby1 = '-';
outformat = 'r';
pagesize = 24;
pccnet = 0;
ioconnects = 0;
stmsize = 0;
recurinit = '-';
prevnpats = 0;
printrate = 10;

ringbell = '-';
saverate = MAXINT;
minimumsofar = MAXINT;
saveonminimum = '-';
seedstart = (SEEDNODE *) malloc(sizeof(SEEDNODE));
snode = (SEEDNODE *) malloc(sizeof(SEEDNODE));
seedstart->next = snode;
snode->val = 0;
srand(0);
snode->next = NULL;
#ifdef LOOKUP
{
 int i; double x,y;
 for (i=0;i<=7807;i++)
  {
   x = (double) i / 1024.0;
   y = 1.0 / (1.0 + exp(-x));
   sigmoid[i] = (WTTYPE) (y * 1024.0 + 0.5);
  };
}
#endif

for (i=TRAIN;i<=TEST;i++) for (j=TOL;j<=MAX;j++)
 {
  s[j][i].uncertain = 0;
  s[j][i].npats = 0;
  s[j][i].iterno = 0;
  s[j][i].right = 0;
  s[j][i].wrong = 0;
  s[j][i].avgerr = 0.0;
  s[j][i].pctright = 0.0;
 };
stdthresh = 0;
biasset = 0;
sysstr[0] = '\0';
summary = '+';
timestoread = 1;
toler = scale(0.1);
toloverall = 0.0;
toosmall = 0;            /* size of weights too small to use */
trigger = 0.1;
unknown = scale(0.5);
update = 'p';
up_to_date_stats = '-';
wtfilecount = 0;
wtfile = "weights";
trfiles[0] = '\0';
runningflag = '+';
wtformat = 'r';
#ifdef INTEGER
wtlimithit = 0;
#endif
wttotal = 0;
zeroderiv = 0;
maxiter = 100;

ah = 's';
ao = 's';
hbiasact = scale(1.0);
obiasact = scale(1.0);
Dh = scale(1.0);
Do = scale(1.0);
}

int nonetwork()
{
if (start != NULL) return(0);
pg(stdout,"there is no network\n");
return(1);
}

int nopatterns()
{
if (s[TOL][TRAIN].npats != 0) return(0);
pg(stdout,"there are no patterns\n");
return(1);
}

void setbiasact(layer) /* sets the bias unit ACTIVATION VALUE */
LAYER *layer;
{
 UNIT *u;
 WTNODE *w;
 
 u = layer->units;
 while (u != NULL)
  {
   w = u->wtlist;
   while (w != NULL)
    {
     if (w->backunit->unitnumber == 32767)
        w->backunit->oj = layer->biasact;
     w = w->next;
    };
   u = u->next;
  };
}

int patternencode(list) /* finds addresses of pattern values found */
int list;               /* within the net */
{
 PATNODE *pn;
 PATLIST *pl;
 int i, n, finished;
 UNIT *u;
 resetpats(list);
 pl = start->currentpat[list];
 nextpat(list);
 while (pl != NULL)
  {
   pn = pl->pats;
   n = start->unitcount;
   i = 1;
   finished = 0;
   while (i <= n && !finished)
    {
     if (pn->addr != NULL && pn->addr != (WTTYPE *) -1)
      {
       u = locateunit((int) pn->layer, (int) pn->unitno);
       pn->addr = &(u->oj);
      };
     i = i + 1;
     pn++;
     if (pn->unitno == CAPXCODE || pn->unitno == CAPHCODE) finished = 1;
    };
   nextpat(list);
  };
}

/* for a SIGINT, restart in cmdloop */
#ifdef UNIX
void restartcmdloop()
#else
void restartcmdloop(int dummy)
#endif
{
while (data != stdin) popfile();
signal(SIGINT,restartcmdloop);
longjmp(cmdloopstate,1);
}
 
void cmdloop()    /* read commands and process them */
{
int finished, layerno, unitno, layer1, layer2, node1, node2;
int i, itemp, itemp2;
WTTYPE temp, temp2, *target;
PATLIST *pl;
REAL rtemp;
LAYER *p;
UNIT *u, *n1, *n2;
WTNODE *w;
SEEDNODE *snode, *sprev;

setjmp(cmdloopstate); /* restart here from SIGINT */
finished = 0;
do{
   if (data == stdin) pg(stdout,"[ACDFGMNPQTW?!acdefhlmopqrstw]? ");
   if (data == stdin) lineno = 0;
   while(ch == ' ' || ch == '\n') ch = readch();
   switch (ch) {

case EOF:
popfile();
if (data == stdin) pg(stdout,"taking commands from stdin now\n");
break;

case '?': menus('?'); break;

case '!':
i = 0;
do ch = readch(); while (ch == ' ');
if (ch == '\n')
 {
  system(sysstr);
  break;
 }
else if (ch == '"') break; /* loading string but not executing */
while ((ch != '\n' && ch != '"') && i <= 80)
 {
  sysstr[i] = ch;
  ch = readch();
  i = i + 1;
 };
if (ch == '"') sysstr[i-1] = '\0';
else
 {
  sysstr[i] = '\0';
  system(sysstr);
 };
bufferptr = bufferptr - 1;
break;

case '*': break;  /* * on a line is a comment */

case 'a':
do ch = readch(); while (ch == ' ');
if (ch == '0' || ch == '.')
 {
  bufferptr = bufferptr - 1;
  temp = rdr(GE,0.0,'a');
  if (!readerror) alpha = temp;
  goto enda;
 }
else bufferptr = bufferptr - 1;
if (nonetwork()) goto enda;
while (ch != '\n' && ch != '*')
 {
  ch = readch();
  if (ch == 'a')
   {
    do ch = readch(); while (ch == ' ');
    if (ch == 'l' || ch == 's')
       {
        p = start;
        while (p != NULL) {p->activation = ch; p = p->next;};
        ah = ch;
        ao = ch;
       }
    else if (ch == 'h')
     {
      do ch = readch(); while (ch == ' ');
      if (ch == 'l' || ch == 's')
         {
          p = start;
          while (p != last) {p->activation = ch; p = p->next;};
          ah = ch;
         }
      else texterror();
     }
    else if (ch == 'o')
     {
      do ch = readch(); while (ch == ' ');
      if (ch == 'l' || ch == 's')
         {
          last->activation = ch;
          ao = ch;
         }
      else texterror();
     }
    else texterror();
   }
  else if (ch == 'D')
   {
    do ch = readch(); while (ch == ' ');
    if (ch == 'h')
     {
      temp = rdr(GT,0.0,'a');
      if (readerror) goto enda;
      p = start;
      while (p != last) {p->D = temp; p = p->next;};
      Dh = temp;
     }
    else if (ch == 'o')
     {
      temp = rdr(GT,0.0,'a');
      if (readerror) goto enda;
      last->D = temp;
      Do = temp;
     }
    else if (ch >= '0' && ch <= '9')
     {
      bufferptr = bufferptr - 1;
      temp = rdr(GT,0.0,'a');
      if (readerror) goto enda;
      p = start;
      while (p != NULL) {p->D = temp; p = p->next;};
      Do = temp;
      Dh = temp;
     }
    else texterror();
   }
  else if (ch == 'd')
   {
    do ch = readch(); while (ch == ' ');
    if (ch == 'd' || ch == 'f' || ch == 'c' || ch == 'o') deriv = ch;
    else texterror();
    if (deriv == 'o') deriv = 'c';
   }
  else if (ch == 'i')
   {
    do ch = readch(); while (ch == ' ');
    if (ch == '+' || ch == '-') recurinit = ch;
    else texterror();
   }
  else if (ch == 'u')
   {
    do ch = readch(); while (ch == ' ');
    if (ch == 'j') ch = 'd';
#ifdef SYMMETRIC
    if (ch == 'C' || ch == 'c' || ch == 'p') update = ch;
#else
    if (ch == 'C' || ch == 'c' || ch == 'p' || ch == 'd' || ch == 'q')
       update = ch;
#endif
    else texterror();
   }
  else if (ch == '*' || ch == '\n' || ch == ' ');
  else texterror();
 }
bufferptr = bufferptr - 1;
enda: break;

case 'c': {int initialize;
if (nonetwork()) goto endc;
initialize = 0;
do ch = readch(); while (ch == ' ');
if (ch == '\n' || ch == '*') goto clearnet;
else if (ch == 'i')
 {
  initialize = 1;
  do ch = readch(); while (ch == ' ');
  if (ch == '\n' || ch == '*') goto clearnet;
  else
     {
      bufferptr = bufferptr - 1;
      temp2 = rdr(GE,scale(0.0),'c');
      if (readerror) goto endc;
      initrange = temp2;
     };
 };

clearnet:
clear();
srand(seed);
if (initialize == 1)
 {
  kick((WTTYPE) 0,initrange);
  sprintf(outstr,"seed = %d, range = -%5.2f to +%5.2f\n",seed,
          unscale(initrange), unscale(initrange));
  pg(stdout,outstr);
 }
else {sprintf(outstr,"seed = %d\n",seed); pg(stdout,outstr);};
endc: break;};

#ifndef SYMMETRIC
case 'd':
while (ch != '\n' && ch != '*')
 {
  ch = readch();
  if (ch == 'd')
   {
    temp = rdr(GT,0.0,'d');
    if (!readerror) decay = temp;
   }
  else if (ch == 'e')
   {
    temp = rdr(GT,0.0,'d');
    if (!readerror && !nonetwork())
     {
      dbdeta = temp;
      initialize_etas(dbdeta);
     };
   }
  else if (ch == 'k')
   {
    temp = rdr(GT,0.0,'d');
    if (!readerror) kappa = temp;
   }
  else if (ch == 'm')
   {
    temp = rdr(GT,0.0,'d');
    if (!readerror) etamax = temp;
   }
  else if (ch == 't')
   {
    temp = rdr(GE,0.0,'d');
    if (!readerror)
     {
      theta1 = temp;
      theta2 = scale(1.0) - theta1;
     };
   }
  else if (ch == '*' || ch == '\n' || ch == ' ');
  else texterror();
 }
bufferptr = bufferptr - 1;
break;
#endif

case 'e':
temp = rdr(GT,0.0,'e');
if (!readerror) eta = temp;
do ch = readch(); while (ch == ' ');
bufferptr = bufferptr - 1;
if (ch != '\n' && ch != '*')
 {
  temp = rdr(GT,0.0,'r');
  if (!readerror) eta2 = temp;
 }
else eta2 = eta;
break;

case 'f':
while (ch != '\n' && ch != '*')
 {
  ch = readch();
  if (ch == 'B')
   {
    itemp = 0;
    do ch = readch(); while (ch == ' ');
    while (ch >= '1' && ch <= '9')
     {
      bufferptr = bufferptr - 1;
      itemp2 = readint(format[itemp],MAXINT,'f');
      if (readerror) goto endf;
      itemp = itemp + 1;
      if (itemp < MAXFORMAT) format[itemp] = itemp2;
      else pg(stdout,"format too long\n");
      do ch = readch(); while (ch == ' ');
     };
    if (itemp < MAXFORMAT-1)
       for (i=itemp+1;i<=MAXFORMAT-1; i++) format[i] = format[i-1] + 10;
   }
  else if (ch == 'b')
   {
    do ch = readch(); while (ch == ' ');
    if (ch == '+' || ch == '-') ringbell = ch; else texterror();
   }
  else if (ch == 'c')
   {
    do ch = readch(); while (ch == ' ');
    if (ch == '+')
     {
      if (copy != NULL) fflush(copy);
      else copy = fopen("copy","w");
      if (copy == NULL)
       {sprintf(outstr,"cannot open file:  %s\n",outstr); pg(stdout,outstr); };
      copyflag = '+';
     }
    else if (ch == '-')
     {
      copyflag = '-';
      if (copy != NULL)
       {
        fflush(copy);
        fclose(copy);
       }
     }
    else texterror();
   }
  else if (ch == 'd')
   {/* with fd+ I can turn on a piece of debugging code (if it exists) */
    do ch = readch(); while (ch == ' ');
    if (ch == '+' || ch == '-') debugoutput = ch;
    else
     {
      texterror();
      goto endf;
     }
   }
  else if (ch == 'e')
   {
    do ch = readch(); while (ch == ' ');
    if (ch == '+' || ch == '-') echo = ch;
   }
  else if (ch == 'i')
   {
    do ch = readch(); while (ch == ' ');
    if (ch == 'c' || ch == 'r') informat = ch; else texterror();
   }
  else if (ch == 'O')
   {
    do ch = readch(); while (ch == ' ');
    if (ch == '+' || ch == '-') offby1 = ch;
    else texterror();
   }
  else if (ch == 'o')
   {
    do ch = readch(); while (ch == ' ');
    if (ch == 'a' || ch == 'c' || ch == 'r' || ch == 'e' || ch == 's')
       outformat = ch;
    else texterror();
   }
  else if (ch == 'P')
   {
    itemp = readint(0,MAXINT,'f');
    if (!readerror) pagesize = itemp;
   }
  else if (ch == 'p')
   {
    do ch = readch(); while (ch == ' ');
    if (ch == 'c' || ch == 'g') probtype = ch;
    else texterror();
   }
  else if (ch == 'R')
   {
    do ch = readch(); while (ch == ' ');
    if (ch == '+' || ch == '-') runningflag = ch;
    else texterror();
   }
  else if (ch == 's')
   {
    do ch = readch(); while (ch == ' ');
    if (ch == '+' || ch == '-') summary = ch;
    else texterror();
   }
  else if (ch == 'u')
   {
    do ch = readch(); while (ch == ' ');
    if (ch == '+' || ch == '-') up_to_date_stats = ch; else texterror();
   }
  else if (ch == 'x')
   {
    temp = rdr(GT,(REAL) SKIPCODE,'f');
    if (!readerror) unknown = temp;
   }
  else if (ch == ' ' || ch == '*' || ch == '\n');
  else texterror();
 }
bufferptr = bufferptr - 1;
endf:
break;

case 'h': help(); break;

case 'l':
if (nonetwork()) break;
layerno = readint(1,nlayers,'l'); 
if (readerror) break;
p = start;
for (i=2;i<=layerno;i++) p = p->next;
printoutunits(0,p,-1,0.0,(char) 0,(char) 0);
break;

/* make a network */

⌨️ 快捷键说明

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