home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume07 / art1 < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  10.4 KB

  1. From decwrl!ucbvax!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!allbery Sun Jun  4 14:44:40 PDT 1989
  2. Article 902 of comp.sources.misc:
  3. Path: decwrl!ucbvax!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!allbery
  4. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  5. Newsgroups: comp.sources.misc
  6. Subject: v07i013: ART1 Simulator
  7. Message-ID: <56719@uunet.UU.NET>
  8. Date: 4 Jun 89 01:49:00 GMT
  9. Sender: allbery@uunet.UU.NET
  10. Reply-To: bandu@cs.buffalo.edu (Jagath SamaraBandu)
  11. Organization: SUNY/Buffalo Computer Science
  12. Lines: 410
  13. Approved: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  14.  
  15. Posting-number: Volume 7, Issue 13
  16. Submitted-by: bandu@cs.buffalo.edu
  17. Archive-name: art1
  18.  
  19. [Why does stuff from cs.buffalo.edu come from "nobody@cs.buffalo.edu"?  ++bsa]
  20.  
  21. This is a short program which simulates ART1 neural network, proposed by 
  22. Grossberg and Carpenter. The algorithm is taken directly from an article in
  23. 87 IEEE ASSP magazine by Lippman. (which btw is an excellent article).
  24.  
  25. Please send me any bug reports/fixes. I'm not that keen on improving this,
  26. but if there are any suggestions, I'll try to include them.
  27.  
  28. Jagath
  29. PS:Any comments, brickbats and flowers are welcome. (Even if it is about the
  30. programming style)
  31.  
  32. -------------------cut-------here----------------------------------------------
  33. # This is a shell archive.  Remove anything before this line, then
  34. # unpack it by saving it in a file and typing "sh file".  (Files
  35. # unpacked will be owned by you and have default permissions.)
  36. #
  37. # This archive contains:
  38. # art1.c art1.data art1.tex
  39.  
  40. echo x - art1.c
  41. sed -e 's/^X//' > "art1.c" << '//E*O*F art1.c//'
  42. X/* Simiulation of ART-1 Neural network (Ref: Lippman 87)
  43. X   Written by Jagath Samarabandu <bandu@cs.buffalo.edu> 
  44. X   To compile, type cc art1.c -o art1  */
  45. X
  46. X#include <stdio.h>
  47. X
  48. X#define N      25   /* No. of nodes in F1 */
  49. X#define M      10   /* No. of nodes in F2 */
  50. X
  51. Xinitialize(top_down,bot_up,n,m) /* initialize the LTM traces */
  52. X     double top_down[],bot_up[]; /* top down and bot. up LTM traces */
  53. X     int n,m;                   /* No. of nodes in F1 and F2 */
  54. X{
  55. X  int i;
  56. X  double t;
  57. X
  58. X  t = 1.0/(1.0+n);
  59. X  for (i=0;i<n*m;i++) {
  60. X    top_down[i] = 1.0; bot_up[i] = t;
  61. X  }
  62. X}
  63. X
  64. Xcompute_matching_scores(mu,bot_up,input,n,m) /* calculate matching scores */
  65. X     double mu[],bot_up[],input[];            /* mu - matching score */
  66. X     int n,m;                                /* No. of F1 and F2 nodes */
  67. X{
  68. X  int i,j;
  69. X
  70. X  for (j=0; j<m; j++) 
  71. X    for (i=0, mu[j]=0.0; i<n; i++)
  72. X      mu[j] += bot_up[i*m+j]*input[i];
  73. X}
  74. X
  75. Xdouble vigilance(top_down,input,jmax,n,m) /* returns |T.X|/|X| */
  76. X     double top_down[],input[];
  77. X     int n,m,jmax;
  78. X{
  79. X  int i;
  80. X  double x,t;
  81. X
  82. X  for (i=0,x=0.0; i<n; i++)
  83. X    x += input[i];
  84. X  for (i=0,t=0.0; i<n; i++)
  85. X    t += top_down[i*m+jmax]*input[i];
  86. X  return(t/x);
  87. X}
  88. X
  89. Xint find_max(array,len) /* find the max of array and return the index */
  90. X     double array[];
  91. X     int len;
  92. X{
  93. X  int j,jmax;
  94. X
  95. X  for (j=0,jmax=0; j<len; j++)
  96. X    if (array[j]>array[jmax])
  97. X      jmax = j;
  98. X  return (jmax);
  99. X}
  100. X
  101. Xadapt_LTM(top_down,bot_up, input,jmax,n,m) /* change top down and bot.up LTM */
  102. X     double top_down[],bot_up[],input[];
  103. X     int n,m,jmax;
  104. X{
  105. X  int i,ij;
  106. X  double sum,t;
  107. X  
  108. X  for (i=0,sum=0.5; i<n; i++)
  109. X    sum += top_down[i*m+jmax]*input[i];
  110. X
  111. X  for (i=0,ij=jmax; i<n; i++,ij += m) {
  112. X    t = top_down[ij]*input[i];
  113. X    bot_up[ij] = t/sum;
  114. X    top_down[ij] = t;
  115. X  }
  116. X}
  117. X
  118. Xload_data(d,max,n,fname) /* load data from file */
  119. X     int d[],max,n;
  120. X     char *fname[];
  121. X{
  122. X  FILE *fp;
  123. X  int n_pat,n_var,i,j;
  124. X  
  125. X  if (!(fp=fopen(fname,"r"))) exit(perror(fname));
  126. X  fscanf(fp,"%d %d\n",&n_pat,&n_var);
  127. X  if (n_pat>max) {
  128. X    printf("Warning! only %2d patterns out of %d are read\n",max,n_pat);
  129. X    n_pat = max;
  130. X  }
  131. X  if (n_var!=n)
  132. X    exit(printf("wrong pattern size: should be %2d. was %2d\n",n,n_var)); 
  133. X    
  134. X  for (i=0;j<n_pat;i++)
  135. X    for (j=0;j<n_var;j++)
  136. X      fscanf(fp,"%d",&d[i*n+j]);
  137. X  fclose(fp);
  138. X}
  139. X
  140. Xdisplay(in,top_down,x,y,m) /* display input and top down weights */
  141. X     double in[],top_down[];
  142. X     int x,y,m;
  143. X{
  144. X  int i,ix,iy,j;
  145. X
  146. X  for (iy=0,i=0; iy<y; iy++){
  147. X    for (ix=0,i=iy*y; ix<x; ix++,i++)
  148. X      printf("%c",(in[i]>0.5)?'#':' ');
  149. X    printf(" | ");
  150. X    for (j=0; j<m; j++) {
  151. X      for (ix=0,i=iy*y; ix<x; ix++,i++)
  152. X    printf("%c",(top_down[i*m+j]>0.5)?'#':' ');
  153. X      printf(" ");
  154. X    }
  155. X    printf("\n");
  156. X  }
  157. X  printf("\n");
  158. X}
  159. X/*****************  main routine starts here  *******************************/
  160. X
  161. Xint data[20][N]={
  162. X#include "art1.data"
  163. X};
  164. X
  165. Xmain(argc,argv)
  166. X     int argc;
  167. X     char *argv[];
  168. X{
  169. X  double t[N][M],b[N][M],x[N],mu[M],rho;
  170. X  int max_j,i,j,n_pat,ok,seq[M];
  171. X
  172. X  if (argc>1)
  173. X    n_pat = load_data(data,20,N,argv[1]);
  174. X  else n_pat=20;
  175. X  initialize(t,b,N,M);
  176. X  printf("Vigilance threshold: "); scanf("%lf",&rho);
  177. X  printf("\nSimulation of ART1 network with vigilance Threshold = %3.1lf\n\n",rho);
  178. X
  179. X  do {
  180. X    for (i=0; i<n_pat; i++) {
  181. X      for (j=0; j<N; j++)
  182. X    x[j] = (double)data[i][j];
  183. X      compute_matching_scores(mu,b,x,N,M);
  184. X      bzero((char *)seq,M*sizeof(int)); j=1;
  185. X      do {
  186. X    max_j = find_max(mu,M); seq[max_j] = j++;
  187. X    if (vigilance(t,x,max_j,N,M)>rho) {
  188. X      adapt_LTM(t,b,x,max_j,N,M);
  189. X      seq[max_j] = -1;
  190. X      break;
  191. X    }
  192. X    else 
  193. X      mu[max_j] = 0.0;
  194. X      } while (1);
  195. X      printf("IN:%2d    ",i);
  196. X      for (j=0;j<M; j++) {
  197. X    if (seq[j]>0)
  198. X      printf("%1d     ",seq[j]);
  199. X    else if (seq[j]==0)
  200. X      printf("      ");
  201. X    else {
  202. X      printf("R\n"); break;
  203. X    }
  204. X      }
  205. X      display(x,t,5,5,M);
  206. X    }
  207. X    printf("Another round? (1-yes): "); scanf("%d",&ok);
  208. X  } while (ok);
  209. X}
  210. //E*O*F art1.c//
  211.  
  212. echo x - art1.data
  213. sed -e 's/^X//' > "art1.data" << '//E*O*F art1.data//'
  214. X/* art1 data file - 20x25 */
  215. X
  216. X{1,1,1,1,1,
  217. X 1,0,0,0,1,
  218. X 1,1,1,1,1,
  219. X 1,0,0,0,1,
  220. X 1,0,0,0,1,},
  221. X
  222. X{1,1,1,1,0,
  223. X 1,0,0,0,1,
  224. X 1,1,1,1,0,
  225. X 1,0,0,0,1,
  226. X 1,1,1,1,0,},
  227. X
  228. X{1,1,1,1,1,
  229. X 1,0,0,0,0,
  230. X 1,0,0,0,0,
  231. X 1,0,0,0,0,
  232. X 1,1,1,1,1,},
  233. X
  234. X{1,1,1,1,0,
  235. X 1,0,0,0,1,
  236. X 1,0,0,0,1,
  237. X 1,0,0,0,1,
  238. X 1,1,1,1,0,},
  239. X
  240. X{1,1,1,1,1,
  241. X 1,0,0,0,0,
  242. X 1,1,1,1,1,
  243. X 1,0,0,0,0,
  244. X 1,1,1,1,1,},
  245. X
  246. X{1,1,1,1,1,
  247. X 1,0,0,0,0,
  248. X 1,1,1,1,1,
  249. X 1,0,0,0,0,
  250. X 1,0,0,0,0,},
  251. X
  252. X{1,1,1,1,1,
  253. X 1,0,0,0,0,
  254. X 1,0,1,1,1,
  255. X 1,0,0,0,1,
  256. X 1,1,1,1,1,},
  257. X
  258. X{1,0,0,0,1,
  259. X 1,0,0,0,1,
  260. X 1,1,1,1,1,
  261. X 1,0,0,0,1,
  262. X 1,0,0,0,1,},
  263. X
  264. X{1,1,1,1,1,
  265. X 0,0,1,0,0,
  266. X 0,0,1,0,0,
  267. X 0,0,1,0,0,
  268. X 1,1,1,1,1,},
  269. X
  270. X{1,1,1,1,1,
  271. X 0,0,1,0,0,
  272. X 0,0,1,0,0,
  273. X 0,0,1,0,0,
  274. X 1,1,1,0,0,},
  275. X
  276. X{1,0,0,0,1,
  277. X 1,0,0,1,0,
  278. X 1,1,1,0,0,
  279. X 1,0,0,1,0,
  280. X 1,0,0,0,1,},
  281. X
  282. X{1,0,0,0,0,
  283. X 1,0,0,0,0,
  284. X 1,0,0,0,0,
  285. X 1,0,0,0,0,
  286. X 1,1,1,1,1,},
  287. X
  288. X{1,0,0,0,1,
  289. X 1,1,0,1,1,
  290. X 1,0,1,0,1,
  291. X 1,0,0,0,1,
  292. X 1,0,0,0,1,},
  293. X
  294. X{1,0,0,0,1,
  295. X 1,1,0,0,1,
  296. X 1,0,1,0,1,
  297. X 1,0,0,1,1,
  298. X 1,0,0,0,1,},
  299. X
  300. X{1,1,1,1,1,
  301. X 1,0,0,0,1,
  302. X 1,0,0,0,1,
  303. X 1,0,0,0,1,
  304. X 1,1,1,1,1,},
  305. X
  306. X{1,1,1,1,1,
  307. X 1,0,0,0,1,
  308. X 1,1,1,1,1,
  309. X 1,0,0,0,0,
  310. X 1,0,0,0,0,},
  311. X
  312. X{1,1,1,1,1,
  313. X 1,0,0,0,1,
  314. X 1,0,1,0,1,
  315. X 1,0,0,1,1,
  316. X 1,1,1,1,1,},
  317. X
  318. X{1,1,1,1,1,
  319. X 1,0,0,0,1,
  320. X 1,1,1,1,1,
  321. X 1,0,0,1,0,
  322. X 1,0,0,0,1,},
  323. X
  324. X{1,1,1,1,1,
  325. X 1,0,0,0,0,
  326. X 1,1,1,1,1,
  327. X 0,0,0,0,1,
  328. X 1,1,1,1,1,},
  329. X
  330. X{1,1,1,1,1,
  331. X 0,0,1,0,0,
  332. X 0,0,1,0,0,
  333. X 0,0,1,0,0,
  334. X 0,0,1,0,0,},
  335. //E*O*F art1.data//
  336.  
  337. echo x - art1.tex
  338. sed -e 's/^X//' > "art1.tex" << '//E*O*F art1.tex//'
  339. X\font\ninerm=cmr9 \font\rm=cmr8 \font\serif=amss10 
  340. X\magnification=1200
  341. X\parskip 10pt plus 1pt
  342. X\parindent 0pt
  343. X\nopagenumbers
  344. X\null\vskip-46pt
  345. X\hbox to 6.5truein {\bf March 1989 \hfil Project Documentation and source code
  346. X - ECE 551}
  347. X\vfill
  348. X\centerline{\bf PROJECT: Simulation of ART1 Neural Network}
  349. X\vskip .25in
  350. X\centerline{by}
  351. X\centerline{Jagath K. Samarabandu <bandu@cs.buffalo.edu>}
  352. X\vfill
  353. X\line {\bf Dept. of Electrical and Computer Engineering \hfil SUNY at Buffalo}
  354. X\eject
  355. X{\ninerm\bf \underbar{System Description}}\vskip 0.25in
  356. XThe program simulates a neural network using the Adaptive Resonance Theory
  357. Xproposed by Carpenter and Grossberg. [Carpenter 86]. The network forms clusters
  358. Xand is trained without supervision.
  359. X
  360. XFollowing algorithm is used in the program [Lippman 87].
  361. X
  362. X{\bf Step 1. Initialization}\hfil\break
  363. X
  364. X$$\eqalign{t_{ij}(0) &= 1 \cr
  365. Xb_{ij}(0) &= {1 \over 1 + N }}$$
  366. X$$0 \le i \le N-1$$
  367. X$$0 \le j \le M-1$$\rm
  368. Xset $\rho$,\qquad$ 0 \le \rho \le 1$\hfil\break
  369. X
  370. Xwhere \hfil\break $b_{ij}(t)$ - bottom up LTM traces\hfil\break
  371. X$t_{ij}(t)$ - top down LTM traces\hfil\break
  372. Xbetween input node $i$ and output node $j$. The fraction $\rho$ is the vigilance threshold\hfil\break
  373. X
  374. X{\bf Step 2. Apply New Input \hfil\break
  375. XStep 3. Compute Matching Scores }\hfil\break
  376. X$$ \mu_j = \sum_{i=0}^{N-1} b_{ij}(t)X_i, \qquad 0 \le j \le M-1 $$
  377. Xwhere $\mu_j$ is the output of output node $j$ and $X_i$ is the input element
  378. X$i$.\hfil\break
  379. X{\bf Step 4. Select best matching Exemplar}\hfil\break
  380. X$$\mu_j^* = \displaystyle \max_j\left\{{\mu_j}\right\}$$
  381. X
  382. X{\bf Step 5. Vigilance test}\hfil\break
  383. X$$\Vert X \Vert = \sum_{i=0}^{N-1} X_i$$
  384. X$$\Vert T \cdot X\Vert = \sum_{i=0}^{N-1} t_{ij}\cdot X_i$$
  385. XIF ${{\Vert T\cdot X\Vert}\over\Vert X\Vert}>\rho$ THEN GOTO STEP 6\hfil\break
  386. X
  387. X{\bf Step 6. Disable best matching Exemplar}\hfil\break
  388. XThe output of the bes matching node is selected in {\bf step 4} is temporarily 
  389. Xset to zero and no longer takes part in the maximization of {\bf step 4}. Then
  390. Xgo to {\bf Step 3}\hfil\break
  391. X
  392. X{\bf Step 7. Adapt Best Matching Exemplar}\hfil\break
  393. X$$t_{ij^*}(t+1) = t_{ij^*}(t)X_i$$
  394. X$$b_{ij^*}(t+1) = {{t_{ij^*}(i)X_i}\over 0.5+\sum_{i=0}^{N-1}t_{ij^*}(t)X_i}$$
  395. X
  396. X{\bf Step 8. repeat by Going to Step 2}\hfil\break
  397. X(First enable any nodes disabled in step 6)
  398. X\vskip 0.25in{\ninerm\bf \underbar{Testing}}\hfil\break\vskip.25in
  399. XThe system was tested using 20 patterns representing letters A to T, each of 
  400. Xwhich is a 5x5 matrix. Results of the program when $\rho = 0.5$ and $\rho = 0.8$
  401. Xare shown below. Numbers above the LTM patterns shows the iteration at which 
  402. Xthat node was selected and the letter 'R' indicates the point at which reset 
  403. Xocurred.
  404. X
  405. XFrom the output, it can be seen that trained LTM traces agree with that of Grossberg's data.
  406. X\vfil
  407. X\eject\end
  408. X
  409. //E*O*F art1.tex//
  410.  
  411. echo Possible errors detected by \'wc\' [hopefully none]:
  412. temp=/tmp/shar$$
  413. trap "rm -f $temp; exit" 0 1 2 3 15
  414. cat > $temp <<\!!!
  415.      168     456    3905 art1.c
  416.      121     107    1289 art1.data
  417.       70     393    2789 art1.tex
  418.      359     956    7983 total
  419. !!!
  420. wc  art1.c art1.data art1.tex | sed 's=[^ ]*/==' | diff -b $temp -
  421. exit 0
  422.  
  423. Jagath K. Samarabandu (716)-834-7386        bandu@cs.buffalo.edu   
  424. 269, Winspear Ave.,Buffalo,NY14215        v092r8c2@ubvms.bitnet  
  425.  
  426.  
  427.