home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / sources / misc / 3879 < prev    next >
Encoding:
Text File  |  1992-09-02  |  44.3 KB  |  1,710 lines

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