home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume28 / backprop / part02 < prev    next >
Encoding:
Text File  |  1992-02-22  |  40.1 KB  |  1,582 lines

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