home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume31 / backprop / part02 < prev    next >
Encoding:
Text File  |  1992-09-01  |  44.1 KB  |  1,704 lines

  1. Newsgroups: comp.sources.misc
  2. From: drt@chinet.chi.il.us (Donald Tveter)
  3. Subject:  v31i130:  backprop - Fast Backpropagation, Part02/04
  4. Message-ID: <1992Sep2.180710.28037@sparky.imd.sterling.com>
  5. X-Md4-Signature: fa2030a933ef7f7a35dbe27f337dbaa4
  6. Date: Wed, 2 Sep 1992 18:07:10 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: drt@chinet.chi.il.us (Donald Tveter)
  10. Posting-number: Volume 31, Issue 130
  11. Archive-name: backprop/part02
  12. Supersedes: backprop: Volume 28, Issue 63-66
  13. Environment: UNIX, DOS
  14.  
  15. #! /bin/sh
  16. # This is a shell archive.  Remove anything before this line, then unpack
  17. # it by saving it into a file and typing "sh file".  To overwrite existing
  18. # files, type "sh file -c".  You can also feed this as standard input via
  19. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  20. # will see the following message at the end:
  21. #        "End of archive 2 (of 4)."
  22. # Contents:  bp.c readme.mak
  23. # Wrapped by drt@chinet on Mon Aug 24 08:58:15 1992
  24. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  25. if test -f 'bp.c' -a "${1}" != "-c" ; then 
  26.   echo shar: Will not clobber existing file \"'bp.c'\"
  27. else
  28. echo shar: Extracting \"'bp.c'\" \(39734 characters\)
  29. sed "s/^X//" >'bp.c' <<'END_OF_FILE'
  30. X/* ************************************************** */
  31. X/* file bp.c:  contains the main program and network  */
  32. X/*             creation routines.                     */
  33. X/*                                                    */
  34. X/* Copyright (c) 1990, 1991, 1992 by Donald R. Tveter */
  35. X/*                                                    */
  36. X/* ************************************************** */
  37. X
  38. X#include <stdio.h>
  39. X#include <signal.h>
  40. X#include <setjmp.h>
  41. X
  42. X#ifdef DOS16
  43. X#include <stdlib.h>
  44. X#include <time.h>
  45. X#endif
  46. X
  47. X#ifdef DOS32
  48. X#include <stdlib.h>
  49. X#include <time.h>
  50. X#endif
  51. X
  52. X#ifdef UNIX
  53. X#include <malloc.h>
  54. X#define SIGINT 2
  55. X#define CLOCKS_PER_SEC 1000000.0
  56. Xextern long clock();
  57. X#endif
  58. X
  59. X#ifdef INTEGER
  60. X#include "ibp.h"
  61. X#else
  62. X#include "rbp.h"
  63. X#endif
  64. X
  65. X/* an addition for large data sets */
  66. X
  67. XINT32 g = 0;
  68. X
  69. X/* built-in C functions */
  70. X
  71. Xextern int rand();
  72. Xextern void srand();
  73. X
  74. X/* functions from io.c */
  75. X
  76. X#ifdef INTEGER
  77. Xextern int scale();
  78. Xextern REAL unscale();
  79. X#endif
  80. X
  81. Xextern void help(), printweights();
  82. Xextern void restoreweights(), saveweights(), texterror();
  83. Xextern WTTYPE rdr();
  84. Xextern int pg(), printoutunits(), readch(), readint();
  85. Xextern REAL readchar();
  86. Xextern char *readstr();
  87. X
  88. X/* functions from misc.c */
  89. X
  90. Xextern void clear(), nullpatterns(), findendofpats(), kick();
  91. Xextern void setonepat(), setoutputpat(), whittle();
  92. Xextern int loadpat(), patcheck(), run();
  93. X
  94. X/* global variables used in all versions */
  95. X
  96. Xchar activation;      /* activation function, p or s */
  97. XWTTYPE alpha;         /* momentum term */
  98. Xchar backprop;        /* flags whether to back propagate error for */
  99. X                      /* units close to their targets */
  100. Xint benchmark;        /* flags benchmarking in progress */
  101. Xint bufferend;        /* index of last character in input line */
  102. Xint bufferptr;        /* position of next character in buffer */
  103. Xchar buffer[buffsize];/* holds contents of one input line */
  104. Xint ch;               /* general purpose character variable */
  105. XFILE *copy;           /* file pointer to copy file */
  106. Xchar copyflag;        /* + for copying, - for no copy */
  107. Xjmp_buf cmdloopstate; /* to save state in case of a SIGINT */
  108. XWTTYPE D;             /* sigmoid sharpness */
  109. XFILE *data;           /* file for original data */
  110. Xchar *datafilename;   /* copy of the data file name saved here */
  111. XWTTYPE dbdeta;        /* the initial eta value for the DBD method */
  112. XWTTYPE decay;         /* the decay parameter for the DBD method */
  113. Xchar deriv;           /* flags type of derivative to use */
  114. Xchar echo;            /* controls echoing of characters during input */
  115. Xchar emptystring;     /* for unused string values */
  116. XREAL errorperunit;    /* average unsquared error on output layer */
  117. XWTTYPE eta;           /* basic learning rate */
  118. XWTTYPE eta2;          /* learning rate for lower layers */
  119. XWTTYPE etamax;        /* the maximum eta for the DBD method */
  120. Xint extraconnect;     /* flags the use of connections between */
  121. X                      /* non-adjacent layers */
  122. XFILE *filestack[4];   /* allows for nested reads from files */
  123. Xint filestackptr;     /* has the index of the current file */
  124. Xint format[maxformat];/* each value in format indicates where to put */
  125. X                      /* a blank for compressed output mode or a */
  126. X                      /* carriage return for real output */
  127. Xint goal;             /* successes desired when benchmarking */
  128. XUNIT *hlayer;         /* pointer to list of units in second layer */
  129. XUNIT *ilayer;         /* pointer to list of units in third layer */
  130. Xchar informat;        /* controls format to read numbers */
  131. XWTTYPE initialkick;   /* the range weights are initialized to */
  132. Xchar *inputfile;      /* name of file to take extra commands from */
  133. Xint iter;             /* for counting iterations in one run */
  134. XUNIT *jlayer;         /* pointer to list of units in fourth layer */
  135. XWTTYPE kappa;         /* the DBD learning parameter */
  136. XUNIT *klayer;         /* pointer to list of units in fifth layer */
  137. XWTTYPE kicksize;      /* range of random weights when benchmarking */
  138. XLAYER *last;          /* has address of the output layer */
  139. Xint lastprint;        /* last iteration pattern responses printed */
  140. Xint lastsave;         /* last time weights were saved */
  141. XINT32 lineno;         /* counts lines for paging */
  142. Xint maxiter;          /* maximum iterations when benchmarking */
  143. Xint maxtries;         /* max networks to try when benchmarking */
  144. XWTTYPE mu;            /* the quickprop acceleration factor */
  145. Xshort nlayers;        /* number of layers in network */
  146. XWTTYPE noise;         /* noise parameter for dbd */
  147. Xint npats;            /* number of patterns currently in use */
  148. Xchar outformat;       /* controls format to print output */
  149. Xchar outstr[OUTSTRSIZE]; /* the output string */
  150. Xint pagesize;         /* size of page for pg */
  151. Xchar patform;         /* flags general or classification pattern format */
  152. XREAL pct_right;       /* % of training patterns correct */
  153. Xint prevnpats;        /* previous number of patterns, initially 0 */
  154. Xint printrate;        /* printrate when benchmarking */
  155. XWTTYPE qmark;         /* value for ? in compressed input */
  156. XWTTYPE qpdecay;       /* the quickprop decay */
  157. XWTTYPE qpeta;         /* the quickprop eta */
  158. XWTTYPE qpnoise;       /* quickprop noise (integer version only) */
  159. Xint qpslope;          /* flags using slope in quickprop all the time */
  160. Xint readerror;        /* flags an error in reading a value */
  161. Xint readingpattern;   /* flags reading pattern state */
  162. Xchar ringbell;        /* flag to ring bell when finished */
  163. Xint right;            /* number of training patterns learned */
  164. Xint saverate;         /* rate at which to save weights */
  165. Xunsigned seed;        /* seed for generating random weights */
  166. XSEEDNODE *seedstart;  /* the list of user defined seeds */
  167. Xshort skiprate;       /* number of times to bypass a learned pattern */
  168. XLAYER *start;         /* has address of the input layer */
  169. Xchar summary;         /* flags summary output mode */
  170. Xint testpat;          /* pattern to skip when benchmarking; else 0 */
  171. Xchar *testfile;       /* file to take test patterns from */
  172. XWTTYPE theta1;        /* the DBD parameter */
  173. XWTTYPE theta2;        /* 1 - theta1 */
  174. XWTTYPE toler;         /* value used in testing for completion */
  175. XWTTYPE toosmall;      /* weights smaller than toosmall were removed */
  176. X#ifdef INTEGER
  177. XINT32 totaldiff;      /* totals errors to find average error per unit */
  178. X#else
  179. XREAL totaldiff;
  180. X#endif
  181. Xint totaliter;        /* counts total iterations for the program */
  182. Xchar *trainfile;      /* file to take training patterns from */
  183. Xint unlearned;        /* number unlearned in last learning cycle */
  184. Xchar update;          /* flags type of update rule to use */
  185. Xchar up_to_date_stats;/* + does an extra forward pass after update */
  186. Xint wrong;            /* number of training patterns unlearned */
  187. Xchar w[8] = "weights";/* the string, weights */
  188. Xchar *wtfile;         /* file to write weights to */
  189. Xchar wtformat;        /* controls format to save and restore weights */
  190. Xchar wtlimithit;      /* flags whether the limit has been hit */
  191. Xint wttotal;          /* total number of weights in use */
  192. X
  193. X/* global variable for the symmetric integer version */
  194. X
  195. X#ifdef SYMMETRIC
  196. XWTTYPE  stdthresh;    /* the standard threshold weight value */
  197. X#endif
  198. X
  199. X/* given a layer no. and unit no. locateunit returns the address */
  200. XUNIT *locateunit(layerno,unitno)
  201. Xint layerno, unitno;
  202. X{int i;
  203. X UNIT *u;
  204. X LAYER *layer;
  205. Xlayer = start;
  206. Xfor(i=1;i<=(layerno-1);i++) layer = layer->next;
  207. Xu = (UNIT *) layer->units;
  208. Xwhile (u != NULL && u->unitnumber != unitno) u = u->next;
  209. Xif (u == NULL)
  210. X  printf("there is no unit %3d in layer %3d\n",unitno,layerno);
  211. Xreturn(u);     
  212. X}
  213. X
  214. X#ifdef SYMMETRIC
  215. X
  216. XINT32 wtaddress(i,j,biasunit,type,size) /* Returns the address of a */
  217. Xint i,j;                                /* weight (1), olddw (2),   */
  218. Xint biasunit;                           /* eta (3), total (4),      */
  219. Xint type;                               /* or slope (5)             */
  220. Xint size;                               /* One is created if it     */
  221. X                                        /* doesn't already exist.   */
  222. X{ int k;
  223. X  INT32 addr;
  224. X  UNIT *u;
  225. X  WTNODE *w;
  226. X
  227. Xif (biasunit) addr = (INT32) malloc(size);
  228. Xelse if (j >= i) addr = (INT32) malloc(size);
  229. Xelse /* the item already exists, so find its address */
  230. X {
  231. X  u = locateunit(2,j);
  232. X  w = (WTNODE *) u->wtlist;
  233. X  k = 1;
  234. X  while (k < i)
  235. X   {
  236. X    w = w->next;
  237. X    k = k + 1;
  238. X   };
  239. X  if (type == 1) addr = (INT32) w->weight;
  240. X  else if (type == 2) addr = (INT32) w->olddw;
  241. X  else if (type == 3) addr = (INT32) w->eta;
  242. X  else if (type == 4) addr = (INT32) w->total;
  243. X  else addr = (INT32) w->slope;
  244. X };
  245. Xreturn(addr);
  246. X}
  247. X
  248. Xvoid setweight(w,i,j,biasunit) /* set initial values in w */
  249. XWTNODE *w;
  250. Xshort i, j;
  251. Xint biasunit;
  252. X{WTTYPE *s;
  253. X
  254. Xs = (WTTYPE *) wtaddress(i,j,biasunit,1,WTSIZE);
  255. X*s = 0;
  256. Xw->weight = s;
  257. Xs = (WTTYPE *) wtaddress(i,j,biasunit,2,WTSIZE);
  258. X*s = 0;
  259. Xw->olddw = s;
  260. Xs = (WTTYPE *) wtaddress(i,j,biasunit,3,WTSIZE);
  261. X*s = eta;
  262. Xw->eta = s;
  263. X#ifdef INTEGER
  264. Xw->total = (INT32 *) wtaddress(i,j,biasunit,4,sizeof(INT32));
  265. X#else
  266. Xw->total = (REAL *) wtaddress(i,j,biasunit,4,sizeof(REAL));
  267. X#endif
  268. X}
  269. X
  270. X#else
  271. X
  272. Xvoid setweight(w,i,j,biasunit) /* set initial values in w */
  273. XWTNODE *w;
  274. Xshort i,j;
  275. Xint biasunit;
  276. X{
  277. Xw->weight = 0;
  278. Xw->olddw = 0;
  279. Xw->slope = 0;
  280. Xw->eta = dbdeta;
  281. X}
  282. X
  283. X#endif
  284. X
  285. XLAYER *mklayer(prevlayer,n)  /* creates a layer of n units, pointers */
  286. XLAYER *prevlayer;            /* and weights back to the units in the */
  287. Xint n;                       /* previous layer and links this new */
  288. X                             /* layer into the list of layers */
  289. X{UNIT *front, *p, *q, *bias, *prev, *ptr;
  290. X WTNODE *wfront, *wprev, *w;
  291. X LAYER *lptr;
  292. X int i, j, count;
  293. X
  294. X/* make a list of nodes in this layer */
  295. X
  296. Xcount = 1;
  297. Xfront = (UNIT *) malloc(sizeof(UNIT));
  298. Xfront->unitnumber = count;
  299. Xfront->layernumber = nlayers;
  300. Xprev = front;
  301. Xfor(i=1;i<n;i++)
  302. X {
  303. X  count = count + 1;
  304. X  ptr = (UNIT *) malloc(sizeof(UNIT));
  305. X  prev->next = ptr;
  306. X  ptr->unitnumber = count;
  307. X  ptr->layernumber = nlayers;
  308. X  prev = ptr;
  309. X };
  310. Xprev->next = NULL;
  311. X
  312. X/* make a LAYER node to point to this list of units */
  313. X
  314. Xlptr = (LAYER *) malloc(sizeof(LAYER));
  315. Xlptr->unitcount = n;
  316. Xlptr->patstart = NULL;
  317. Xlptr->currentpat = NULL;
  318. Xlptr->backlayer = prevlayer;
  319. Xlptr->next = NULL;
  320. X(UNIT *) lptr->units = front;   /* connect the list of units */
  321. X
  322. X/* return if this is the input layer */
  323. X
  324. Xif (prevlayer == NULL) return(lptr);
  325. Xprevlayer->next = lptr;
  326. X
  327. X/* If we are working on a deeper layer, for every node in this layer, */
  328. X/* create a linked list back to units in the previous layer. */
  329. X
  330. Xi = 1;
  331. Xq = front;
  332. Xwhile (q != NULL) /* do a unit */
  333. X {    
  334. X  j = 1;            /* handle first connection */
  335. X  p = (UNIT *) prevlayer->units;
  336. X  wfront = (WTNODE *) malloc(sizeof(WTNODE));
  337. X  wttotal = wttotal + 1;
  338. X  (WTNODE *) q->wtlist = wfront;
  339. X  wprev = wfront;
  340. X  (UNIT *) wfront->backunit = p;
  341. X  setweight(wfront,i,j,0);
  342. X  p = p->next;
  343. X  while (p != NULL) /* handle rest of connections */
  344. X   {
  345. X    j = j + 1;
  346. X    w = (WTNODE *) malloc(sizeof(WTNODE));
  347. X    wttotal = wttotal + 1;
  348. X    wprev->next = w;
  349. X    (UNIT *) w->backunit = p;
  350. X    setweight(w,i,j,0);
  351. X    wprev = w;
  352. X    p = p->next;
  353. X   };
  354. X  j = j + 1;
  355. X  bias = (UNIT *) malloc(sizeof(UNIT));   /* create a bias unit */
  356. X  bias->oj = scale(1.0);
  357. X  bias->layernumber = nlayers;
  358. X  bias->unitnumber = 32767;           /* bias unit is unit 32767 */
  359. X  w = (WTNODE *) malloc(sizeof(WTNODE)); /* connect to end of list */
  360. X  wttotal = wttotal + 1;
  361. X  wprev->next = w;
  362. X  (UNIT *) w->backunit = bias;
  363. X  setweight(w,n+2,i,1);
  364. X  w->next = NULL;
  365. X  q = q->next;
  366. X  i = i + 1;
  367. X };
  368. Xreturn(lptr);
  369. X}
  370. X
  371. X#ifndef SYMMETRIC
  372. X
  373. Xvoid connect(a,b,range)  /* add a connection from unit a to unit b */
  374. XUNIT *a, *b;             /* connections go in increasing order */
  375. XWTTYPE range;
  376. X
  377. X{WTNODE *wnew, *w, *wprev;
  378. X UNIT *wunit;
  379. X int farenough;
  380. X
  381. Xwnew = (WTNODE *) malloc(sizeof(WTNODE));
  382. Xwttotal = wttotal + 1;
  383. Xwnew->eta = dbdeta;
  384. Xwnew->weight = range * (rand() & 32767) / 32768;
  385. Xif ((rand() & 32767) > 16383) wnew->weight = -wnew->weight;
  386. Xwnew->olddw = 0;
  387. Xwnew->slope = 0;
  388. X(UNIT *) wnew->backunit = a;
  389. Xw = (WTNODE *) b->wtlist;
  390. Xwprev = NULL;
  391. Xwunit = (UNIT *) w->backunit;
  392. Xfarenough = 0;                  /* insert the weight in order */
  393. Xwhile (w != NULL && !farenough)
  394. X if (wunit->layernumber > a->layernumber) farenough = 1;
  395. X else if (wunit->layernumber == a->layernumber)
  396. X  while (w != NULL && !farenough)
  397. X   {
  398. X    if (wunit->unitnumber < a->unitnumber &&
  399. X        wunit->layernumber == a->layernumber)
  400. X     {
  401. X      wprev = w;
  402. X      w = w->next;
  403. X      wunit = (UNIT *) w->backunit;
  404. X     }
  405. X    else farenough = 1;
  406. X   }
  407. X  else
  408. X   {
  409. X    wprev = w;
  410. X    w = w->next;
  411. X    wunit = (UNIT *) w->backunit;
  412. X   };
  413. Xif (wprev == NULL)
  414. X {
  415. X  wnew->next = w;
  416. X  (WTNODE *) b->wtlist = wnew;
  417. X }
  418. Xelse
  419. X {
  420. X  wnew->next = w;
  421. X  wprev->next = wnew;
  422. X };
  423. X}
  424. X
  425. Xvoid addhiddenunit(layerno,range)
  426. Xint layerno;  /* add hidden unit to end of the layer */
  427. XWTTYPE range;
  428. X{
  429. X LAYER *lptr, *prevlayer, *nextlayer;
  430. X UNIT *u, *prevu, *p, *bias;
  431. X WTNODE *wnode;
  432. X int i, unitno;
  433. X
  434. Xlptr = start;
  435. Xfor (i=1;i <= (layerno - 1); i++) lptr = lptr->next;
  436. Xunitno = lptr->unitcount;
  437. Xlptr->unitcount = unitno + 1;
  438. Xprevu = locateunit(layerno,unitno);
  439. Xif (prevu == NULL) return;
  440. Xu = (UNIT *) malloc(sizeof(UNIT));
  441. Xprevu->next = u;
  442. Xu->next = NULL;
  443. Xu->unitnumber = unitno + 1;
  444. Xu->layernumber = layerno;
  445. Xbias = (UNIT *) malloc(sizeof(UNIT));
  446. Xbias->oj = scale(1.0);
  447. Xbias->layernumber = layerno;
  448. Xbias->unitnumber = 32767;           /* bias unit is unit 32767 */
  449. Xwnode = (WTNODE *) malloc(sizeof(WTNODE));
  450. Xwttotal = wttotal + 1;
  451. Xwnode->weight = range * (rand() & 32767) / 32768;
  452. Xif ((rand() & 32767) > 16383) wnode->weight = -wnode->weight;
  453. Xwnode->olddw = 0;
  454. Xwnode->slope = 0;
  455. Xwnode->eta = dbdeta;
  456. Xwnode->next = NULL;
  457. X(UNIT *) wnode->backunit = bias;
  458. X(WTNODE *) u->wtlist = wnode;
  459. Xprevlayer = lptr->backlayer;
  460. Xp = (UNIT *) prevlayer->units;
  461. Xwhile (p != NULL)
  462. X {
  463. X  connect(p,u,range);
  464. X  p = p->next;
  465. X };
  466. Xnextlayer = lptr->next;
  467. Xp = (UNIT *) nextlayer->units;
  468. Xwhile (p != NULL)
  469. X {
  470. X  connect(u,p,range);
  471. X  p = p->next;
  472. X };
  473. X}      
  474. X
  475. X#endif
  476. X
  477. Xvoid readpatson(layer,command)
  478. XLAYER *layer;
  479. Xint command;
  480. X
  481. X{PATLIST *pl;
  482. X int i, answer, veclength;
  483. X WTTYPE *val;
  484. X
  485. Xpl = (PATLIST *) malloc(sizeof(PATLIST));
  486. Xpl->next = NULL;
  487. Xpl->bypass = 0;      /* number of times to bypass this pattern */
  488. Xpl->pats = NULL;     /* no patterns read yet */
  489. Xif (layer->patstart == NULL) (PATLIST *) layer->patstart = pl;
  490. Xelse layer->currentpat->next = pl;
  491. Xlayer->currentpat = pl;
  492. X
  493. Xif (layer == last && (patform == 'c' || patform == 'C'))
  494. X {
  495. X  answer = readint(1,last->unitcount,command);
  496. X  if (readerror) return;
  497. X  val = (WTTYPE *) malloc(sizeof(WTTYPE));
  498. X  *val = answer;
  499. X  pl->pats = val;
  500. X  return;
  501. X };
  502. Xveclength = layer->unitcount;
  503. Xval = (WTTYPE *) malloc(veclength * sizeof(WTTYPE));
  504. Xpl->pats = val;
  505. Xfor (i=1;i<=veclength;i++)
  506. X {
  507. X  if (informat == 'r') *val++ = rdr(GE,(REAL) HCODE,command);
  508. X  else *val++ = scale(readchar());
  509. X  if (readerror)
  510. X   {
  511. X    if (readerror == 2 && i == 1) readerror = 2; else readerror = 1;
  512. X    return;
  513. X   };
  514. X };
  515. Xreturn;
  516. X}
  517. X
  518. Xint readpats(number,command)
  519. Xint number, command;
  520. X{ int i, j;
  521. X  PATLIST *pl;
  522. X
  523. Xfor (i=1;i<=number;i++)
  524. X {
  525. X  readpatson(start,command);
  526. X  if (readerror == 1) goto failure;
  527. X  if (readerror == 2) /* EOF */ break;
  528. X  readpatson(last,command);
  529. X  if (readerror) goto failure;
  530. X };
  531. Xif (readerror == 0) return(number);
  532. Xelse if (readerror == 2)
  533. X {
  534. X  popfile();
  535. X  return(i-1);
  536. X };
  537. X
  538. Xfailure:
  539. Xprintf("error while reading pattern %d\n",i);
  540. Xpopfile();
  541. Xpl = (PATLIST *) start->patstart;
  542. Xfor (j=1;j<=prevnpats + i - 2;j++) pl = pl->next;
  543. Xpl->next = NULL;
  544. Xpl = (PATLIST *) last->patstart;
  545. Xfor (j=1;j<=prevnpats + i - 2;j++) pl = pl->next;
  546. Xpl->next = NULL;
  547. Xreturn(i-1);
  548. X}
  549. X
  550. Xvoid init()
  551. X{int i;
  552. X
  553. Xactivation = 'p';
  554. Xalpha = scale(0.5);
  555. Xbackprop = 1;
  556. Xbenchmark = 0;
  557. Xbufferend = 0;
  558. Xbufferptr = buffsize + 1;
  559. Xch = ' ';
  560. Xcopyflag = '-';            /* default is to not make a copy */
  561. XD = scale(1.0);
  562. Xdbdeta = scale(0.5);
  563. Xdecay = scale(0.5);
  564. Xderiv = 'd';
  565. Xecho = '-';
  566. Xeta = scale(0.5);
  567. Xeta2 = eta;
  568. Xetamax = scale(30.0);
  569. Xextraconnect = 0;
  570. Xformat[0] = 0;
  571. Xfor(i=1;i<=maxformat-1;i++) format[i] = format[i-1] + 10;
  572. Xgoal = 10;
  573. Xinformat = 'c';
  574. Xinitialkick = -1;
  575. Xkappa = scale(0.5);
  576. Xkicksize = scale(1.0);
  577. Xlastprint = 0;
  578. Xlastsave = 0;
  579. Xmaxiter = 1000;
  580. Xmaxtries = 10;
  581. Xmu = scale(1.75);
  582. Xnoise = 0;
  583. Xoutformat = 'r';
  584. Xpagesize = 24;
  585. Xpatform = 'g';
  586. Xpct_right = 0.0;
  587. Xprevnpats = 0;
  588. Xqmark = scale(0.5);
  589. Xqpdecay = scale(0.1);
  590. Xqpeta = scale(0.5);
  591. Xqpnoise = 0;
  592. Xqpslope = '+';
  593. Xright = 0;
  594. Xringbell = '-';
  595. Xskiprate = 0;
  596. Xtestpat = 0;
  597. Xsaverate = MAXINT;
  598. Xseedstart = (SEEDNODE *) malloc(sizeof(SEEDNODE));
  599. Xseedstart->val = 0;
  600. Xseedstart->next = NULL;
  601. X#ifdef SYMMETRIC
  602. Xstdthresh = -32768;      /* indicates no threshold set */
  603. X#endif
  604. Xsummary = '+';
  605. Xtheta1 = scale(0.5);
  606. Xtheta2 = scale(1.0) - theta1;
  607. Xtoler = scale(0.1);
  608. Xtoosmall = -1;           /* indicates no weights whittled away */
  609. Xtotaliter = 0;
  610. Xupdate = 'p';
  611. Xup_to_date_stats = '-';
  612. Xwrong = 0;
  613. Xwtfile = &w[0];
  614. Xwtformat = 'r';
  615. Xwtlimithit = 0;
  616. Xwttotal = 0;
  617. X}
  618. X
  619. Xint nonetwork()
  620. X{
  621. Xif (start != NULL) return(0);
  622. Xpg("there is no network\n");
  623. Xreturn(1);
  624. X}
  625. X
  626. Xint nopatterns()
  627. X{
  628. Xif (npats != 0) return(0);
  629. Xpg("there are no patterns\n");
  630. Xreturn(1);
  631. X}
  632. X
  633. X/* for a SIGINT, restart in cmdloop */
  634. X#ifdef UNIX
  635. Xvoid restartcmdloop()
  636. X#else
  637. Xvoid restartcmdloop(int dummy)
  638. X#endif
  639. X{
  640. Xwhile (data != stdin) popfile();
  641. Xbenchmark = 0;
  642. Xsignal(SIGINT,restartcmdloop);
  643. Xlongjmp(cmdloopstate,1);
  644. X}
  645. Xvoid cmdloop()    /* read commands and process them */
  646. X{
  647. Xint finished, layerno, unitno, layer1, layer2, node1, node2;
  648. Xint i, itemp, itemp2, successes, tries, sumiter, dobenchmark;
  649. XINT32 itemp32;
  650. XWTTYPE temp, temp2;
  651. XREAL averageiter, averagecpu;
  652. XLAYER *p;
  653. XUNIT *u, *n1, *n2;
  654. Xchar string[81];
  655. XWTNODE *w;
  656. XSEEDNODE *s, *sprev;
  657. Xlong cputime, prevcputime, totalcpu;
  658. X
  659. Xsetjmp(cmdloopstate); /* restart here from SIGINT */
  660. Xfinished = 0;
  661. Xdo{
  662. X#ifdef SYMMETRIC
  663. X   if (data == stdin) pg("[?!*AaBbCdefhiklmnOoPpQqRrSsTtWwx]? ");
  664. X#else
  665. X   if (data == stdin) pg("[?!*AaBbCcdefHhiklmnOoPpQqRrSstWwx]? ");
  666. X#endif
  667. X
  668. X   while(ch == ' ' || ch == '\n') ch = readch();
  669. X   lineno = 0;
  670. X   switch (ch) {
  671. X
  672. Xcase EOF:
  673. Xpopfile();
  674. Xif (data == stdin) pg("taking commands from stdin now\n");
  675. Xbreak;
  676. X
  677. Xcase '?':
  678. Xsprintf(outstr,"\n%d iterations, s %1d  ",totaliter,seed); pg(outstr);
  679. Xsprintf(outstr,"k 0 %5.3f,  ",unscale(initialkick)); pg(outstr);
  680. Xsprintf(outstr,"data file = %s\n",datafilename); pg(outstr);
  681. Xsprintf(outstr,"testing file = %s\n",testfile); pg(outstr);
  682. Xsprintf(outstr,"training file = %s\n",trainfile); pg(outstr);
  683. Xsprintf(outstr,"input file = %s\n",inputfile); pg(outstr);
  684. Xsprintf(outstr,"weight file = %s\n",wtfile); pg(outstr);
  685. Xsprintf(outstr,"Algorithm: a%c",activation); pg(outstr);
  686. Xif (backprop) pg(" b+"); else pg(" b-");
  687. Xsprintf(outstr," D%5.2f d%c g %d ",unscale(D),deriv,g); pg(outstr);
  688. Xsprintf(outstr,"s%1d t%1d u%c\n",skiprate,testpat,update); pg(outstr);
  689. Xsprintf(outstr,"e %7.5f %7.5f",unscale(eta),unscale(eta2)); pg(outstr);
  690. Xsprintf(outstr," --- a %7.5f\n",unscale(alpha)); pg(outstr);
  691. Xsprintf(outstr,"d d %8.5f e %8.5f ",unscale(decay),unscale(dbdeta));
  692. Xpg(outstr);
  693. Xsprintf(outstr,"k %8.5f m %8.5f ",unscale(kappa),unscale(etamax));
  694. Xpg(outstr);
  695. Xsprintf(outstr,"n %8.5f t %8.5f\n",unscale(noise),unscale(theta1));
  696. Xpg(outstr);
  697. Xsprintf(outstr,"qp d %8.5f e %8.5f ",unscale(qpdecay),unscale(qpeta));
  698. Xpg(outstr);
  699. Xsprintf(outstr,"m %8.5f n %8.5f s%c\n",unscale(mu),unscale(qpnoise),qpslope);
  700. Xpg(outstr);
  701. Xsprintf(outstr,"tolerance = %4.2f\n",unscale(toler)); pg(outstr);
  702. Xsprintf(outstr,"f b%c c%c e%c i%c",ringbell,copyflag,echo,informat);
  703. Xpg(outstr);
  704. Xsprintf(outstr," o%c P %d p%c s%c ",outformat,pagesize,patform,summary);
  705. Xpg(outstr);
  706. Xsprintf(outstr,"u%c w%c\n",up_to_date_stats,wtformat); pg(outstr);
  707. Xpg("format breaks after: ");
  708. Xfor (i=1;i<=10;i++) {sprintf(outstr,"%4d",format[i]); pg(outstr);};
  709. Xpg("\n                     ");
  710. Xfor (i=11;i<=maxformat-1;i++)
  711. X {sprintf(outstr,"%4d",format[i]); pg(outstr);};
  712. Xsprintf(outstr,"\nlast time weights were saved: %d\n",lastsave);
  713. Xpg(outstr);
  714. Xsprintf(outstr,"saving weights every %d iterations\n",saverate);
  715. Xpg(outstr);
  716. Xif (wtlimithit) pg(">>>>> WEIGHT LIMIT HIT <<<<<\n");
  717. Xpg("network size: ");
  718. Xp = start;
  719. Xwhile (p != NULL)
  720. X {
  721. X  sprintf(outstr," %1d",p->unitcount);
  722. X  pg(outstr);
  723. X  p = p->next;
  724. X };
  725. Xif (extraconnect) pg(" with extra connections");
  726. Xsprintf(outstr," (total:  %1d weights)\n",wttotal); pg(outstr);
  727. Xif (toosmall != -1)
  728. X {
  729. X  pg("removed non-bias weights with absolute ");
  730. X  sprintf(outstr,"value below  %4.2f\n",unscale(toosmall)); pg(outstr);
  731. X };
  732. X#ifdef SYMMETRIC
  733. Xif (stdthresh != -32768)
  734. X {
  735. X  sprintf(outstr,"thresholds frozen at %f\n", unscale(stdthresh));
  736. X  pg(outstr);
  737. X };
  738. X#endif
  739. Xsprintf(outstr,"%d patterns   %5.2f%%  right ",npats,pct_right);
  740. Xpg(outstr);
  741. Xsprintf(outstr,"(%d right,  %d wrong)  ",right,wrong); pg(outstr);
  742. Xsprintf(outstr,"%7.5f error/unit\n",errorperunit); pg(outstr);
  743. Xsprintf(outstr,"? = %f\n",unscale(qmark)); pg(outstr);
  744. Xsprintf(outstr,"benchmark parameters: g %d ",goal); pg(outstr);
  745. Xsprintf(outstr,"k %4.2f ",unscale(kicksize)); pg(outstr);
  746. Xsprintf(outstr,"m %d r %d ",maxtries,maxiter); pg(outstr);
  747. Xif (printrate != -1) {sprintf(outstr,"%d ",printrate); pg(outstr);};
  748. Xif (*testfile != emptystring)
  749. X {sprintf(outstr,"t %s\n",testfile); pg(outstr);}
  750. Xelse {sprintf(outstr,"t %d\n",testpat); pg(outstr);};
  751. Xpg("for help, type h followed by the letter of the command\n\n");
  752. Xbreak;
  753. X
  754. Xcase '!':
  755. Xi = 0;
  756. Xch = readch();
  757. Xwhile (ch != '\n' && i <= 80)
  758. X {
  759. X  string[i] = ch;
  760. X  ch = readch();
  761. X  i = i + 1;
  762. X };
  763. Xbufferptr = bufferptr - 1;
  764. Xstring[i] = '\0';
  765. Xsystem(string);
  766. Xbreak;
  767. X
  768. Xcase '*': break;  /* * on a line is a comment */
  769. X
  770. Xcase 'A':
  771. Xwhile (ch != '\n' && ch != '*')
  772. X {
  773. X  ch = readch();
  774. X  if (ch == 'a')
  775. X   {
  776. X    do ch = readch(); while (ch == ' ');
  777. X    if (ch == 'p' || ch == 'l' || ch == 't') activation = ch;
  778. X#ifndef INTEGER
  779. X    else if (ch == 's' || ch == 'T') activation = ch;
  780. X#endif
  781. X    else texterror();
  782. X   }
  783. X  else if (ch == 'b')
  784. X   {
  785. X    do ch = readch(); while (ch == ' ');
  786. X    if (ch == '+') backprop = 1;
  787. X    else if (ch == '-') backprop = 0;
  788. X    else texterror();
  789. X   }
  790. X  else if (ch == 'D')
  791. X   {
  792. X    temp = rdr(GT,0.0,'A');
  793. X    if (!readerror) D = temp;
  794. X   }
  795. X  else if (ch == 'd')
  796. X   {
  797. X    do ch = readch(); while (ch == ' ');
  798. X    if (ch == 'd' || ch == 'f' || ch == 'o' || ch == 'F') deriv = ch;
  799. X    else texterror();
  800. X   }
  801. X  else if (ch == 'g')
  802. X   {
  803. X    itemp32 = readint(0,MAXINT,'f');
  804. X    if (!readerror) g = itemp32;
  805. X   }
  806. X  else if (ch == 's')
  807. X   {
  808. X    itemp = readint(0,32767,'A');
  809. X    if (!readerror) skiprate = itemp;
  810. X   }
  811. X  else if (ch == 't')
  812. X   {
  813. X    itemp = readint(0,npats,'A');
  814. X    if (!readerror) testpat = itemp;
  815. X    resetpats();
  816. X    for (i=1;i<=npats;i++)
  817. X     {
  818. X      nextpat();
  819. X      if (last->currentpat->bypass < 0) last->currentpat->bypass = 0;
  820. X      else if (i == testpat) last->currentpat->bypass = -1;
  821. X     };
  822. X   }
  823. X  else if (ch == 'u')
  824. X   {
  825. X    do ch = readch(); while (ch == ' ');
  826. X    if (ch == 'j') ch = 'd';
  827. X#ifdef SYMMETRIC
  828. X    if (ch == 'c' || ch == 'p') update = ch;
  829. X#else
  830. X    if (ch == 'c' || ch == 'p' || ch == 'd' || ch == 'q' || ch == 's')
  831. X     update = ch;
  832. X#endif
  833. X    else texterror();
  834. X   }
  835. X  else if (ch == '*' || ch == '\n' || ch == ' ');
  836. X  else texterror();
  837. X }
  838. Xbufferptr = bufferptr - 1;
  839. Xbreak;
  840. X
  841. Xcase 'a':
  842. Xtemp = rdr(GE,0.0,'a');
  843. Xif (!readerror) alpha = temp;
  844. Xbreak;
  845. X
  846. Xcase 'B':
  847. Xdobenchmark = 0;
  848. Xwhile (ch != '\n' && ch != '*')
  849. X {
  850. X  do ch = readch(); while (ch == ' ');
  851. X  if (ch == 'g')
  852. X   {
  853. X    itemp2 = readint(0,MAXINT,'B');
  854. X    if (!readerror) goal = itemp2;
  855. X   }
  856. X  else if (ch == 'k')
  857. X   {
  858. X    temp = rdr(GT,0.0,'B');
  859. X    if (!readerror) kicksize = temp;
  860. X   }
  861. X  else if (ch == 'm')
  862. X   {
  863. X    itemp2 = readint(0,MAXINT,'B');
  864. X    if (!readerror) maxtries = itemp2;
  865. X   }
  866. X  else if (ch == 'r')
  867. X   {
  868. X    if (nonetwork() || nopatterns()) goto endB;
  869. X    maxiter = readint(1,MAXINT,'B');
  870. X    if (readerror) goto endB;
  871. X    do ch = readch(); while (ch == ' ');
  872. X    bufferptr = bufferptr - 1;
  873. X    if (ch >= '0' && ch <= '9')
  874. X     {
  875. X      printrate = readint(1,MAXINT,'B');
  876. X      if (readerror) goto endB;
  877. X     }
  878. X    else printrate = -1;
  879. X    dobenchmark = 1;
  880. X   }
  881. X  else if (ch == 't')
  882. X   {
  883. X    do ch = readch(); while (ch == ' ');
  884. X    if (ch == 'f') testfile = readstr();
  885. X    else if (nonetwork() || nopatterns()) goto endB;
  886. X    else
  887. X     {
  888. X      bufferptr = bufferptr - 1;
  889. X      itemp = readint(0,npats,'B');
  890. X      if (readerror) goto endB;
  891. X      testpat = itemp;
  892. X      if (testpat == 0) testfile = &emptystring;
  893. X     };
  894. X   }
  895. X  else if (ch == ' ' || ch == '*' || ch == '\n');
  896. X  else texterror();
  897. X };
  898. Xbufferptr = bufferptr - 1;
  899. Xif (dobenchmark)
  900. X {
  901. X  if (testpat)
  902. X   {
  903. X    sprintf(outstr,"testing pattern %d\n",testpat);
  904. X    if (pg(outstr)) goto endB;
  905. X   };
  906. X  benchmark = 1;
  907. X  tries = 0;
  908. X  sumiter = 0;
  909. X  successes = 0;
  910. X  totalcpu = 0;
  911. X  s = seedstart->next;
  912. X  while (successes < goal && tries < maxtries)
  913. X   {
  914. X    if (s != NULL)
  915. X     {
  916. X      seed = s->val;
  917. X      srand(seed);
  918. X      s = s->next;
  919. X     };
  920. X    clear();
  921. X    kick(scale(0.0),kicksize);
  922. X    sprintf(outstr," seed = %6d; ",seed);
  923. X    pg(outstr);
  924. X    if (testpat)
  925. X     {
  926. X      resetpats();
  927. X      for (i=1;i<=testpat;i++) nextpat();
  928. X      last->currentpat->bypass = -1;
  929. X     };
  930. X    prevcputime = clock();
  931. X    if (run(maxiter,printrate)) goto endB;
  932. X    cputime = clock();
  933. X    tries = tries + 1;;
  934. X    if (unlearned == 0 || ((unlearned == 1) && testpat))
  935. X     {
  936. X      successes = successes + 1;
  937. X      sumiter = sumiter + totaliter;
  938. X      totalcpu = totalcpu + cputime - prevcputime;
  939. X     };
  940. X    if (testpat)
  941. X     {
  942. X      for (i=1;i<(testpat-1);i++) setonepat();
  943. X      last->currentpat->bypass = 0;
  944. X     }
  945. X   };
  946. X  sprintf(outstr,"%d failures; %d successes;",tries - successes,successes);
  947. X  pg(outstr);
  948. X  if (successes > 0)
  949. X   {
  950. X    averageiter = (REAL) sumiter / (REAL) successes;
  951. X    averagecpu = (REAL) totalcpu / (REAL) successes;
  952. X    sprintf(outstr," average = %f ",averageiter); pg(outstr);
  953. X    sprintf(outstr,"%12.6f sec/network",averagecpu / CLOCKS_PER_SEC);
  954. X    pg(outstr);
  955. X   };
  956. X  pg("\n");
  957. X };
  958. X
  959. XendB: benchmark = 0;
  960. Xbreak;
  961. X
  962. Xcase 'b':
  963. Xitemp = 0;
  964. Xch = readch();
  965. Xwhile (ch != '\n' && ch != '*')
  966. X {
  967. X  bufferptr = bufferptr - 1;
  968. X  itemp2 = readint(format[itemp],last->unitcount,'b');
  969. X  if (readerror) goto endb;
  970. X  itemp = itemp + 1;
  971. X  if (itemp < maxformat) format[itemp] = itemp2;
  972. X  else pg("format too long\n");
  973. X  ch = readch();
  974. X  while (ch == ' ') ch = readch();
  975. X  if (ch >= '0' && ch <= '9') bufferptr = bufferptr - 1;
  976. X };
  977. Xif (itemp < maxformat-1)
  978. X   for (i=itemp+1;i <= maxformat-1; i++) format[i] = format[i-1] + 10;
  979. Xbufferptr = bufferptr - 1;
  980. X
  981. Xendb: break;
  982. X
  983. Xcase 'C':
  984. Xif (nonetwork()) break;
  985. Xclear();
  986. Xsrand(seed);
  987. Xbreak;
  988. X
  989. X#ifndef SYMMETRIC
  990. Xcase 'c':
  991. Xif (nonetwork()) break;
  992. Xlayer1 = readint(1,nlayers,'c');
  993. Xif (readerror) break;
  994. Xnode1 = readint(1,MAXINT,'c');
  995. Xif (readerror) break;
  996. Xlayer2 = readint(1,nlayers,'c');
  997. Xif (readerror) break;
  998. Xnode2 = readint(1,MAXINT,'c');
  999. Xif (readerror) break;
  1000. Xif (layer1 >= layer2)
  1001. X {
  1002. X  pg("backward connections in c command not implemented\n");
  1003. X  break;
  1004. X };
  1005. Xn1 = locateunit(layer1,node1);
  1006. Xn2 = locateunit(layer2,node2);
  1007. Xif (n1 != NULL && n2 != NULL)
  1008. X {
  1009. X  connect(n1,n2,0);
  1010. X  extraconnect = 1;
  1011. X }
  1012. Xelse
  1013. X {
  1014. X  sprintf(outstr,"connection not made: %d %d %d %d\n", layer1, node1, layer2, node2);
  1015. X  pg(outstr);
  1016. X };
  1017. Xbreak;
  1018. X#endif
  1019. X
  1020. Xcase 'd':
  1021. Xcase 'j':
  1022. Xwhile (ch != '\n' && ch != '*')
  1023. X {
  1024. X  ch = readch();
  1025. X  if (ch == 'd')
  1026. X   {
  1027. X    temp = rdr(GT,0.0,'d');
  1028. X    if (!readerror) decay = temp;
  1029. X   }
  1030. X  else if (ch == 'e')
  1031. X   {
  1032. X    temp = rdr(GT,0.0,'d');
  1033. X    if (!readerror && !nonetwork())
  1034. X     {
  1035. X      dbdeta = temp;
  1036. X      p = start->next;
  1037. X      while (p != NULL)
  1038. X       {
  1039. X        u = (UNIT *) p->units;
  1040. X        while (u != NULL)
  1041. X         {
  1042. X          w = (WTNODE *) u->wtlist;
  1043. X          while (w != NULL)
  1044. X           {
  1045. X            w->eta = dbdeta;
  1046. X            w = w->next;
  1047. X           }
  1048. X          u = u->next;
  1049. X         }
  1050. X        p = p->next;
  1051. X       }
  1052. X     }
  1053. X   }
  1054. X  else if (ch == 'k')
  1055. X   {
  1056. X    temp = rdr(GT,0.0,'d');
  1057. X    if (!readerror) kappa = temp;
  1058. X   }
  1059. X  else if (ch == 'm')
  1060. X   {
  1061. X    temp = rdr(GT,0.0,'d');
  1062. X    if (!readerror) etamax = temp;
  1063. X   }
  1064. X  else if (ch == 'n')
  1065. X   {
  1066. X    temp = rdr(GE,0.0,'d');
  1067. X    if (!readerror) noise = temp;
  1068. X   }
  1069. X  else if (ch == 't')
  1070. X   {
  1071. X    temp = rdr(GE,0.0,'d');
  1072. X    if (!readerror)
  1073. X     {
  1074. X      theta1 = temp;
  1075. X      theta2 = scale(1.0) - theta1;
  1076. X     };
  1077. X   }
  1078. X  else if (ch == '*' || ch == '\n' || ch == ' ');
  1079. X  else texterror();
  1080. X }
  1081. Xbufferptr = bufferptr - 1;
  1082. Xbreak;
  1083. X
  1084. Xcase 'e':
  1085. Xtemp = rdr(GT,0.0,'e');
  1086. Xif (!readerror) eta = temp;
  1087. Xdo ch = readch(); while (ch == ' ');
  1088. Xbufferptr = bufferptr - 1;
  1089. Xif (ch != '\n' && ch != '*')
  1090. X {
  1091. X  temp = rdr(GT,0.0,'r');
  1092. X  if (!readerror) eta2 = temp;
  1093. X }
  1094. Xelse eta2 = eta;
  1095. Xbreak;
  1096. X
  1097. Xcase 'f':
  1098. Xwhile (ch != '\n' && ch != '*')
  1099. X {
  1100. X  ch = readch();
  1101. X  if (ch == 'b')
  1102. X   {
  1103. X    do ch = readch(); while (ch == ' ');
  1104. X    if (ch == '+' || ch == '-') ringbell = ch; else texterror();
  1105. X   }
  1106. X  else if (ch == 'c')
  1107. X   {
  1108. X    do ch = readch(); while (ch == ' ');
  1109. X    if (ch == '+')
  1110. X     {
  1111. X       copyflag = '+';
  1112. X       copy = fopen("copy","w");
  1113. X       if (copy == NULL)
  1114. X        {sprintf(outstr,"cannot open file:  copy\n"); pg(outstr); };
  1115. X     }
  1116. X    else if (ch == '-')
  1117. X     {
  1118. X      copyflag = '-';
  1119. X      if (copy != NULL)
  1120. X       {
  1121. X        fflush(copy);
  1122. X        fclose(copy);
  1123. X       }
  1124. X     }
  1125. X    else texterror();
  1126. X   }
  1127. X  else if (ch == 'e')
  1128. X   {
  1129. X    do ch = readch(); while (ch == ' ');
  1130. X    if (ch == '+' || ch == '-') echo = ch;
  1131. X   }
  1132. X  else if (ch == 'i')
  1133. X   {
  1134. X    do ch = readch(); while (ch == ' ');
  1135. X    if (ch == 'c' || ch == 'r') informat = ch; else texterror();
  1136. X   }
  1137. X  else if (ch == 'o')
  1138. X   {
  1139. X    do ch = readch(); while (ch == ' ');
  1140. X    if (ch == 'a' || ch == 'c' || ch == 'r') outformat = ch;
  1141. X    else texterror();
  1142. X   }
  1143. X  else if (ch == 'P')
  1144. X   {
  1145. X    itemp = readint(0,MAXINT,'f');
  1146. X    if (!readerror) pagesize = itemp;
  1147. X   }
  1148. X  else if (ch == 'p')
  1149. X   {
  1150. X    do ch = readch(); while (ch == ' ');
  1151. X    if (ch == 'c' || ch == 'C' || ch == 'g' || ch == 'G') patform = ch;
  1152. X    else texterror();
  1153. X   }
  1154. X  else if (ch == 's')
  1155. X   {
  1156. X    do ch = readch(); while (ch == ' ');
  1157. X    if (ch == '+' || ch == '-') summary = ch; else texterror();
  1158. X   }
  1159. X  else if (ch == 'u')
  1160. X   {
  1161. X    do ch = readch(); while (ch == ' ');
  1162. X    if (ch == '+' || ch == '-') up_to_date_stats = ch; else texterror();
  1163. X   }
  1164. X  else if (ch == 'w')
  1165. X   {
  1166. X    do ch = readch(); while (ch == ' ');
  1167. X    if (ch == 'r' || ch == 'R' || ch == 'b' || ch == 'B') wtformat = ch;
  1168. X    else texterror();
  1169. X   }
  1170. X  else if (ch == ' ' || ch == '*' || ch == '\n');
  1171. X  else texterror();
  1172. X }
  1173. Xbufferptr = bufferptr - 1;
  1174. Xbreak;
  1175. X#ifndef SYMMETRIC
  1176. X
  1177. Xcase 'H':
  1178. Xif (nonetwork()) break;
  1179. Xitemp = readint(2,nlayers,'H');
  1180. Xif (readerror) break;
  1181. Xtemp = rdr(GE,0.0,'H');
  1182. Xif (!readerror) addhiddenunit(itemp,temp);
  1183. Xbreak;
  1184. X#endif
  1185. X
  1186. Xcase 'h': help(); break;
  1187. X
  1188. Xcase 'i':
  1189. Xdo ch = readch(); while (ch == ' ');
  1190. Xif (ch != '\n')
  1191. X {
  1192. X  bufferptr = bufferptr - 1;
  1193. X  inputfile = readstr();
  1194. X }
  1195. Xpushfile(inputfile);
  1196. Xbreak;
  1197. X
  1198. Xcase 'k':
  1199. Xif (nonetwork()) break;
  1200. Xtemp = rdr(GE,0.0,'k');
  1201. Xif (readerror) break;
  1202. Xtemp2 = rdr(GT,0.0,'k');
  1203. Xif (!readerror)
  1204. X {
  1205. X  if (initialkick == -1 && temp == 0) initialkick = temp2;
  1206. X  kick(temp,temp2);
  1207. X }
  1208. Xbreak;
  1209. X
  1210. Xcase 'l':
  1211. Xif (nonetwork()) break;
  1212. Xlayerno = readint(1,nlayers,'l'); 
  1213. Xif (readerror) break;
  1214. Xp = start;
  1215. Xfor (i=2;i<=layerno;i++) p = p->next;
  1216. Xprintoutunits(1,p,0);
  1217. Xbreak;
  1218. X
  1219. Xcase 'm':
  1220. Xnlayers = 0;
  1221. Xwttotal = 0;
  1222. Xch = readch();
  1223. Xp = NULL;
  1224. Xwhile (ch != '\n' && ch != '*')
  1225. X {
  1226. X  itemp = readint(1,MAXINT,'m');
  1227. X  if (readerror)
  1228. X   {
  1229. X    wttotal = 0;
  1230. X    goto endm;
  1231. X   };
  1232. X  nlayers = nlayers + 1;
  1233. X  p = mklayer(p,itemp);
  1234. X  if (nlayers == 1) start = p;
  1235. X  ch = readch();
  1236. X  while (ch == ' ') ch = readch();
  1237. X  if (ch >= '0' && ch <= '9') bufferptr = bufferptr - 1;
  1238. X };
  1239. Xlast = p;
  1240. Xp = start;
  1241. Xp = p->next;
  1242. Xhlayer = (UNIT *) p->units;
  1243. Xp = p->next;
  1244. Xif (p != NULL)
  1245. X {
  1246. X  ilayer = (UNIT *) p->units;
  1247. X  p = p->next;
  1248. X  if (p != NULL)
  1249. X   {
  1250. X    jlayer = (UNIT *) p->units;
  1251. X    p = p->next;
  1252. X    if (p != NULL) klayer = (UNIT *) p->units;
  1253. X   }
  1254. X };
  1255. Xbufferptr = bufferptr - 1;
  1256. Xnullpatterns();
  1257. Xclear();
  1258. Xendm: break;
  1259. X
  1260. Xcase 'n':
  1261. Xif (nonetwork()) break;
  1262. Xdo ch = readch(); while (ch == ' ');
  1263. Xif (ch == 'f')
  1264. X {
  1265. X  trainfile = readstr();
  1266. X  itemp = MAXINT;
  1267. X  pushfile(trainfile);
  1268. X }
  1269. Xelse
  1270. X {
  1271. X  bufferptr = bufferptr - 1;
  1272. X  itemp = readint(1,MAXINT,'n');
  1273. X  if (readerror) break;
  1274. X };
  1275. Xnullpatterns();
  1276. Xreadingpattern = 1;
  1277. Xnpats = readpats(itemp,'n');
  1278. Xif (itemp != MAXINT && npats < itemp)
  1279. X {
  1280. X  sprintf(outstr,"\n>>>>> only %d patterns read <<<<<\n\n",npats);
  1281. X  pg(outstr);
  1282. X };
  1283. Xreadingpattern = 0;
  1284. Xwrong = npats;
  1285. Xbreak;
  1286. X
  1287. Xcase 'O':
  1288. Xif (nonetwork() || nopatterns()) break;
  1289. Xitemp = readint(1,npats,'O');
  1290. Xif (readerror) break;
  1291. Xresetpats();
  1292. Xfor (i=1;i<=itemp;i++) nextpat();
  1293. Xsetoutputpat();
  1294. Xu = (UNIT *) last->units;
  1295. Xitemp2 = 0; /* unit counter */
  1296. Xi = 1;      /* format counter */
  1297. Xwhile (u != NULL)
  1298. X {
  1299. X  if (outformat == 'c')
  1300. X     if (unscale(u->tj) == 1) pg("1"); else pg("0");
  1301. X  else {sprintf(outstr,"%5.2f",unscale(u->tj)); pg(outstr);};
  1302. X  itemp2 = itemp2 + 1;
  1303. X  if (format[i] == itemp2)
  1304. X   {
  1305. X    if (outformat == 'r') pg("\n"); else pg(" ");
  1306. X    if (i < maxformat - 1) i = i + 1;
  1307. X   };
  1308. X  u = u->next;
  1309. X }
  1310. Xpg("\n");
  1311. Xbreak;
  1312. X
  1313. Xcase 'o':
  1314. Xdo ch = readch(); while (ch == ' ' || ch == '\n');
  1315. Xif (ch == 'r' || ch == 'a' || ch == 'c') outformat = ch;
  1316. Xelse printf("incorrect output format: %c\n",ch);
  1317. Xbreak;
  1318. X
  1319. Xcase 'P':
  1320. Xif (nonetwork() || nopatterns()) break;
  1321. Xdo ch = readch(); while (ch == ' ');
  1322. Xbufferptr = bufferptr - 1;
  1323. Xif (ch == '\n' || ch == '*') patcheck(1,npats,1,1,1,1,0);
  1324. Xelse
  1325. X {
  1326. X  itemp = readint(0,npats,'P');
  1327. X  if (readerror) break;
  1328. X  if (itemp == 0) patcheck(1,npats,0,0,1,1,0);
  1329. X  else patcheck(itemp,itemp,1,1,0,0,0);
  1330. X };
  1331. Xbreak;
  1332. X
  1333. Xcase 'p':
  1334. Xif (nonetwork()) break;
  1335. Xloadpat('p');
  1336. Xprintoutunits(1,last,0);
  1337. Xbreak;
  1338. X
  1339. Xcase 'Q':
  1340. Xtemp = rdr(GT,(REAL) KCODE,'Q');
  1341. Xif (!readerror) qmark = temp;
  1342. Xbreak;
  1343. X
  1344. Xcase 'q':
  1345. Xch = readch();
  1346. Xif (ch == '\n') return;
  1347. Xelse if (ch != 'p')
  1348. X {
  1349. X  texterror();
  1350. X }
  1351. Xelse
  1352. X {
  1353. X  while (ch != '\n' && ch != '*')
  1354. X   {
  1355. X    ch = readch();
  1356. X    if (ch == 'd')
  1357. X     {
  1358. X      temp = rdr(GT,0.0,'d');
  1359. X      if (!readerror) qpdecay = temp;
  1360. X     }
  1361. X    else if (ch == 'e')
  1362. X     {
  1363. X      temp = rdr(GT,0.0,'d');
  1364. X      if (!readerror && !nonetwork())
  1365. X       {
  1366. X        qpeta = temp;
  1367. X        p = start->next;
  1368. X        while (p != NULL)
  1369. X         {
  1370. X          u = (UNIT *) p->units;
  1371. X          while (u != NULL)
  1372. X           {
  1373. X            w = (WTNODE *) u->wtlist;
  1374. X            while (w != NULL)
  1375. X             {
  1376. X              w->eta = dbdeta;
  1377. X              w = w->next;
  1378. X             }
  1379. X            u = u->next;
  1380. X           }
  1381. X          p = p->next;
  1382. X         }
  1383. X       }
  1384. X     }
  1385. X    else if (ch == 'm')
  1386. X     {
  1387. X      temp = rdr(GT,0.0,'d');
  1388. X      if (!readerror) mu = temp;
  1389. X     }
  1390. X    else if (ch == 'n')
  1391. X     {
  1392. X      temp = rdr(GE,0.0,'d');
  1393. X      if (!readerror) qpnoise = temp;
  1394. X     }
  1395. X    else if (ch == 's')
  1396. X     {
  1397. X      do (ch = readch()); while (ch == ' ');
  1398. X      if (ch == '+' || ch == '-') qpslope = ch;
  1399. X      else texterror();
  1400. X     }
  1401. X    else if (ch == '*' || ch == '\n' || ch == ' ');
  1402. X    else texterror();
  1403. X   }
  1404. X  };
  1405. Xbufferptr = bufferptr - 1;
  1406. Xbreak;
  1407. X
  1408. Xcase 'R': if (nonetwork()) break; else restoreweights(); break;
  1409. X
  1410. Xcase 'r': /* r for run, rw for restore weights */
  1411. Xdo ch = readch(); while (ch == ' ');
  1412. Xif (ch == 'w')
  1413. X {
  1414. X  do ch = readch(); while (ch == ' ');
  1415. X  bufferptr = bufferptr - 1;
  1416. X  if (ch == '*' || ch == '\n') /* nothing */ ; else wtfile = readstr();
  1417. X  if (nonetwork()) break; else restoreweights();
  1418. X }
  1419. Xelse
  1420. X {
  1421. X  if (nonetwork() || nopatterns()) break;
  1422. X  bufferptr = bufferptr - 1;
  1423. X  itemp = readint(1,MAXINT,'r'); 
  1424. X  if (!readerror) iter = itemp; else break;
  1425. X  do ch = readch(); while (ch == ' ');
  1426. X  bufferptr = bufferptr - 1;
  1427. X  if (ch != '\n' && ch != '*')
  1428. X   {
  1429. X    itemp = readint(1,MAXINT,'r');
  1430. X    if (readerror != 1) run(iter,itemp);
  1431. X   }
  1432. X  else run(iter,-1);
  1433. X };
  1434. Xbreak;
  1435. X
  1436. Xcase 'S':
  1437. Xdo ch = readch(); while (ch == ' ');
  1438. Xbufferptr = bufferptr - 1;
  1439. Xif (ch == '\n' || ch == '*') itemp = 0;
  1440. Xelse
  1441. X {
  1442. X  itemp = readint(0,MAXINT,'S');
  1443. X  if (readerror) break; 
  1444. X };
  1445. Xif (itemp == 0) if (nonetwork()) break; else saveweights();
  1446. Xelse saverate = itemp;
  1447. Xbreak;
  1448. X
  1449. Xcase 's':  /* s <int> for seed, sw <filename> for save weights */
  1450. Xdo ch = readch(); while (ch == ' ');
  1451. Xif (ch == 'w')
  1452. X {
  1453. X  do ch = readch(); while (ch == ' ');
  1454. X  bufferptr = bufferptr - 1;
  1455. X  if (ch == '*' || ch == '\n') /* nothing */ ; else wtfile = readstr();
  1456. X  if (nonetwork()) break; else saveweights();
  1457. X }
  1458. Xelse
  1459. X {
  1460. X  bufferptr = bufferptr - 1;
  1461. X  sprev = seedstart;
  1462. X  while (ch != '\n' && ch != '*')
  1463. X   {
  1464. X    seed = readint(0,MAXINT,'s');
  1465. X    if (readerror) goto ends;
  1466. X    s = (SEEDNODE *) malloc(sizeof(SEEDNODE));
  1467. X    s->val = seed;
  1468. X    s->next = NULL;
  1469. X    sprev->next = s;
  1470. X    sprev = s;
  1471. X    do ch = readch(); while(ch == ' ');
  1472. X    if (ch >= '0' && ch <= '9') bufferptr = bufferptr - 1;
  1473. X   };
  1474. Xends: seed = seedstart->next->val;
  1475. X  srand(seed);
  1476. X}
  1477. Xbufferptr = bufferptr - 1;
  1478. Xbreak;
  1479. X#ifdef SYMMETRIC
  1480. X
  1481. Xcase 'T':
  1482. Xif (nonetwork()) break;
  1483. Xstdthresh = rdr(GT,-unscale(32767),'T');
  1484. Xif (readerror) break;
  1485. Xu = (UNIT *) last->units;
  1486. Xwhile (u != NULL)
  1487. X {
  1488. X  w = (WTNODE *) u->wtlist;
  1489. X  while (w->next != NULL) w = w->next;
  1490. X  *(w->weight) = stdthresh;
  1491. X  u = u->next;
  1492. X };
  1493. Xbreak;
  1494. X#endif
  1495. X
  1496. Xcase 't':
  1497. Xdo ch = readch(); while (ch == ' ');
  1498. Xif (ch == '\n' && (!nonetwork())) testcheck();
  1499. Xelse if (ch == 'f')
  1500. X {
  1501. X  do ch = readch(); while (ch == ' ');
  1502. X  if (ch == '\n' && (!nonetwork())) testcheck();
  1503. X  else
  1504. X   {
  1505. X    bufferptr = bufferptr - 1;
  1506. X    testfile = readstr();
  1507. X    if (!nonetwork()) testcheck();
  1508. X   };
  1509. X }
  1510. Xelse
  1511. X {
  1512. X  bufferptr = bufferptr - 1;
  1513. X  temp = rdr(GT,0.0,'t');
  1514. X  if (readerror) break;
  1515. X  else if (temp < scale(1.0)) toler = temp;
  1516. X  else printf("tolerance value out of range\n");
  1517. X };
  1518. Xbreak;
  1519. X#ifndef SYMMETRIC
  1520. X
  1521. Xcase 'W':
  1522. Xif (nonetwork()) break;
  1523. Xtemp = rdr(GT,0.0,'W');
  1524. Xif (!readerror)
  1525. X {
  1526. X  toosmall = temp;
  1527. X  whittle(temp);
  1528. X  printf("total weights now: %1d\n",wttotal);
  1529. X };
  1530. Xbreak;
  1531. X#endif
  1532. X
  1533. Xcase 'w':
  1534. Xif (nonetwork()) break;
  1535. Xlayerno = readint(2,nlayers,'w');
  1536. Xif (readerror) break;
  1537. Xunitno = readint(1,MAXINT,'w');
  1538. Xif (readerror) break;
  1539. Xu = locateunit(layerno,unitno);
  1540. Xif (u != NULL) printweights(u);
  1541. Xbreak;
  1542. X
  1543. Xcase 'x':
  1544. Xif (nonetwork()) break;
  1545. Xdo ch = readch(); while (ch == ' ');
  1546. Xif (ch == 'f')
  1547. X {
  1548. X  trainfile = readstr();
  1549. X  itemp = MAXINT;
  1550. X  pushfile(trainfile);
  1551. X }
  1552. Xelse
  1553. X {
  1554. X  bufferptr = bufferptr - 1;
  1555. X  itemp = readint(1,MAXINT,'n');
  1556. X  if (readerror) break;
  1557. X };
  1558. Xprevnpats = npats;
  1559. Xfindendofpats(start);
  1560. Xfindendofpats(last);
  1561. Xreadingpattern = 1;
  1562. Xitemp2 = readpats(itemp,'x');
  1563. Xif (itemp != MAXINT && npats < itemp2)
  1564. X   printf("\n>>>>> only %d patterns read <<<<<\n\n",npats);
  1565. Xreadingpattern = 0;
  1566. Xnpats = npats + itemp2;
  1567. Xwrong = wrong + itemp2;
  1568. Xbreak;
  1569. X
  1570. X/*
  1571. Xcase 'y': u = last->units;
  1572. X          w = (WTNODE *) u->wtlist;
  1573. X          temp = rdr(GE,-100.0,'y');
  1574. X          w = w->next;
  1575. X          w = w->next;
  1576. X          w->weight = temp;
  1577. X          break;
  1578. Xcase 'z': tracer();
  1579. X          break;
  1580. X*/
  1581. X
  1582. Xdefault: texterror();
  1583. Xbreak;
  1584. X      };
  1585. X    do ch = readch(); while (ch != '\n');
  1586. X  }while (!finished);
  1587. X}
  1588. X
  1589. Xvoid main(argc,argv)
  1590. Xint argc;
  1591. Xchar *argv[];
  1592. X{
  1593. XFILE *tempfile;
  1594. Xsetbuf(stdout,NULL);  /* set unbuffered output */
  1595. Xprintf("Fast Backpropagation Copyright (c) 1990, 1991, 1992 by Donald R. Tveter\n");
  1596. Xfilestackptr = 0;
  1597. Xfilestack[0] = stdin;
  1598. Xdata = stdin;
  1599. Xemptystring = '\0';
  1600. Xif (argc == 1)
  1601. X {
  1602. X  printf("no data file, stdin assumed\n");
  1603. X  datafilename = &emptystring;
  1604. X  testfile = &emptystring;
  1605. X }
  1606. Xelse
  1607. X {
  1608. X  datafilename = argv[1];
  1609. X  pushfile(datafilename);
  1610. X  if (argc == 3)
  1611. X   {
  1612. X    tempfile = fopen(argv[2],"r");
  1613. X    if (tempfile == NULL)
  1614. X     {
  1615. X      printf("there is no file: %s\n",argv[2]);
  1616. X      testfile = &emptystring;
  1617. X     }
  1618. X    else
  1619. X     {
  1620. X      testfile = argv[2];
  1621. X      fclose(tempfile);
  1622. X     }
  1623. X   }
  1624. X  else testfile = &emptystring;
  1625. X };
  1626. Xinit();
  1627. Xsignal(SIGINT,restartcmdloop); /* restart from interrrupt */
  1628. Xcmdloop();
  1629. Xif (copy != NULL)
  1630. X {
  1631. X  fflush(copy);
  1632. X  fclose(copy);
  1633. X }
  1634. X}
  1635. END_OF_FILE
  1636. if test 39734 -ne `wc -c <'bp.c'`; then
  1637.     echo shar: \"'bp.c'\" unpacked with wrong size!
  1638. fi
  1639. # end of 'bp.c'
  1640. fi
  1641. if test -f 'readme.mak' -a "${1}" != "-c" ; then 
  1642.   echo shar: Will not clobber existing file \"'readme.mak'\"
  1643. else
  1644. echo shar: Extracting \"'readme.mak'\" \(1657 characters\)
  1645. sed "s/^X//" >'readme.mak' <<'END_OF_FILE'
  1646. X   This code has been written to use either 32-bit floating point
  1647. X(float) or 64-bit floating point (double) arithmetic.  On System V
  1648. Xmachines the standard seems to be that all floating point arithmetic is
  1649. Xdone with double precision arithmetic so double arithmetic is faster
  1650. Xthan float and therefore this is the default.  Other versions of C (e.g.
  1651. XANSI C) will do single precision real arithmetic.  To get 32-bit
  1652. Xfloating point set the compiler flag FLOAT in the makefile.  The
  1653. Xfunction, exp, defined in real.c is double since System V specifies it
  1654. Xas double.  If your C uses float, change this definition as well.  One
  1655. Xoption exists for bp and sbp: if your compiler isn't smart enough to
  1656. Xdivide by 1024 by shifting remove the SMART flag in the makefile.
  1657. X
  1658. X   For UNIX systems, use the makefile, makefile.unx.  For DOS systems
  1659. Xthere are two makefiles to choose from, makefile and makereal.  Makefile
  1660. Xis designed to make all four programs but it only leaves around the
  1661. Xobject files for bp while erasing object files for sbp, rbp and srbp.
  1662. XOn the other hand, makereal only makes rbp and it leaves its object
  1663. Xfiles around.  For 16-bit DOS you need to set the flag, -DDOS16 and for
  1664. X32-bit DOS, you need to set the flag -DDOS32.  The flags I have in the
  1665. XDOS makefiles are what I use with Zortech C 3.0.
  1666. X
  1667. X   To make a particular executable file use the makefile given with the
  1668. Xdata files and make any or all of them like so:
  1669. X
  1670. X        UNIX                        DOS
  1671. X
  1672. X    make -f makefile.unx bp       make bp
  1673. X    make -f makefile.unx sbp      make sbp
  1674. X    make -f makefile.unx rbp      make rbp  or make -f makereal rbp
  1675. X    make -f makefile.unx srbp     make srbp
  1676. X
  1677. END_OF_FILE
  1678. if test 1657 -ne `wc -c <'readme.mak'`; then
  1679.     echo shar: \"'readme.mak'\" unpacked with wrong size!
  1680. fi
  1681. # end of 'readme.mak'
  1682. fi
  1683. echo shar: End of archive 2 \(of 4\).
  1684. cp /dev/null ark2isdone
  1685. MISSING=""
  1686. for I in 1 2 3 4 ; do
  1687.     if test ! -f ark${I}isdone ; then
  1688.     MISSING="${MISSING} ${I}"
  1689.     fi
  1690. done
  1691. if test "${MISSING}" = "" ; then
  1692.     echo You have unpacked all 4 archives.
  1693.     rm -f ark[1-9]isdone
  1694. else
  1695.     echo You still need to unpack the following archives:
  1696.     echo "        " ${MISSING}
  1697. fi
  1698. ##  End of shell archive.
  1699. exit 0
  1700.  
  1701. exit 0 # Just in case...
  1702.