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

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