home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume7 / art1 < prev    next >
Encoding:
Text File  |  1989-06-03  |  10.1 KB  |  419 lines

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