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

  1. Newsgroups: comp.sources.misc
  2. Path: sparky!kent
  3. From: drt@chinet.chi.il.us (Donald Tveter)
  4. Subject:  v31i132:  backprop - Fast Backpropagation, Part04/04
  5. Message-ID: <1992Sep2.180801.28220@sparky.imd.sterling.com>
  6. Followup-To: comp.sources.d
  7. X-Md4-Signature: 22e9ea4124ae514281f5d106620a1edf
  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:08:01 GMT
  12. Approved: kent@sparky.imd.sterling.com
  13. Lines: 1816
  14.  
  15. Submitted-by: drt@chinet.chi.il.us (Donald Tveter)
  16. Posting-number: Volume 31, Issue 132
  17. Archive-name: backprop/part04
  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 4 (of 4)."
  28. # Contents:  real.c misc.c int.c
  29. # Wrapped by drt@chinet on Sat Jun 13 14:58:12 1992
  30. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  31. if test -f 'real.c' -a "${1}" != "-c" ; then 
  32.   echo shar: Will not clobber existing file \"'real.c'\"
  33. else
  34. echo shar: Extracting \"'real.c'\" \(10612 characters\)
  35. sed "s/^X//" >'real.c' <<'END_OF_FILE'
  36. X/* *********************************************************** */
  37. X/* file real.c:  contains the network evaluation and weight    */
  38. X/* adjustment procedures for the 64-bit floating point program */
  39. X/*                                                             */
  40. X/* Copyright (c) 1990, 1991, 1992 by Donald R. Tveter          */
  41. X/*                                                             */
  42. X/* *********************************************************** */
  43. X
  44. X#include "rbp.h"
  45. X#include <stdio.h>
  46. X
  47. Xextern char activation, backprop, deriv;
  48. Xextern REAL alpha, D, decay, eta, eta2, etamax, kappa, qpdecay, qpeta;
  49. Xextern REAL mu, noise, theta1, theta2, toler, totaldiff;
  50. Xextern int qpslope;
  51. Xextern LAYER *last, *start;
  52. X
  53. Xextern double exp(); /* built-in functions */
  54. X
  55. Xvoid forward()       /* computes unit activations */
  56. X{
  57. XUNIT *u, *predu;
  58. XLAYER *layer;
  59. XWTNODE *b;
  60. Xregister REAL fract, x, sum;
  61. XREAL val; /* should be in a register, but UNIX pc C-compiler does */
  62. X          /* not handle it correctly */
  63. Xint intpart;
  64. X
  65. Xlayer = start->next;
  66. Xwhile (layer != NULL)
  67. X {
  68. X  u = (UNIT *) layer->units;
  69. X  while (u != NULL)
  70. X   {
  71. X    sum = 0.0;
  72. X    b = (WTNODE *) u->wtlist;
  73. X    while (b != NULL)
  74. X     {
  75. X      predu = (UNIT *) b->backunit;
  76. X#ifdef SYMMETRIC
  77. X      sum = sum + *(b->weight) * predu->oj;
  78. X#else
  79. X      sum = sum + b->weight * predu->oj;
  80. X#endif
  81. X      b = b->next;
  82. X     };
  83. X    sum = sum * D;
  84. X    if (activation == 'p' || activation == 't')
  85. X     {
  86. X      if (sum >= 0.0) x = sum; else x = - sum;
  87. X      intpart = x;
  88. X      fract = x - intpart;
  89. X      switch (intpart) {
  90. Xcase 0:  val = 0.5 + 0.231 * fract;          /* 0 <= x < 1 */
  91. X         break;
  92. Xcase 1:  val = 0.731059 + 0.149738 * fract;  /* 1 <= x < 2 */
  93. X         break;
  94. Xcase 2:  val = 0.880797 + 0.071777 * fract;  /* 2 <= x < 3 */
  95. X         break;
  96. Xcase 3:
  97. Xcase 4:  val = 0.9525741 + (x - 3.0) * 0.02; /* 3 <= x < 5 */
  98. X         break;
  99. Xdefault: val = 1.0;                          /* x >= 5 */
  100. X            };
  101. X      if (sum < 0.0) u->oj = 1.0 - val; else u->oj = (REAL) val;
  102. X      if (activation == 't') u->oj = (u->oj - 0.5) * 2;
  103. X     }  /* end of p or t */
  104. X    else if (activation == 's') u->oj = 1.0 / (1.0 + exp(-sum));
  105. X    else if (activation == 'l') u->oj = sum;
  106. X    else if (activation == 'T') u->oj = 2.0 / (1.0 + exp(-sum)) - 1.0;
  107. X    u = u->next;
  108. X   };
  109. X  layer = layer->next;
  110. X };
  111. X}
  112. X
  113. Xshort backoutput()  /* back propagate errors from the output units */
  114. X{                   /* send down errors for any previous layers    */
  115. Xregister REAL deltaj, diff, adiff, uoj;
  116. Xregister UNIT *u, *bunit;
  117. Xregister WTNODE *w;
  118. Xregister short notclose;
  119. X
  120. Xnotclose = last->unitcount;
  121. Xu = (UNIT *) last->units;
  122. Xwhile (u != NULL)
  123. X {
  124. X  diff = u->tj - u->oj;
  125. X  if (diff > 0) adiff = diff; else adiff = -diff;
  126. X  if (adiff < toler) notclose = notclose - 1;
  127. X  totaldiff = totaldiff + adiff;
  128. X  if (adiff >= toler || backprop)
  129. X   {
  130. X    if (deriv == 'd') /* differential step size */
  131. X       deltaj = diff;
  132. X    else if (deriv == 'f' || deriv == 'F') /* Fahlman's derivative */
  133. X     {
  134. X      if (activation == 't' || activation == 'T') uoj = u->oj / 2 + 0.5;
  135. X      else uoj = u->oj;
  136. X      deltaj = diff * (0.1 + uoj * (1.0 - uoj));
  137. X     }
  138. X    else /* the original derivative */
  139. X     {
  140. X      if (activation == 't' || activation == 'T') uoj = u->oj / 2 + 0.5;
  141. X      else uoj = u->oj;
  142. X      deltaj = diff * uoj * (1.0 - uoj);
  143. X     };
  144. X    w = (WTNODE *) u->wtlist;
  145. X#ifdef SYMMETRIC
  146. X    while (w->next != NULL)
  147. X#else
  148. X    while (w != NULL)
  149. X#endif
  150. X     {
  151. X      bunit = (UNIT *) w->backunit;
  152. X#ifdef SYMMETRIC
  153. X      *(w->total) = *(w->total) + deltaj * bunit->oj;
  154. X#else
  155. X      w->total = w->total + deltaj * bunit->oj;
  156. X      if (bunit->layernumber > 1)  /* pass back the error */
  157. X         bunit->error = bunit->error + deltaj * w->weight;
  158. X#endif
  159. X      w = w->next;
  160. X     };
  161. X   }
  162. X  u = u->next;
  163. X }
  164. Xreturn(notclose);
  165. X}
  166. X
  167. X#ifndef SYMMETRIC
  168. X
  169. Xvoid backinner()  /* compute weight changes for hidden layers */
  170. X{                 /* send down errors for any previous layers */
  171. XLAYER *layer;
  172. Xregister REAL deltaj, uoj;
  173. Xregister UNIT *bunit;
  174. Xregister WTNODE *w;
  175. Xregister UNIT *u;
  176. X
  177. Xlayer = last->backlayer;
  178. Xwhile (layer->backlayer != NULL)
  179. X {
  180. X  u = (UNIT *) layer->units;
  181. X  while (u != NULL)
  182. X   {
  183. X    if (activation == 't' || activation == 'T') uoj = u->oj / 2 + 0.5;
  184. X    else uoj = u->oj;
  185. X    if (deriv == 'f') /* Fahlman's derivative */
  186. X       deltaj = (0.1 + uoj * (1.0 - uoj)) * u->error;
  187. X    else /* for o, d and F */
  188. X       deltaj = (uoj * (1.0 - uoj)) * u->error;
  189. X    w = (WTNODE *) u->wtlist;
  190. X    while (w != NULL)
  191. X     {
  192. X      bunit = (UNIT *) w->backunit;
  193. X      w->total = w->total + deltaj * bunit->oj;
  194. X      if (bunit->layernumber > 1)
  195. X         bunit->error = bunit->error + deltaj * w->weight;
  196. X      w = w->next;
  197. X     };
  198. X    u = u->next;
  199. X   };
  200. X  layer = layer->backlayer;
  201. X };
  202. X}
  203. X
  204. X#endif
  205. X
  206. X#ifdef SYMMETRIC
  207. Xvoid dbd_update() {pg("symmetric dbd update no longer supported\n");}
  208. X#else
  209. Xvoid dbd_update() /* delta-bar-delta method for changing weights */
  210. X{
  211. Xregister short stotal,sdbarm1;
  212. Xregister UNIT *u;
  213. Xregister WTNODE *w;
  214. XLAYER *layer;
  215. X
  216. X/* w->olddw is used for delta-bar minus 1 */
  217. X
  218. Xlayer = last;
  219. Xwhile (layer->backlayer != NULL)
  220. X {
  221. X  u = (UNIT *) layer->units;
  222. X  while (u != NULL)
  223. X   {
  224. X    w = (WTNODE *) u->wtlist;
  225. X    while (w != NULL)
  226. X     {
  227. X      if (w->total > 0) stotal = 1;
  228. X        else if (w->total < 0) stotal = -1;
  229. X         else stotal = 0;
  230. X      if (w->olddw > 0) sdbarm1 = 1;
  231. X        else if (w->olddw < 0) sdbarm1 = -1;
  232. X         else sdbarm1 = 0;
  233. X      w->olddw = theta2 * w->total + theta1 * w->olddw;
  234. X      if ((stotal > 0) && (sdbarm1 > 0)) w->eta = w->eta + kappa;
  235. X      else if ((stotal < 0) && (sdbarm1 < 0)) w->eta = w->eta + kappa;
  236. X      else if ((stotal > 0) && (sdbarm1 < 0)) w->eta = w->eta * decay;
  237. X      else if ((stotal < 0) && (sdbarm1 > 0)) w->eta = w->eta * decay;
  238. X      if (w->eta > etamax) w->eta = etamax;
  239. X      w->weight = w->weight + w->eta * w->total;
  240. X      w = w->next;
  241. X     };
  242. X    u = u->next;
  243. X   };
  244. X  layer = layer->backlayer;
  245. X };
  246. X}
  247. X#endif
  248. X
  249. Xvoid periodic_update()  /* the original periodic method */
  250. X{
  251. Xregister REAL reta, ralpha;
  252. Xregister UNIT *u;
  253. Xregister WTNODE *w;
  254. XLAYER *layer;
  255. X
  256. Xralpha = alpha;
  257. Xlayer = last;
  258. Xwhile (layer->backlayer != NULL)
  259. X {
  260. X  if (layer == last) reta = eta; else reta = eta2;
  261. X  u = (UNIT *) layer->units;
  262. X  while (u != NULL)
  263. X   {
  264. X    w = (WTNODE *) u->wtlist;
  265. X    while (w != NULL)
  266. X     {
  267. X#ifdef SYMMETRIC
  268. X      if (((UNIT *) w->backunit)->unitnumber > u->unitnumber)
  269. X       {
  270. X        *(w->olddw) = *(w->total) * reta + ralpha * *(w->olddw);
  271. X        *(w->weight) = *(w->weight) + *(w->olddw);
  272. X       };
  273. X#else
  274. X      w->olddw = w->total * reta + ralpha * w->olddw;
  275. X      w->weight = w->weight + w->olddw;
  276. X#endif
  277. X      w = w->next;
  278. X     };
  279. X    u = u->next;
  280. X   };
  281. X  layer = layer->backlayer;
  282. X };
  283. X}
  284. X
  285. X#ifndef SYMMETRIC
  286. Xvoid qp_update()
  287. X{
  288. Xregister REAL reta, s, nextdw, shrinkfactor, rqpdecay;
  289. Xregister short addslope;
  290. Xregister REAL rmu;
  291. Xregister UNIT *u;
  292. Xregister WTNODE *w;
  293. XLAYER *layer;
  294. X
  295. Xrqpdecay = qpdecay * 0.001;
  296. Xrmu = mu;
  297. Xshrinkfactor = rmu / (1.0 + rmu);
  298. Xreta = qpeta;
  299. Xif (qpslope == '+') addslope = 1; else addslope = 0;
  300. Xlayer = last;
  301. Xwhile (layer->backlayer != NULL)
  302. X {
  303. X  u = (UNIT *) layer->units;
  304. X  while (u != NULL)
  305. X   {
  306. X    w = (WTNODE *) u->wtlist;
  307. X    while (w != NULL)
  308. X     {
  309. X      s = rqpdecay * w->weight - w->total;
  310. X      if (w->olddw < 0.0)
  311. X       {
  312. X        if (s >= (shrinkfactor * w->eta)) nextdw = rmu * w->olddw;
  313. X        else nextdw = w->olddw * s / (w->eta - s);
  314. X        if (addslope && s > 0.0) nextdw = nextdw - reta * s;
  315. X       }
  316. X      else if (w->olddw > 0.0)
  317. X       {
  318. X        if (s <= (shrinkfactor * w->eta)) nextdw = rmu * w->olddw;
  319. X        else nextdw = w->olddw * s / (w->eta - s);
  320. X        if (addslope && s < 0.0) nextdw = nextdw - reta * s;
  321. X       }
  322. X      else nextdw = - reta * s;
  323. X      w->olddw = nextdw;
  324. X      w->weight = w->weight + nextdw;
  325. X      w->eta = s;
  326. X      w = w->next;
  327. X     };
  328. X    u = u->next;
  329. X   };
  330. X  layer = layer->backlayer;
  331. X };
  332. X}
  333. X#else
  334. Xvoid qp_update() {}
  335. Xvoid supersab() {}
  336. X#endif
  337. X
  338. Xshort cbackoutput()  /* backoutput for continuous updates */
  339. X{
  340. Xregister REAL deltaj, etadeltaj, diff, adiff, uoj, reta, ralpha;
  341. Xregister UNIT *u, *bunit;
  342. Xregister WTNODE *b;
  343. Xregister short notclose;
  344. X
  345. Xreta = eta;
  346. Xralpha = alpha;
  347. Xnotclose = last->unitcount;
  348. Xu = (UNIT *) last->units;
  349. Xwhile (u != NULL)
  350. X {
  351. X  diff = u->tj - u->oj;
  352. X  if (diff > 0) adiff = diff; else adiff = -diff;
  353. X  if (adiff < toler) notclose = notclose - 1;
  354. X  totaldiff = totaldiff + adiff;
  355. X  if (adiff >= toler || backprop)
  356. X   {
  357. X    if (deriv == 'd') /* differential step size derivative */
  358. X       deltaj = diff;
  359. X    else if (deriv == 'f' || deriv == 'F') /* Fahlman's derivative */
  360. X     {
  361. X      if (activation == 't' || activation == 'T') uoj = u->oj / 2 + 0.5;
  362. X      else uoj = u->oj;
  363. X      deltaj = diff * (0.1 + uoj * (1.0 - uoj));
  364. X     }
  365. X    else /* the original derivative */
  366. X     {
  367. X      if (activation == 't' || activation == 'T') uoj = u->oj / 2 + 0.5;
  368. X      else uoj = u->oj;
  369. X      deltaj = diff * uoj * (1.0 - uoj);
  370. X     };
  371. X    etadeltaj = deltaj * reta;
  372. X    b = (WTNODE *) u->wtlist;
  373. X#ifdef SYMMETRIC
  374. X    while (b->next != NULL)
  375. X#else
  376. X    while (b != NULL)
  377. X#endif
  378. X     {
  379. X      bunit = (UNIT *) b->backunit;
  380. X#ifdef SYMMETRIC
  381. X      *(b->olddw) = etadeltaj * bunit->oj + ralpha * *(b->olddw);
  382. X      *(b->weight) = *(b->weight) + *(b->olddw);
  383. X#else
  384. X      b->olddw = etadeltaj * bunit->oj + ralpha * b->olddw;
  385. X      b->weight = b->weight + b->olddw;
  386. X      if (bunit->layernumber > 1)
  387. X         bunit->error = bunit->error + deltaj * b->weight;
  388. X#endif
  389. X      b = b->next;
  390. X     };
  391. X   };
  392. X  u = u->next;
  393. X }
  394. Xreturn(notclose);
  395. X}
  396. X
  397. X#ifndef SYMMETRIC
  398. X
  399. Xvoid cbackinner()  /* backinner for continuous updates */
  400. X{
  401. XLAYER *layer;
  402. Xregister REAL deltaj, etadeltaj, reta, uoj, ralpha;
  403. Xregister UNIT *bunit, *u;
  404. Xregister WTNODE *b;
  405. X
  406. Xreta = eta2;
  407. Xralpha = alpha;
  408. Xlayer = last->backlayer;
  409. Xwhile (layer->backlayer != NULL)
  410. X {
  411. X  u = (UNIT *) layer->units;
  412. X  while (u != NULL)
  413. X   {
  414. X    if (activation == 't' || activation == 'T') uoj = u->oj / 2 + 0.5;
  415. X    else uoj = u->oj;
  416. X    if (deriv == 'f') /* Fahlman's derivative */
  417. X       deltaj = (0.1 + uoj * (1.0 - uoj)) * u->error;
  418. X    else /* for o, d and F */
  419. X       deltaj = (uoj * (1.0 - uoj)) * u->error;
  420. X    etadeltaj = reta * deltaj;
  421. X    b = (WTNODE *) u->wtlist;
  422. X    while (b != NULL)
  423. X     {
  424. X      bunit = (UNIT *) b->backunit;
  425. X      b->olddw = etadeltaj * bunit->oj + ralpha * b->olddw;
  426. X      b->weight = b->weight + b->olddw;
  427. X      if (bunit->layernumber > 1)
  428. X         bunit->error = bunit->error + deltaj * b->weight;
  429. X      b = b->next;
  430. X     };
  431. X    u = u->next;
  432. X   };
  433. X  layer = layer->backlayer;
  434. X };
  435. X}
  436. X#endif
  437. END_OF_FILE
  438. if test 10612 -ne `wc -c <'real.c'`; then
  439.     echo shar: \"'real.c'\" unpacked with wrong size!
  440. fi
  441. # end of 'real.c'
  442. fi
  443. if test -f 'misc.c' -a "${1}" != "-c" ; then 
  444.   echo shar: Will not clobber existing file \"'misc.c'\"
  445. else
  446. echo shar: Extracting \"'misc.c'\" \(17659 characters\)
  447. sed "s/^X//" >'misc.c' <<'END_OF_FILE'
  448. X/* **************************************************** */
  449. X/* file misc.c:  contains pattern manipulation routines */
  450. X/*               and miscellaneous other functions.     */
  451. X/*                                                      */
  452. X/* Copyright (c) 1990, 1991, 1992 by Donald R. Tveter   */
  453. X/*                                                      */
  454. X/* **************************************************** */
  455. X
  456. X#include <stdio.h>
  457. X
  458. X#ifdef UNIX
  459. X#include <malloc.h>
  460. X#else
  461. X#include <stdlib.h>
  462. X#include <conio.h>
  463. X#endif
  464. X
  465. X#ifdef INTEGER
  466. X#include "ibp.h"
  467. X#else
  468. X#include "rbp.h"
  469. X#endif
  470. X
  471. X/* an addition for large data sets */
  472. X
  473. Xextern INT32 g;
  474. X
  475. X/* built-in function */
  476. X
  477. Xextern int rand();
  478. X
  479. X/* homemade functions */
  480. X
  481. X#ifdef INTEGER
  482. Xextern REAL unscale(), unscaleint();
  483. Xextern WTTYPE scale();
  484. X#endif
  485. X
  486. Xextern short backoutput(), cbackoutput();
  487. Xextern void backinner(), cbackinner(), saveweights();
  488. Xextern WTTYPE rdr();
  489. Xextern void dbd_update(), periodic_update(), qp_update();
  490. Xextern REAL readchar();
  491. X
  492. Xextern char backprop,emptystring,informat,outstr[],patform,ringbell;
  493. Xextern char summary, *testfile, update, up_to_date_stats, wtlimithit;
  494. Xextern int bad, benchmark,bufferptr,lastprint,lastsave,npats;
  495. Xextern int prevnpats,readerror,readingpattern,right,saverate,testpat;
  496. Xextern int totaliter,unlearned,wrong,wttotal;
  497. Xextern WTTYPE dbdeta, error, initialkick, toler, toosmall;
  498. Xextern REAL errorperunit, pct_right;
  499. Xextern UNIT *hlayer, *ilayer, *jlayer, *klayer;
  500. Xextern LAYER *last, *start;
  501. Xextern short skiprate;
  502. X#ifdef INTEGER
  503. Xextern INT32 totaldiff;
  504. X#else
  505. Xextern REAL totaldiff;
  506. X#endif
  507. X
  508. Xvoid nullpatterns()  /* dispose of any patterns before reading more */
  509. X{
  510. XPATLIST *pl, *nextpl;
  511. XWTTYPE *p;
  512. X
  513. Xif (start->patstart != NULL)
  514. X {
  515. X  pl = start->patstart;
  516. X  while (pl != NULL)
  517. X   {
  518. X    nextpl = pl->next;
  519. X    p = pl->pats;
  520. X    free(p);
  521. X    pl = nextpl;
  522. X   };
  523. X  pl = last->patstart;
  524. X  while (pl != NULL)
  525. X   {
  526. X    nextpl = pl->next;
  527. X    p = pl->pats;
  528. X    free(p);
  529. X    pl = nextpl;
  530. X   };
  531. X };
  532. Xstart->patstart = NULL;
  533. Xlast->patstart = NULL;
  534. Xnpats = 0;
  535. Xprevnpats = 0;
  536. X}
  537. X
  538. Xvoid resetpats()
  539. X{
  540. Xstart->currentpat = NULL;
  541. Xlast->currentpat = NULL;
  542. X}
  543. X
  544. Xvoid findendofpats(layer)  /* purpose is to set all layer->currentpat */
  545. XLAYER *layer;              /* fields to end of pattern list so more   */
  546. X{                          /* patterns can be added at the end.       */
  547. XPATLIST *pl;
  548. Xpl = (PATLIST *) layer->patstart;
  549. Xwhile (pl->next != NULL) pl = pl->next;
  550. Xlayer->currentpat = pl;
  551. X}
  552. X
  553. Xint copyhidden(u,hidden,layerno)
  554. XUNIT *u, **hidden;
  555. Xint layerno;
  556. X{
  557. Xif (hidden == NULL)
  558. X {
  559. X  sprintf(outstr,"ran out of hidden units in layer %d\n",layerno);
  560. X  pg(outstr);
  561. X  return(0);
  562. X }
  563. Xu->oj = (*hidden)->oj;
  564. X*hidden = (*hidden)->next;
  565. Xreturn(1);
  566. X}
  567. X
  568. Xint loadpat(command)
  569. Xchar command;
  570. X{
  571. XUNIT *u, *hunit, *iunit, *junit, *kunit;
  572. Xhunit = hlayer;
  573. Xiunit = ilayer;
  574. Xjunit = jlayer;
  575. Xkunit = klayer;
  576. Xreadingpattern = 1;
  577. Xu = (UNIT *) start->units;
  578. Xwhile (u != NULL)
  579. X {
  580. X  if (informat == 'r') u->oj = rdr(GE,(REAL) HCODE,command);
  581. X  else u->oj = scale(readchar());
  582. X  if (readerror) goto errorexit;
  583. X  if (u->oj <= KCODE)
  584. X   {
  585. X    if (u->oj == HCODE)
  586. X       {if (!copyhidden(u,&hunit,2)) goto errorexit;}
  587. X    else if (u->oj == ICODE)
  588. X       {if (!copyhidden(u,&iunit,3)) goto errorexit;}
  589. X    else if (u->oj == JCODE)
  590. X       {if (!copyhidden(u,&junit,4)) goto errorexit;}
  591. X    else if (!copyhidden(u,&kunit,5)) goto errorexit;
  592. X   };
  593. X  u = u->next;
  594. X };
  595. Xreadingpattern = 0;
  596. Xforward();
  597. Xreturn(1);
  598. X
  599. Xerrorexit:
  600. Xreadingpattern = 0;
  601. Xreturn(0);
  602. X}
  603. X
  604. Xvoid nextpat()
  605. X{
  606. Xif (start->currentpat == NULL)
  607. X {
  608. X  start->currentpat = start->patstart;
  609. X  last->currentpat = last->patstart;
  610. X }
  611. Xelse
  612. X {
  613. X  start->currentpat = (start->currentpat)->next;
  614. X  last->currentpat = (last->currentpat)->next;
  615. X };
  616. X}
  617. X
  618. Xvoid setoutputpat()
  619. X{
  620. Xregister WTTYPE *p;
  621. Xregister UNIT *u;
  622. Xregister short i, answer;
  623. XPATLIST *pl;
  624. X
  625. Xif (patform == 'c' || patform == 'C')
  626. X {
  627. X  pl = last->currentpat;
  628. X  p = pl->pats;
  629. X  answer = *p;
  630. X  u = (UNIT *) last->units;
  631. X  for (i=1;i<=last->unitcount;i++)
  632. X   {
  633. X    if (i == answer) u->tj = scale(1.0); else u->tj = scale(0.0);
  634. X    u = u->next;
  635. X   };
  636. X }
  637. Xelse
  638. X {
  639. X  pl = last->currentpat;
  640. X  p = pl->pats;
  641. X  u = (UNIT *) last->units;
  642. X  while (u != NULL)
  643. X   {
  644. X    u->tj = *p++;
  645. X    u = u->next;
  646. X   };
  647. X }
  648. X}
  649. X
  650. Xvoid setinputpat()
  651. X{
  652. Xregister WTTYPE *p;
  653. Xregister UNIT *u;
  654. XUNIT *hunit, *iunit, *junit, *kunit;
  655. XPATLIST *pl;
  656. X  
  657. Xhunit = hlayer;
  658. Xiunit = ilayer;
  659. Xjunit = jlayer;
  660. Xkunit = klayer;
  661. Xpl = start->currentpat;
  662. Xp = pl->pats;
  663. Xu = (UNIT *) start->units;
  664. Xwhile (u != NULL)
  665. X {
  666. X  if (*p > KCODE) u->oj = *p++;
  667. X  else if (*p++ == HCODE)
  668. X     {if (!copyhidden(u,&hunit,2)) return;}
  669. X  else if (*p++ == ICODE)
  670. X     {if (!copyhidden(u,&iunit,3)) return;}
  671. X  else if (*p++ == JCODE)
  672. X     {if (!copyhidden(u,&junit,4)) return;}
  673. X  else if (!copyhidden(u,&kunit,5)) {p++; return;};
  674. X  u = u->next;
  675. X };
  676. X}
  677. X
  678. Xvoid setonepat() /* set input and output patterns */
  679. X{
  680. Xregister UNIT *u;
  681. Xregister LAYER *innerlayers;
  682. X
  683. Xsetinputpat();
  684. Xsetoutputpat();
  685. Xinnerlayers = start->next;
  686. Xwhile (innerlayers->next != NULL)
  687. X {  /* set errors on the inner layer units to 0 */
  688. X  u = (UNIT *) innerlayers->units;
  689. X  while (u != NULL)
  690. X   {
  691. X    u->error = 0;
  692. X    u = u->next;
  693. X   };
  694. X  innerlayers = innerlayers->next;
  695. X };
  696. X}
  697. X
  698. Xvoid clear()
  699. X{
  700. XLAYER *p;
  701. XUNIT *u;
  702. XWTNODE *w;
  703. Xint i;
  704. X
  705. Xif (toosmall != -1)
  706. X {
  707. X  pg("cannot restart with the weights removed\n");
  708. X  return;
  709. X };
  710. Xright = 0;
  711. Xwrong = npats;
  712. Xpct_right = 0.0;
  713. Xunlearned = npats;
  714. Xwtlimithit = 0;
  715. Xtotaliter = 0;
  716. Xlastsave = 0;
  717. Xinitialkick = -1;
  718. Xlastprint = 0;
  719. Xresetpats();
  720. Xfor (i=1;i<=npats;i++)
  721. X {
  722. X  nextpat();
  723. X  if (last->currentpat->bypass > 0) last->currentpat->bypass = 0;
  724. X  else if (last->currentpat->bypass < 0) last->currentpat->bypass = -1;
  725. X };
  726. Xp = start->next;
  727. Xwhile (p != NULL)
  728. X {
  729. X  u = (UNIT *) p->units;
  730. X  while (u != NULL)
  731. X   {
  732. X    w = (WTNODE *) u->wtlist;
  733. X    while (w != NULL)
  734. X     {
  735. X#ifdef SYMMETRIC
  736. X      if (w->next != NULL)
  737. X       { /* skip threshold weight */
  738. X        *(w->weight) = 0;
  739. X        *(w->olddw) = 0;
  740. X        *(w->eta) = dbdeta;
  741. X       };
  742. X#else
  743. X      w->weight = 0;
  744. X      w->olddw = 0;
  745. X      w->eta = dbdeta;
  746. X      w->slope = 0;
  747. X#endif
  748. X      w = w->next;
  749. X     };
  750. X    u = u->next;
  751. X   };
  752. X  p = p->next;
  753. X };
  754. X}
  755. X
  756. X#ifndef SYMMETRIC
  757. X
  758. Xvoid whittle(amount)    /* removes weights whose absolute */
  759. XWTTYPE amount;          /* value is less than amount      */
  760. X{LAYER *layer;
  761. X UNIT *u;
  762. X WTNODE *w, *wprev;
  763. X
  764. Xlayer = start->next;
  765. Xwhile (layer != NULL)
  766. X {
  767. X  u = (UNIT *) layer->units;
  768. X  while (u != NULL)
  769. X   {
  770. X    w = (WTNODE *) u->wtlist;
  771. X    wprev = (WTNODE *) NULL;
  772. X    while (w->next != (WTNODE *) NULL)
  773. X     {
  774. X      if ((w->weight) < amount && (w->weight) > -amount)
  775. X       {
  776. X        if (wprev == NULL) (WTNODE *) u->wtlist = w->next;
  777. X        else (WTNODE *) wprev->next = w->next;
  778. X        wttotal = wttotal - 1;
  779. X       }
  780. X      else wprev = w;
  781. X      w = w->next;
  782. X     }
  783. X    u = u->next;
  784. X   }
  785. X  layer = layer->next;
  786. X }
  787. X}
  788. X
  789. X#endif
  790. X
  791. Xvoid testcheck()  /* checks the testfile */
  792. X{
  793. Xint class, best, count, tcright, tcwrong, testcount, printing;
  794. Xint tright, twrong, ch2;
  795. XREAL pct, testerr, eperunit;
  796. XWTTYPE max;
  797. XUNIT *u;
  798. X
  799. Xif (!pushfile(testfile)) return;
  800. Xtesterr = 0.0;
  801. Xtestcount = 0;
  802. Xtcright = 0;
  803. Xtcwrong = 0;
  804. Xtright = 0;
  805. Xtwrong = 0;
  806. Xif (patform == 'c' || patform == 'g') printing = 0; else printing = 1;
  807. Xch2 = readch();
  808. Xwhile (ch2 != EOF)
  809. X {
  810. X  bufferptr = bufferptr - 1;
  811. X  if (!loadpat('t')) if (readerror == 2) goto summarize; else goto exit;
  812. X  class = 0;
  813. X  if (patform == 'c' || patform == 'C')
  814. X   {
  815. X    class = readint(1,last->unitcount,'t');
  816. X    if (readerror) goto exit;
  817. X    count = 0;
  818. X    max = -MAXINT;
  819. X    best = 0;
  820. X   };
  821. X  u = (UNIT *) last->units;
  822. X  while (u != NULL)
  823. X   {
  824. X    if (class)
  825. X     {
  826. X      count = count + 1;
  827. X      if (u->oj > max)
  828. X       {
  829. X        max = u->oj;
  830. X        best = count;
  831. X       }
  832. X      if (count == class) u->tj = scale(1.0); else u->tj = scale(0.0);
  833. X     }
  834. X    else
  835. X     {
  836. X      if (informat == 'r') u->tj = rdr(GT,(REAL) KCODE,'t');
  837. X      else u->tj = scale(readchar());
  838. X      if (readerror) goto exit;
  839. X     };
  840. X    u = u->next;
  841. X   };
  842. X  testcount = testcount + 1;
  843. X  if (class)
  844. X   if (best == class) tcright = tcright + 1; else tcwrong = tcwrong + 1;
  845. X  if (printing)
  846. X   {
  847. X    sprintf(outstr,"%5d",testcount);
  848. X    pg(outstr);
  849. X   };
  850. X  if (printoutunits(printing,last,1))
  851. X   {
  852. X    popfile();
  853. X    return;
  854. X   };
  855. X  testerr = testerr + unscale(error);
  856. X  if (bad) twrong = twrong + 1; else tright = tright + 1;
  857. X  do ch2 = readch(); while (ch2 != '\n');
  858. X  ch2 = readch();
  859. X };
  860. X
  861. Xsummarize:
  862. Xpct = 100.0 * (REAL) tright / (REAL) testcount;
  863. Xif (pg("based on tolerance:\n")) return;
  864. Xsprintf(outstr,"   %6.2f%%,   (%d right,  %d wrong)",pct,tright,twrong);
  865. Xpg(outstr);
  866. Xeperunit = testerr / (REAL) (last->unitcount * testcount);
  867. Xsprintf(outstr,"   %7.5f error/unit\n",eperunit); pg(outstr);
  868. Xif (patform == 'c' || patform == 'C')
  869. X {
  870. X  pct = 100.0 * (REAL) tcright / (REAL) testcount;
  871. X  if (pg("based on maximum value:\n")) return;
  872. X  sprintf(outstr,"   %6.2f%%,   (%d right,   %d wrong)\n",pct,tcright,tcwrong);
  873. X  pg(outstr);
  874. X };
  875. Xpopfile();
  876. Xreturn;
  877. X
  878. Xexit:
  879. Xsprintf(outstr,"error while reading pattern %d\n",testcount+1);
  880. Xpg(outstr);
  881. Xpopfile();
  882. X}
  883. X
  884. Xvoid stats(callfromrun)
  885. Xint callfromrun;
  886. X{
  887. X if (callfromrun) wrong = unlearned;
  888. X right = npats - wrong;
  889. X if (testpat) right = right - 1;
  890. X errorperunit =
  891. X    unscaleint(totaldiff) / (REAL) ((right + wrong) * last->unitcount);
  892. X pct_right = 100.0 * (REAL) right / (REAL) (right + wrong);
  893. X}
  894. X
  895. Xint patcheck(first,finish,printoutputs,printerrors,sumup,printsumup,skip)
  896. Xint first,finish,printoutputs,printerrors,sumup,printsumup,skip;
  897. X{
  898. Xint i;
  899. X
  900. Xif (skip && printoutputs == 0) goto shortcut;
  901. Xif (sumup)
  902. X {
  903. X  totaldiff = 0;
  904. X  wrong = 0;
  905. X };
  906. Xresetpats();
  907. Xfor (i=1;i<first;i++) nextpat();
  908. Xfor (i=first;i<=finish;i++)
  909. X { 
  910. X  nextpat();
  911. X  setonepat();
  912. X  forward();
  913. X  if (printoutputs)
  914. X   {
  915. X    sprintf(outstr,"%3d ",i);
  916. X    pg(outstr);
  917. X   };
  918. X  if (printoutunits(printoutputs,last,printerrors)) return(1);
  919. X  if (i != testpat && sumup)
  920. X    {
  921. X     wrong = wrong + bad;
  922. X     totaldiff = totaldiff + error;
  923. X    };
  924. X };
  925. Xif (printoutputs) lastprint = totaliter;
  926. Xif (sumup) stats(0);
  927. X
  928. Xshortcut:
  929. Xif (printsumup)
  930. X {
  931. X  sprintf(outstr,"%5d iterations  ",totaliter); pg(outstr);
  932. X  sprintf(outstr,"%6.2f%% right ",pct_right); pg(outstr);
  933. X  sprintf(outstr,"(%1d right ",right); pg(outstr);
  934. X  sprintf(outstr,"  %1d wrong)   ",wrong); pg(outstr);
  935. X  sprintf(outstr,"%7.5f error/unit\n",errorperunit);
  936. X  if (pg(outstr)) return(1);
  937. X }
  938. Xreturn(0);
  939. X}
  940. X
  941. Xvoid oneset() /* go through the patterns once and update weights */
  942. X{ 
  943. Xint i;
  944. XLAYER *layer;
  945. Xregister UNIT *u;
  946. Xregister WTNODE *w;
  947. Xshort numbernotclose, attempted, passed;
  948. X
  949. Xlayer = last;      /* make all b->totals = 0 */
  950. Xwhile (layer->backlayer != NULL)
  951. X {
  952. X  u = (UNIT *) layer->units;
  953. X  while (u != NULL)
  954. X   {
  955. X    w = (WTNODE *) u->wtlist;
  956. X    while (w != NULL)
  957. X     {
  958. X#ifdef SYMMETRIC
  959. X      *(w->total) = 0;
  960. X#else
  961. X      w->total = 0;
  962. X#endif
  963. X      w = w->next;
  964. X     };
  965. X    u = u->next;
  966. X   };
  967. X  layer = layer->backlayer;
  968. X };
  969. Xattempted = 0;
  970. Xpassed = 0;
  971. Xif (testpat) unlearned = npats - 1; else unlearned = npats;
  972. Xresetpats();
  973. Xfor(i=1;i<=npats;i++)
  974. X {
  975. X  nextpat();
  976. X  if (last->currentpat->bypass == 0)
  977. X   {
  978. X    setonepat();
  979. X    forward();
  980. X    attempted = attempted + 1;
  981. X    if (update == 'c') numbernotclose = cbackoutput();
  982. X    else numbernotclose = backoutput();
  983. X    if (numbernotclose != 0)
  984. X     {
  985. X#ifndef SYMMETRIC
  986. X      if (update == 'c') cbackinner(); else backinner();
  987. X#endif
  988. X     }
  989. X    else /* this one pattern has been learned */
  990. X     {
  991. X      passed = passed + 1;
  992. X      unlearned = unlearned - 1;
  993. X      last->currentpat->bypass = skiprate;
  994. X#ifndef SYMMETRIC
  995. X      if (backprop) if (update == 'c') cbackinner(); else backinner();
  996. X#endif
  997. X     }
  998. X   }
  999. X  else last->currentpat->bypass = last->currentpat->bypass - 1;
  1000. X };
  1001. Xif (update == 'c') totaliter = totaliter + 1;
  1002. Xif (up_to_date_stats == '+' && update == 'c') patcheck(1,npats,0,0,1,0,0);
  1003. Xif (unlearned == 0) return;
  1004. Xif (skiprate && (attempted == passed))
  1005. X {
  1006. X  resetpats();
  1007. X  for (i=1;i<=npats;i++)
  1008. X   {
  1009. X    nextpat();
  1010. X    if (last->currentpat->bypass > 0) last->currentpat->bypass = 0;
  1011. X   };
  1012. X };
  1013. Xif (update == 'c') return;
  1014. Xelse if (update == 'd') dbd_update();
  1015. Xelse if (update == 'p') periodic_update();
  1016. Xelse if (update == 'q') qp_update();
  1017. Xif (up_to_date_stats == '+') patcheck(1,npats,0,0,1,0,0);
  1018. Xtotaliter = totaliter + 1;
  1019. X}
  1020. X
  1021. Xvoid kick(size,amount) /* give the network a kick */
  1022. XWTTYPE size, amount;
  1023. X{ 
  1024. XLAYER *layer;
  1025. XUNIT *u;
  1026. XWTNODE *w;
  1027. XWTTYPE value;
  1028. XWTTYPE delta;
  1029. Xint sign;
  1030. X
  1031. Xlayer = start->next;
  1032. Xwhile (layer != NULL)
  1033. X {
  1034. X  u = (UNIT *) layer->units;
  1035. X  while (u != NULL)
  1036. X   {
  1037. X    w = (WTNODE *) u->wtlist;
  1038. X    while (w != NULL)
  1039. X     {
  1040. X#ifdef SYMMETRIC
  1041. X      value = *(w->weight);
  1042. X#else
  1043. X      value = w->weight;
  1044. X#endif
  1045. X      if (value != 0) sign = 1;
  1046. X      else if ((rand() & 32767) > 16383) sign = -1;
  1047. X      else sign = 1;
  1048. X      delta = (INT32) sign * amount * (rand() & 32767) / 32768;
  1049. X      if (value >= size) value = value - delta;
  1050. X      else if (value < -size) value = value + delta;
  1051. X#ifdef SYMMETRIC
  1052. X      if (((UNIT *) w->backunit)->unitnumber != u->unitnumber &&
  1053. X         w->next != NULL)
  1054. X         *(w->weight) = value;
  1055. X#else
  1056. X      w->weight = value;
  1057. X#endif
  1058. X      w = w->next;
  1059. X     }
  1060. X    u = u->next;
  1061. X   }
  1062. X  layer = layer->next;
  1063. X } 
  1064. X}
  1065. X
  1066. Xvoid newoneset() /* go through the patterns once and update weights */
  1067. X{ int i;
  1068. X  LAYER *layer;
  1069. X  register UNIT *u;
  1070. X  register WTNODE *w;
  1071. X  short numbernotclose, attempted, passed;
  1072. X
  1073. Xbegin:
  1074. X layer = last;      /* make all b->totals = 0 */
  1075. X while (layer->backlayer != NULL)
  1076. X  {
  1077. X   u = (UNIT *) layer->units;
  1078. X   while (u != NULL)
  1079. X    {
  1080. X     w = (WTNODE *) u->wtlist;
  1081. X     while (w != NULL)
  1082. X      {
  1083. X#ifdef SYMMETRIC
  1084. X       *(w->total) = 0;
  1085. X#else
  1086. X       w->total = 0;
  1087. X#endif
  1088. X       w = w->next;
  1089. X      };
  1090. X     u = u->next;
  1091. X    };
  1092. X   layer = layer->backlayer;
  1093. X  };
  1094. X attempted = 0;
  1095. X passed = 0;
  1096. X unlearned = npats;
  1097. X resetpats();
  1098. X for(i=1;i<=npats;i++)
  1099. X  {
  1100. X   nextpat();
  1101. X   if (last->currentpat->bypass == 0)
  1102. X    {
  1103. X     setonepat();
  1104. X     forward();
  1105. X     attempted = attempted + 1;
  1106. X     if (update == 'c') numbernotclose = cbackoutput();
  1107. X     else numbernotclose = backoutput();
  1108. X     if (numbernotclose != 0)
  1109. X      {
  1110. X#ifndef SYMMETRIC
  1111. X       if (update == 'c') cbackinner(); else backinner();
  1112. X#endif
  1113. X      }
  1114. X     else /* this one pattern has been learned */
  1115. X      {
  1116. X       passed = passed + 1;
  1117. X       unlearned = unlearned - 1;
  1118. X       last->currentpat->bypass = skiprate;
  1119. X#ifndef SYMMETRIC
  1120. X       if (backprop) if (update == 'c') cbackinner(); else backinner();
  1121. X#endif
  1122. X      }
  1123. X    }
  1124. X   else last->currentpat->bypass = last->currentpat->bypass - 1;
  1125. X   if (g && (i % g == 0 || i == npats))
  1126. X    {
  1127. X     if (update == 'd') dbd_update();
  1128. X     else if (update == 'p') periodic_update();
  1129. X     layer = last;      /* make all b->totals = 0 */
  1130. X     while (layer->backlayer != NULL)
  1131. X      {
  1132. X       u = (UNIT *) layer->units;
  1133. X       while (u != NULL)
  1134. X        {
  1135. X         w = (WTNODE *) u->wtlist;
  1136. X         while (w != NULL)
  1137. X          {
  1138. X           w->total = 0;
  1139. X           w = w->next;
  1140. X          };
  1141. X         u = u->next;
  1142. X        };
  1143. X       layer = layer->backlayer;
  1144. X      }; /* end while */
  1145. X    };  /* end if g */
  1146. X}; /* end for i */
  1147. Xif (update == 'c'|| g != 0) totaliter = totaliter + 1;
  1148. Xif (up_to_date_stats == '+' && update == 'c') patcheck(1,npats,0,0,1,0,0);
  1149. Xif (unlearned == 0) return;
  1150. Xif (skiprate && (attempted == passed))
  1151. X {
  1152. X  resetpats();
  1153. X  for (i=1;i<=npats;i++)
  1154. X   {
  1155. X    nextpat();
  1156. X    last->currentpat->bypass = 0;
  1157. X   };
  1158. X  goto begin;
  1159. X };
  1160. Xif (g == 0)
  1161. X {
  1162. X  if (update == 'c') return;
  1163. X  else if (update == 'd') dbd_update();
  1164. X  else if (update == 'p') periodic_update();
  1165. X  else if (update == 'q') qp_update();
  1166. X };
  1167. Xif (up_to_date_stats == '+') patcheck(1,npats,0,0,1,0,0);
  1168. Xif (g == 0) totaliter = totaliter + 1;
  1169. X}
  1170. X
  1171. Xint run(n,prpatsrate)
  1172. Xint n;            /* the number of iterations to run */
  1173. Xint prpatsrate;   /* rate at which to print output patterns */
  1174. X{
  1175. Xint i, wtlimitbefore;
  1176. X#ifndef UNIX
  1177. Xint chx;
  1178. X#endif
  1179. X
  1180. Xif (pg("running . . .\n")) return(1);
  1181. Xfor (i=1;i<=n;i++)
  1182. X {
  1183. X  totaldiff = 0;
  1184. X  wtlimitbefore = wtlimithit;
  1185. X  if (g == 0) oneset(); else newoneset();
  1186. X  stats(1);
  1187. X  if (wtlimitbefore == 0 && wtlimithit == 1)
  1188. X   {
  1189. X    sprintf(outstr,">>>>> WEIGHT LIMIT HIT <<<<< at %d\n",totaliter);
  1190. X    if (pg(outstr)) return(1);
  1191. X   };
  1192. X  if (unlearned == 0) /* training finished */
  1193. X   {
  1194. X    if (benchmark && testpat)
  1195. X     {
  1196. X      sprintf(outstr,"S  %d iterations",totaliter); pg(outstr);
  1197. X      sprintf(outstr," %9.5f error/unit\n",errorperunit); pg(outstr);
  1198. X      if (patcheck(testpat,testpat,1,1,0,0,0)) return(1);
  1199. X     };
  1200. X    if ((prpatsrate > 0 && lastprint != totaliter))
  1201. X     if (patcheck(1,npats,summary == '-',summary == '-',1,1,0)) return(1);
  1202. X    sprintf(outstr,"patterns learned to within %4.2f",unscale(toler));
  1203. X    pg(outstr);
  1204. X    pg(" at iteration");
  1205. X    if (ringbell == '+') putchar(7);
  1206. X    sprintf(outstr," %d\n",totaliter);
  1207. X    if (pg(outstr)) return(1);
  1208. X    if (benchmark && *testfile != emptystring) testcheck();
  1209. X    return(0);
  1210. X   };
  1211. X  if (benchmark && testpat && (prpatsrate > 0 && i % prpatsrate == 0))
  1212. X   {
  1213. X    if (unlearned == 1) pg("S"); else pg("F");
  1214. X    sprintf(outstr," %d iterations",totaliter); pg(outstr);
  1215. X    sprintf(outstr," %7.5f error/unit\n",errorperunit);
  1216. X    if (pg(outstr)) return(1);
  1217. X    if (patcheck(testpat,testpat,1,1,0,0,0)) return(1);
  1218. X   }
  1219. X  if (totaliter % saverate == 0) saveweights();
  1220. X  if ((prpatsrate > 0) && ((i % prpatsrate == 0) || (i == n)))
  1221. X   {
  1222. X    if (patcheck(1,npats,summary == '-',summary == '-',1,1,
  1223. X             up_to_date_stats == '-')) return(1);
  1224. X    if (benchmark && (*testfile != emptystring)) testcheck();
  1225. X   };
  1226. X#ifndef UNIX
  1227. X  if (kbhit() && getch() == 27 /* escape key */) return(1);
  1228. X#endif
  1229. X };
  1230. Xreturn(0);
  1231. X} 
  1232. END_OF_FILE
  1233. if test 17659 -ne `wc -c <'misc.c'`; then
  1234.     echo shar: \"'misc.c'\" unpacked with wrong size!
  1235. fi
  1236. # end of 'misc.c'
  1237. fi
  1238. if test -f 'int.c' -a "${1}" != "-c" ; then 
  1239.   echo shar: Will not clobber existing file \"'int.c'\"
  1240. else
  1241. echo shar: Extracting \"'int.c'\" \(16375 characters\)
  1242. sed "s/^X//" >'int.c' <<'END_OF_FILE'
  1243. X/* *********************************************************** */
  1244. X/* file int.c:  Contains the network evaluation and weight     */
  1245. X/*              adjustment procedures for the integer versions */
  1246. X/*              bp and sbp.                                    */
  1247. X/*                                                             */
  1248. X/* Copyright (c) 1990, 1991, 1992 by Donald R. Tveter          */
  1249. X/*                                                             */
  1250. X/* *********************************************************** */
  1251. X
  1252. X#include "ibp.h"
  1253. X#include <stdio.h>
  1254. X
  1255. Xextern char activation, backprop, deriv, wtlimithit;
  1256. Xextern WTTYPE alpha, D, decay, eta, eta2, etamax, kappa, qpdecay, qpeta;
  1257. Xextern WTTYPE qpnoise, mu, noise, theta1, theta2, toler;
  1258. Xextern int qpslope;
  1259. Xextern LAYER *last, *start;
  1260. Xextern INT32 totaldiff;
  1261. X
  1262. Xvoid forward()             /* computes unit activations */
  1263. X{ 
  1264. Xregister INT32 sum, x, intpart;
  1265. Xregister WTNODE *w;
  1266. Xregister UNIT *u, *predu;
  1267. XLAYER *layer;
  1268. Xregister short fract, val;
  1269. X
  1270. Xlayer = start->next;
  1271. Xwhile (layer != NULL)
  1272. X {
  1273. X  u = (UNIT *) layer->units;
  1274. X  while (u != NULL)
  1275. X   {
  1276. X    sum = 0;
  1277. X    w = (WTNODE *) u->wtlist;
  1278. X    while (w != NULL)
  1279. X     {
  1280. X      predu = (UNIT *) w->backunit;
  1281. X#ifdef SMART
  1282. X#   ifdef SYMMETRIC
  1283. X      sum = sum + (INT32) *(w->weight) * predu->oj / 1024;
  1284. X#   else
  1285. X      sum = sum + (INT32) w->weight * predu->oj / 1024;
  1286. X#   endif
  1287. X#else
  1288. X#   ifdef SYMMETRIC
  1289. X      x = (INT32) *(w->weight) * predu->oj;
  1290. X#   else
  1291. X      x = (INT32) w->weight * predu->oj;
  1292. X#   endif
  1293. X      if (x >= 0) sum = sum + (x >> 10); else sum = sum - ( (-x) >> 10);
  1294. X#endif
  1295. X      w = w->next;
  1296. X     };
  1297. X    sum = (INT32) D * sum / 1024;
  1298. X    if (activation == 'p' || activation == 't')
  1299. X     {
  1300. X      if (sum > 0) x = sum; else x = -sum;
  1301. X      intpart = x >> 10;
  1302. X      fract = x & 01777;
  1303. X      switch (intpart) {
  1304. Xcase 0:  val = 512 + (((INT32) 237 * fract) >> 10);       /* 0 <= x < 1 */
  1305. X         break;
  1306. Xcase 1:  val = 748 + (((INT32) 153 * fract) >> 10);       /* 1 <= x < 2 */
  1307. X         break;
  1308. Xcase 2:  val = 901 + (((INT32) 73 * fract) >> 10);        /* 2 <= x < 3 */
  1309. X         break;
  1310. Xcase 3:
  1311. Xcase 4:  val = 976 + (((INT32) (x - 3072) * 20) >> 10);   /* 3 <= x < 5 */
  1312. X         break;
  1313. Xdefault: val = 1024;                                      /* x >= 5 */ };
  1314. X         if (sum < 0) u->oj = 1024 - val; else u->oj = val;
  1315. X         if (activation == 't') u->oj = (u->oj - 512) * 2;
  1316. X     }
  1317. X    else if (activation == 'l') u->oj = sum;
  1318. X    u = u->next;
  1319. X   };
  1320. X    layer = layer->next;
  1321. X   };
  1322. X}
  1323. X
  1324. Xshort backoutput()  /* computes weight changes from the output layer */
  1325. X{
  1326. Xregister short deltaj, temp2, temp3;
  1327. Xregister INT32 temp;
  1328. Xregister UNIT *bunit, *u;
  1329. Xregister WTNODE *w;
  1330. Xregister short adiff, notclose;
  1331. X
  1332. Xnotclose = last->unitcount;
  1333. Xu = (UNIT *) last->units;
  1334. Xwhile (u != NULL)
  1335. X { 
  1336. X  temp3 = u->oj;
  1337. X  temp2 = u->tj - temp3;
  1338. X  if (temp2 > 0) adiff = temp2; else adiff = -temp2;
  1339. X  if (adiff < toler) notclose = notclose - 1;
  1340. X  totaldiff = totaldiff + adiff;
  1341. X  if (adiff >= toler || backprop)  /* then compute errors */
  1342. X   {
  1343. X    if (deriv == 'd') /* diff. step size method */
  1344. X       deltaj = temp2;
  1345. X    else if (deriv == 'f' || deriv == 'F') /* Fahlman's derivative */
  1346. X     {
  1347. X      if (activation == 't') temp3 = temp3 / 2 + 512;
  1348. X      temp = (INT32) temp2 * ((INT32) 104448 + (INT32) temp3 * ((short)(1024 - temp3)));
  1349. X      if (temp > 0) deltaj = (INT32) (temp + 524288) >> 20;
  1350. X      else deltaj = -((INT32) (524288 - temp) >> 20);
  1351. X     }
  1352. X    else /* the derivative in the original formula */
  1353. X     {
  1354. X      if (activation == 't') temp3 = temp3 / 2 + 512;
  1355. X      temp = (INT32) temp2 * ((INT32) temp3 * ((short)(1024 - temp3)));
  1356. X      if (temp > 0) deltaj = (INT32) (temp + 524288) >> 20;
  1357. X      else deltaj = -((INT32) (524288 - temp) >> 20);
  1358. X     }
  1359. X    w = (WTNODE *) u->wtlist;
  1360. X#ifdef SYMMETRIC
  1361. X    while (w->next != NULL)  /* skips threshold unit at end */
  1362. X#else
  1363. X    while (w != NULL)
  1364. X#endif
  1365. X     {
  1366. X      bunit = (UNIT *) w->backunit;
  1367. X#ifdef SYMMETRIC
  1368. X      *(w->total) = *(w->total) + (INT32) deltaj * bunit->oj;
  1369. X#else
  1370. X      w->total = w->total + (INT32) deltaj * bunit->oj;
  1371. X      if (bunit->layernumber > 1)
  1372. X         bunit->error = bunit->error + (INT32) deltaj * w->weight;
  1373. X#endif
  1374. X      w = w->next;
  1375. X     }
  1376. X   };
  1377. X  u = u->next;
  1378. X };
  1379. Xreturn(notclose);
  1380. X}
  1381. X
  1382. X#ifndef SYMMETRIC
  1383. X
  1384. Xvoid backinner()             /* Computes slopes and passes back */
  1385. X{                            /* errors from hidden layers.      */
  1386. Xregister short deltaj, temp3;
  1387. Xregister INT32 temp;
  1388. Xregister UNIT *bunit, *u;
  1389. Xregister WTNODE *w;
  1390. XLAYER *layer;
  1391. X
  1392. Xlayer = last->backlayer;
  1393. Xwhile (layer->backlayer != NULL)
  1394. X {
  1395. X  u = (UNIT *) layer->units;
  1396. X  while (u != NULL)
  1397. X   {
  1398. X    if (activation == 't') temp3 = u->oj / 2 + 512; else temp3 = u->oj;
  1399. X    if (deriv == 'f') /* Fahlman's derivative */
  1400. X       temp = (INT32) (((short)(((INT32) temp3*((short)(1024-temp3))+512) >> 10))
  1401. X              + 102) * u->error;
  1402. X    else /* either for the original or diff. step size */
  1403. X       temp = (INT32) ((short)(((INT32) temp3*((short)(1024-temp3))+512) >> 10))
  1404. X          * u->error;
  1405. X    if (temp > 0) deltaj = (INT32) (temp + 524288) >> 20;
  1406. X    else deltaj = -((INT32) (524288 - temp) >> 20);
  1407. X    w = (WTNODE *) u->wtlist;
  1408. X    while (w != NULL)
  1409. X     {
  1410. X      bunit = (UNIT *) w->backunit;
  1411. X      w->total = w->total + (INT32) deltaj * bunit->oj;
  1412. X      if (bunit->layernumber > 1)
  1413. X         bunit->error = bunit->error + (INT32) deltaj * w->weight;
  1414. X      w = w->next;
  1415. X     };
  1416. X    u = u->next;
  1417. X   };
  1418. X  layer = layer->backlayer;
  1419. X };
  1420. X}
  1421. X
  1422. X#endif
  1423. X
  1424. X#ifdef SYMMETRIC
  1425. Xvoid dbd_update() {pg("symmetric dbd update no longer supported\n");}
  1426. X#else
  1427. Xvoid dbd_update() /* the delta-bar-delta method for weight updates */
  1428. X{
  1429. Xregister short rkappa, temp2, dbarm1, rdecay;
  1430. Xregister INT32 temp;
  1431. Xregister UNIT *u;
  1432. Xregister WTNODE *w;
  1433. XLAYER *layer;
  1434. X
  1435. X/* w->olddw is used for delta-bar minus 1 */
  1436. X
  1437. Xrkappa = kappa;
  1438. Xrdecay = decay;
  1439. Xlayer = last;
  1440. Xwhile (layer->backlayer != NULL)
  1441. X {
  1442. X  u = (UNIT *) layer->units;
  1443. X  while (u != NULL)
  1444. X   {
  1445. X    w = (WTNODE *) u->wtlist;
  1446. X    while (w != NULL)
  1447. X     {
  1448. X      if (w->total > 0) temp2 = (INT32) (w->total + 512) >> 10;
  1449. X      else temp2 = -((INT32) (512 - w->total) >> 10);
  1450. X      dbarm1 = w->olddw;
  1451. X      temp = (INT32) theta2 * temp2 + (INT32) theta1 * dbarm1;
  1452. X      if (temp > 0) w->olddw = (INT32) (temp + 512) >> 10;
  1453. X      else w->olddw = -((INT32) (512 - temp) >> 10);
  1454. X      if (temp2 > 0 && dbarm1 > 0) w->eta = w->eta + rkappa;
  1455. X      else if (temp2 < 0 && dbarm1 < 0) w->eta = w->eta + rkappa;
  1456. X      else if (temp2 > 0 && dbarm1 < 0)w->eta = ((INT32) w->eta * rdecay) >> 10;
  1457. X      else if (temp2 < 0 && dbarm1 > 0)w->eta = ((INT32) w->eta * rdecay) >> 10;
  1458. X      if (w->eta > etamax) w->eta = etamax;
  1459. X      temp = (INT32) temp2 * w->eta;
  1460. X      if (temp > 0) temp2 = (INT32) (temp + 512) >> 10;
  1461. X      else if (temp < 0) temp2 = -((INT32) (512 - temp) >> 10);
  1462. X
  1463. X      else if (w->slope == 0)
  1464. X         {if (w->total < 0) temp2 = noise; else temp2 = -noise;}
  1465. X      w->slope = temp2;
  1466. X
  1467. X      temp = (INT32) w->weight + temp2;
  1468. X      if (temp > MAXSHORT)
  1469. X       {
  1470. X        wtlimithit = 1;
  1471. X        w->weight = MAXSHORT;
  1472. X       }
  1473. X      else if (temp < MINSHORT)
  1474. X       {
  1475. X        wtlimithit = 1;
  1476. X        w->weight = MINSHORT;
  1477. X       }
  1478. X      else w->weight = temp;
  1479. X      w = w->next;
  1480. X     };
  1481. X    u = u->next;
  1482. X   };
  1483. X  layer = layer->backlayer;
  1484. X };
  1485. X}
  1486. X#endif
  1487. X
  1488. Xvoid periodic_update()   /* update weights for the original method */
  1489. X{                        /* and the differential step size algorithm */
  1490. Xregister short reta, ralpha;
  1491. Xregister INT32 temp;
  1492. Xregister short temp2;
  1493. Xregister UNIT *u;
  1494. Xregister WTNODE *w;
  1495. XLAYER *layer;
  1496. X
  1497. Xralpha = alpha;
  1498. Xlayer = last;
  1499. Xwhile (layer->backlayer != NULL)
  1500. X {
  1501. X  if (layer == last) reta = eta; else reta = eta2;
  1502. X  u = (UNIT *) layer->units;
  1503. X  while (u != NULL)
  1504. X   {
  1505. X    w = (WTNODE *) u->wtlist;
  1506. X    while (w != NULL)
  1507. X     {
  1508. X#ifdef SYMMETRIC
  1509. X      if (((UNIT *) w->backunit)->unitnumber > u->unitnumber)
  1510. X       {
  1511. X        if (*(w->total) > 0) temp = (INT32) ((INT32)(*(w->total) + 512) >> 10) * reta 
  1512. X           + (INT32) ralpha * *(w->olddw);
  1513. X        else temp = (INT32) -(((INT32) 512 - *(w->total)) >> 10) * reta
  1514. X           + (INT32) ralpha * *(w->olddw);
  1515. X        if (temp > 0) temp2 = (INT32) (temp + 512) >> 10;
  1516. X        else temp2 = -(((INT32) 512 - temp) >> 10);
  1517. X        *(w->olddw) = temp2;
  1518. X        temp = (INT32) *(w->weight) + temp2;
  1519. X        if (temp > MAXSHORT)
  1520. X         {
  1521. X          wtlimithit = 1;
  1522. X          *(w->weight) = MAXSHORT;
  1523. X         }
  1524. X        else if (temp < MINSHORT)
  1525. X         {
  1526. X          wtlimithit = 1;
  1527. X          *(w->weight) = MINSHORT;
  1528. X         }
  1529. X        else *(w->weight) = temp;
  1530. X       };
  1531. X#else
  1532. X      if (w->total > 0)
  1533. X        temp = (INT32) (((INT32) w->total + 512) >> 10) * reta + (INT32) ralpha * w->olddw;
  1534. X      else
  1535. X        temp = (INT32) -(((INT32) 512 - w->total) >> 10) * reta + (INT32) ralpha * w->olddw;
  1536. X      if (temp > 0) temp2 = (INT32) (temp + 512) >> 10;
  1537. X      else temp2 = -(((INT32) 512 - temp) >> 10);
  1538. X      w->olddw = temp2;
  1539. X      temp = (INT32) w->weight + temp2;
  1540. X      if (temp > MAXSHORT)
  1541. X       {
  1542. X        wtlimithit = 1;
  1543. X        w->weight = MAXSHORT;
  1544. X       }
  1545. X      else if (temp < MINSHORT)
  1546. X       {
  1547. X        wtlimithit = 1;
  1548. X        w->weight = MINSHORT;
  1549. X       }
  1550. X      else w->weight = temp;
  1551. X#endif
  1552. X      w = w->next;
  1553. X     };
  1554. X    u = u->next;
  1555. X   };
  1556. X  layer = layer->backlayer;
  1557. X };
  1558. X}
  1559. X
  1560. X#ifndef SYMMETRIC
  1561. Xvoid qp_update()
  1562. X{
  1563. Xregister INT32 temp, s, nextdw;
  1564. Xregister WTNODE *w;
  1565. Xregister short rmu, reta, shrinkfactor, rqpdecay, addslope;
  1566. Xregister UNIT *u;
  1567. XLAYER *layer;
  1568. X
  1569. Xrmu = mu;
  1570. Xshrinkfactor = ((INT32) rmu * 1024 + 512) / (1024 + rmu);
  1571. Xreta = qpeta;
  1572. Xrqpdecay = qpdecay;
  1573. Xif (qpslope == '+') addslope = 1; else addslope = 0;
  1574. Xlayer = last;
  1575. Xwhile (layer->backlayer != NULL)
  1576. X {
  1577. X  u = (UNIT *) layer->units;
  1578. X  while (u != NULL)
  1579. X   {
  1580. X    w = (WTNODE *) u->wtlist;
  1581. X    while (w != NULL)
  1582. X     {
  1583. X      if (w->weight > 0)
  1584. X         s = (((INT32) rqpdecay * w->weight + 512) >> 10) - w->total;
  1585. X      else s = -(((INT32) 512 - (INT32) rqpdecay * w->weight) >> 10) - w->total;
  1586. X      if (s > 0) s = (s + 512) >> 10;
  1587. X      else s = -(((INT32) 512 - s) >> 10);
  1588. X      if (w->olddw < 0)
  1589. X       {
  1590. X        if (s >= (((INT32) shrinkfactor * w->eta) >> 10))
  1591. X           nextdw =  - (((INT32) 512 - (INT32) rmu * w->olddw) >> 10);
  1592. X        else nextdw = ((INT32) w->olddw * s) / (w->eta - s);
  1593. X        if (addslope && s > 0)
  1594. X           nextdw = nextdw - (((INT32) reta * s + 512) >> 10);
  1595. X       }
  1596. X      else if (w->olddw > 0)
  1597. X       {
  1598. X        if (s <= (((INT32) shrinkfactor * w->eta) >> 10))
  1599. X           nextdw = (((INT32) rmu * w->olddw + 512) >> 10);
  1600. X        else nextdw = ((INT32) w->olddw * s) / (w->eta - s);
  1601. X        if (addslope && s < 0)
  1602. X           nextdw = nextdw + (((INT32) 512 - (INT32) reta * s) >> 10);
  1603. X       }
  1604. X      else
  1605. X       {
  1606. X        temp = (INT32) reta * s;
  1607. X        if (temp > 0) nextdw = - ((temp + 512) >> 10);
  1608. X        else if (temp < 0) nextdw = (((INT32) 512 - temp) >> 10);
  1609. X        else if (s > 0) nextdw = qpnoise;
  1610. X        else nextdw = -qpnoise;
  1611. X       };
  1612. X      if (nextdw > MAXSHORT) nextdw = MAXSHORT;
  1613. X      else if (nextdw < MINSHORT) nextdw = MINSHORT;
  1614. X      w->olddw = nextdw;
  1615. X      temp = (INT32) w->weight + nextdw;
  1616. X      if (temp > MAXSHORT)
  1617. X       {
  1618. X        wtlimithit = 1;
  1619. X        w->weight = MAXSHORT;
  1620. X       }
  1621. X      else if (temp < MINSHORT)
  1622. X       {
  1623. X        wtlimithit = 1;
  1624. X        w->weight = MINSHORT;
  1625. X       }
  1626. X      else w->weight = temp;
  1627. X      if (s > MAXSHORT) w->eta = MAXSHORT;
  1628. X      else if (s < MINSHORT) w->eta = MINSHORT;
  1629. X      else w->eta = s;
  1630. X      w = w->next;
  1631. X     };
  1632. X    u = u->next;
  1633. X   };
  1634. X  layer = layer->backlayer;
  1635. X };
  1636. X}
  1637. X#endif
  1638. X
  1639. Xshort cbackoutput()          /* The continuous update version */
  1640. X{                            /* of back-propagation */
  1641. Xregister short deltaj;
  1642. Xregister INT32 etadeltaj, temp, temp2;
  1643. Xregister short temp3, adiff;
  1644. Xregister UNIT *bunit;
  1645. Xregister WTNODE *w;
  1646. Xregister UNIT *u;
  1647. Xregister short ralpha, reta, notclose;
  1648. X
  1649. Xralpha = alpha;
  1650. Xreta = eta;
  1651. Xnotclose = last->unitcount;
  1652. Xu = (UNIT *) last->units;
  1653. Xwhile (u != NULL)
  1654. X { 
  1655. X  temp3 = u->oj;
  1656. X  temp2 = u->tj - temp3;
  1657. X  if (temp2 > 0) adiff = temp2; else adiff = -temp2;
  1658. X  if (adiff < toler) notclose = notclose - 1;
  1659. X  totaldiff = totaldiff + adiff;
  1660. X  if (adiff >= toler || backprop)
  1661. X   {
  1662. X    if (deriv == 'd') /* the differential step size method */
  1663. X      deltaj = temp2;
  1664. X    else if (deriv == 'f' || deriv == 'F') /* Fahlman's derivative */
  1665. X     { /* deltaj = (u->tj - u->oj) * [0.1 + u->oj*(1.0 - u->oj)] */
  1666. X      if (activation == 't') temp3 = temp3 / 2 + 512;
  1667. X      temp = (INT32) temp2 * ((INT32) 104448 + (INT32) temp3 * ((short)(1024 - temp3)));
  1668. X      if(temp > 0) deltaj = (INT32) (temp + 524288) >> 20;
  1669. X      else deltaj = -(((INT32) 524288 - temp) >> 20);
  1670. X     }
  1671. X    else /* the original derivative */
  1672. X     { /* deltaj = (u->tj - u->oj) * u->oj * (1.0 - u->oj) */
  1673. X      if (activation == 't') temp3 = temp3 / 2 + 512;
  1674. X      temp = (INT32) temp2 * ((INT32) temp3 * ((short)(1024 - temp3)));
  1675. X      if(temp > 0) deltaj = ((INT32) temp + 524288) >> 20;
  1676. X      else deltaj = -(((INT32) 524288 - temp) >> 20);
  1677. X     };
  1678. X    etadeltaj = (INT32) deltaj * reta;
  1679. X    w = (WTNODE *) u->wtlist;
  1680. X#ifdef SYMMETRIC
  1681. X    while (w->next != NULL)
  1682. X#else
  1683. X    while (w != NULL)
  1684. X#endif
  1685. X     { /* get a slope for each weight */
  1686. X      bunit = (UNIT *) w->backunit;
  1687. X      temp = (INT32) etadeltaj * bunit->oj;
  1688. X      if(temp > 0) temp = (INT32) (temp + 524288) >> 20;
  1689. X      else temp = -(((INT32) 524288 - temp) >> 20);
  1690. X#ifdef SYMMETRIC
  1691. X      temp2 = (INT32) ralpha * *(w->olddw);
  1692. X#else
  1693. X      temp2 = (INT32) ralpha * w->olddw;
  1694. X#endif
  1695. X      if (temp2 > 0) temp3 = temp + (((INT32) temp2 + 512) >> 10);
  1696. X      else temp3 = temp - (((INT32) 512 - temp2) >> 10);
  1697. X#ifdef SYMMETRIC
  1698. X      *(w->olddw) = temp3;
  1699. X#else
  1700. X      w->olddw = temp3;
  1701. X#endif
  1702. X      /* w->weight = w->weight + w->olddw */
  1703. X#ifdef SYMMETRIC
  1704. X      temp = (INT32) *(w->weight) + temp3;
  1705. X      if (temp > MAXSHORT)
  1706. X       {
  1707. X        wtlimithit = 1;
  1708. X        *(w->weight) = MAXSHORT;
  1709. X       }
  1710. X      else if (temp < MINSHORT)
  1711. X       {
  1712. X        wtlimithit = 1;
  1713. X        *(w->weight) = MINSHORT;
  1714. X       }
  1715. X      else *(w->weight) = temp;
  1716. X#else
  1717. X      temp = (INT32) w->weight + temp3;
  1718. X      if (temp > MAXSHORT)
  1719. X       {
  1720. X        wtlimithit = 1;
  1721. X        temp3 = MAXSHORT;
  1722. X       }
  1723. X      else if (temp < MINSHORT)
  1724. X       {
  1725. X        wtlimithit = 1;
  1726. X        temp3 = MINSHORT;
  1727. X       }
  1728. X      else temp3 = temp;
  1729. X      w->weight = temp3;
  1730. X      if (bunit->layernumber > 1)
  1731. X         bunit->error = bunit->error + (INT32) deltaj * temp3;
  1732. X#endif
  1733. X      w = w->next;
  1734. X     }
  1735. X   }
  1736. X  u = u->next;
  1737. X }
  1738. Xreturn(notclose);
  1739. X}
  1740. X
  1741. X#ifndef SYMMETRIC
  1742. X
  1743. Xvoid cbackinner()
  1744. X{
  1745. Xregister short deltaj;
  1746. Xregister INT32 etadeltaj, temp, temp2;
  1747. Xregister short temp3, reta, ralpha;
  1748. Xregister UNIT *bunit;
  1749. Xregister WTNODE *w;
  1750. Xregister UNIT *u;
  1751. XLAYER *layer;
  1752. X
  1753. Xreta = eta2;
  1754. Xralpha = alpha;
  1755. Xlayer = last->backlayer;
  1756. Xwhile (layer->backlayer != NULL)
  1757. X {
  1758. X  u = (UNIT *) layer->units;
  1759. X  while (u != NULL)
  1760. X   {
  1761. X    if (activation == 't') temp3 = u->oj / 2 + 512;
  1762. X    else temp3 = u->oj;
  1763. X    if (deriv == 'f')  /* Fahlman's derivative */
  1764. X       temp = (INT32) ((((INT32) temp3 * ((short)(1024 - temp3)) + 512) >> 10) + 102)
  1765. X               * u->error;
  1766. X    else  /* diff. step size and original derivative */
  1767. X       temp = (((INT32) temp3 * ((short)(1024 - temp3)) + 512) >> 10)
  1768. X                * u->error;
  1769. X    if (temp > 0) deltaj = (INT32) (temp + 524288) >> 20;
  1770. X    else deltaj = -(((INT32) 524288 - temp) >> 20);
  1771. X    etadeltaj = (INT32) reta * deltaj;
  1772. X    w = (WTNODE *) u->wtlist;
  1773. X    while (w != NULL)
  1774. X     {
  1775. X      bunit = (UNIT *) w->backunit;
  1776. X      temp = (INT32) etadeltaj * bunit->oj;
  1777. X      if (temp > 0) temp = (INT32) (temp + 524288) >> 20;
  1778. X      else temp = -(((INT32) 524288 - temp) >> 20);
  1779. X      temp2 = (INT32) ralpha * w->olddw;
  1780. X      if (temp2 > 0) temp3 = temp + ((INT32) (temp2 + 512) >> 10);
  1781. X      else temp3 = temp - (((INT32) 512 - temp2) >> 10);
  1782. X      w->olddw = temp3;
  1783. X      temp = (INT32) w->weight + temp3;
  1784. X      if (temp > MAXSHORT)
  1785. X       {
  1786. X        wtlimithit = 1;
  1787. X        temp3 = MAXSHORT;
  1788. X       }
  1789. X      else if (temp < MINSHORT)
  1790. X       {
  1791. X        wtlimithit = 1;
  1792. X        temp3 = MINSHORT;
  1793. X       }
  1794. X      else temp3 = temp;       
  1795. X      w->weight = temp3;
  1796. X      if (bunit->layernumber > 1)
  1797. X         bunit->error = bunit->error + (INT32) deltaj * temp3;
  1798. X      w = w->next;
  1799. X     };
  1800. X    u = u->next;
  1801. X   };
  1802. X  layer = layer->backlayer;
  1803. X };
  1804. X}
  1805. X#endif
  1806. END_OF_FILE
  1807. if test 16375 -ne `wc -c <'int.c'`; then
  1808.     echo shar: \"'int.c'\" unpacked with wrong size!
  1809. fi
  1810. # end of 'int.c'
  1811. fi
  1812. echo shar: End of archive 4 \(of 4\).
  1813. cp /dev/null ark4isdone
  1814. MISSING=""
  1815. for I in 1 2 3 4 ; do
  1816.     if test ! -f ark${I}isdone ; then
  1817.     MISSING="${MISSING} ${I}"
  1818.     fi
  1819. done
  1820. if test "${MISSING}" = "" ; then
  1821.     echo You have unpacked all 4 archives.
  1822.     rm -f ark[1-9]isdone
  1823. else
  1824.     echo You still need to unpack the following archives:
  1825.     echo "        " ${MISSING}
  1826. fi
  1827. ##  End of shell archive.
  1828. exit 0
  1829.  
  1830. exit 0 # Just in case...
  1831.