home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / MISC / ANNCAM.ZIP / NNETMT.C
Encoding:
C/C++ Source or Header  |  1991-01-03  |  10.1 KB  |  403 lines

  1.     This source contains the following files:
  2.  
  3.     Neuron.c    -    source for content addressible memory program
  4.     NOut.ex1    -    an example output report generated by the program
  5.     NOut.ex2    -    another example output report generated by the program
  6.     Neuron.doc  -    this file.
  7.  
  8.     The program Neuron.c simulates a SIMPLE stable state neural network
  9. reporting on both input and output states and energy levels after each
  10. iteration (namely set up for 8, though usually the network stabilizes after
  11. about 4). The program demonstrates a very straight-forward method of
  12. programming a content-addressable memory and receiving output from that
  13. memory. This program is based on a BASIC program developed in Ed Rietman's book
  14. "Experiments in Artificial Neural Networks" published by TAB Books copyright
  15. 1988.
  16.     The program allows you to create an input vector (vector u - either created
  17. randomly or by entry), a connection strength matrix (matrix T - either created
  18. randomly or by entry), enter a threshold value, a information value (see pg. 27
  19. of "Exp. in ANN"), and a seed for generating the random vector and matrices (if
  20. necessary - this also allows reproducible tests). It outputs a vector (vector
  21. v) and feeds this vector back into the connection strength matrix until the
  22. network stabilizes (the network can have multiple Lorenz strange attractors).
  23.  
  24.     The executable was created on an AMIGA 2000 computer with 1M using a Manx
  25. C Compiler Ver 3.4a and can be run either from the CLI or Workbench (please
  26. use the ICON supplied or incorporate the tool window line into your ICON as
  27. this is what creates the window for the program to run in). This program should
  28. execute on an AMIGA 500/1000/2000 with no difficulty. The source should be
  29. equally compilable on most C compilers as I tried to stay as system independent
  30. as possible.
  31.  
  32.     The program creates an output report to a file called "Neuron.Output". Two
  33. example reports are included in the archive file (NOut.ex1 and NOut.ex2) for
  34. comparison purposes.
  35.  
  36.     Feel free to repost this archive to other BBS's so long as you keep the
  37. copyright and this doc file intact (at least I would appreciate it). Also,
  38. feel free to modify or use this program in any way you like provided it is not
  39. for commercial purposes (i.e. this was written in the spirit of learning and
  40. promoting Neural Networks, Parrallel Distributed Processing and NOT for anyone
  41. to make money!!!).
  42.  
  43.     For more detailed analysis of this program I suggest researching Ed
  44. Rietman's book.
  45.  
  46.     Remember, this is just a simple program demonstrating the programming of
  47. matrix mathematics and content addressible memories. Other programs will be
  48. on the way demonstrating other aspects of PDP and NNets (such as Hebb's
  49. learning rule, Grossberg's revision, interactive activation, etc.) just as
  50. soon as I research them myself (I'm still in the learning curve.)
  51.  
  52.     If you have any questions regarding this program, PDP, NNet's, or anything
  53. dealing with computers and their use (I've been an IBM Mainframe (4300 VM/VSE)
  54. systems programmer for 5 years and an IBM applications programmer before that
  55. for 5 years) send me some EMAIL (check the BBS you got this from - I might be
  56. there - if not I've included my USENET address as that's where I check for mail
  57. most often. If you can't find me check with somebody else on your BBS - I
  58. usually can be tracked down.).
  59.  
  60.     Hope this program helps some newcomers to the field of PDP and NNets,
  61.  
  62.                           Shawn P. Legrand, CCP
  63.  
  64. USENET: spl@cup.portal.com    or    ...sun!cup.portal.com!spl
  65.  
  66. ------------------------------ cut here for source -----------------------------
  67. /*
  68.  
  69.    Original Author: Shawn P. Legrand      15-Sep-88
  70.  
  71.     This program simulates a simple neural network by  calculating the 
  72.     inner product of a vector and a matrix where the matrix T is symmetric
  73.     {T(i,j) = T(j,i)}, dilute (more 0's than 1's), and T(i,i)=0 (to prevent
  74.     oscillations and chaotism). Reports are sent to a report file showing the
  75.     input and output matrices and the energy level of the matrix after each
  76.     iteration.    
  77.  
  78.     This program is based on information contained in a book by Ed Rietman,
  79.     'Experiments in Artificial Neural Networks', published by TAB.
  80.  
  81.     Copyright 1988     Shawn P. Legrand, CCP         All Rights Reserved
  82.     
  83. */
  84.  
  85. #include <stdio.h>
  86.  
  87. /*
  88.  * Random number generator -
  89.  * adapted from the FORTRAN version 
  90.  * in "Software Manual for the Elementary Functions"
  91.  * by W.J. Cody, Jr and William Waite.
  92. */
  93.  
  94. static long int iy = 100001;
  95.  
  96. sran(seed)
  97. long seed;
  98. {
  99.         iy = seed;
  100. }
  101.  
  102. double ran()
  103. {
  104.         iy *= 125;
  105.         iy -= (iy/2796203) * 2796203;
  106.         return (double) iy/ 2796203.0;
  107. }
  108.  
  109. /*
  110.  * Main program logic.
  111. */
  112.  
  113. main()
  114. {
  115.     int neurons, io, info, vector, matrix, i, j, iterate, sigma=0;
  116.     long seed;
  117.     float energy=0;
  118.     double r;
  119.     int t[100][100], u[100], v[100];
  120.     FILE *fp;
  121.  
  122. /* Open new CLI device */
  123.  
  124.     if ((fp = fopen("Neuron.output","w")) == NULL) {
  125.        printf("Error opening Neuron.output!!!\n");
  126.        exit(10);
  127.     }   
  128.  
  129. /* Retrive initial values */
  130.  
  131.     printf("Input the random seed: ");
  132.     scanf("%ld",&seed);
  133.     sran(seed);
  134.      
  135.     printf("Enter the number of neurons (100 maximum): ");
  136.     scanf("%d",&neurons);                                   
  137.     printf("Input the threshold value (0 to 2 are reasonable values): ");
  138.     scanf("%d",&io);
  139.     printf("Enter the value of the information (0 to 1 is a good value): ");
  140.     scanf("%d",&info);
  141.     printf("Do you want to enter the input vector yourself (1/Yes, 0/No)? ");
  142.     scanf("%d",&vector);
  143.     printf("Do you want to enter the T matrix (1/Yes, 0/No)? ");
  144.     scanf("%d",&matrix);
  145.  
  146. /* Initialize the weight matrix (matrix T) */    
  147.  
  148.     if (!matrix) {
  149.         for (i=0; i<neurons; i++) {
  150.         for (j=0; j<neurons; j++) {
  151.             if (i==j) {
  152.             t[i][j] = 0;
  153.         } else {
  154.                     r=ran();
  155.             if (r<0.8) {
  156.                 t[i][j] = 0;
  157.             } else {
  158.                 t[i][j] = 1;
  159.             }
  160.         }    
  161.         }
  162.     }
  163.     } else {
  164.         for (i=0; i<neurons; i++) {
  165.         for (j=0; j<neurons; j++) {
  166.             printf("T(%d,%d): ",i,j);
  167.         scanf("%d",&t[i][j]);
  168.         }
  169.         }
  170.     }
  171.  
  172. /* Output initial values */
  173.     
  174.     fprintf(fp,"Seed = %ld\n",seed); 
  175.     fprintf(fp,"Threshold = %d\n",io);
  176.     fprintf(fp,"Information = %d\n",info);
  177.     fprintf(fp,"T matrix:\n");
  178.     for (i=0; i<neurons; i++) {
  179.         for (j=0; j<neurons; j++) {
  180.         fprintf(fp,"%d",t[i][j]);
  181.     }
  182.     fprintf(fp,"\n");
  183.     }
  184.     fprintf(fp,"\n\n");    
  185.     
  186. /* Retrieve input vector (vector u) */
  187.  
  188.     if (!vector) {
  189.         for (i=0; i<neurons; i++) {
  190.         r=ran();
  191.         if (r<0.5) {
  192.             u[i]=0;
  193.         } else {
  194.             u[i]=1;
  195.         }
  196.     }
  197.     } else {
  198.         for (i=0; i<neurons; i++) {
  199.         fprintf(fp,"Input u(%d): ",i);
  200.         fscanf(fp,"%d",&u[i]);
  201.     }
  202.     }
  203.  
  204. /* Perform calculations, Report input and output vector, Energy level, 
  205.    Iterate using feedback from prior calculation */    
  206.  
  207.     for (iterate=0; iterate<8; iterate++) {
  208.         for (i=0; i<neurons; i++) {
  209.         for (j=0; j<neurons; j++) {
  210.             sigma+=t[i][j]*u[j];
  211.         }
  212.         sigma+=info;
  213.         if (sigma>io) {
  214.             sigma=1;
  215.         } else {
  216.             sigma=0;
  217.         }
  218.         v[i]=sigma;
  219.         sigma=0;
  220.     }
  221.     fprintf(fp,"Iteration %d:\n",iterate);
  222.     fprintf(fp,"Input vector U:\n");
  223.     for (i=0; i<neurons; i++) {
  224.         fprintf(fp,"%d",u[i]);
  225.     }
  226.     fprintf(fp,"\nOutput vector V:\n");
  227.     for (i=0; i<neurons; i++) {
  228.         fprintf(fp,"%d",v[i]);
  229.     }
  230.     fprintf(fp,"\n");
  231.     fprintf(fp,"            Energy: ");
  232.     for (i=0; i<neurons; i++) {
  233.         energy+=(float) (u[i]*v[i]);
  234.     }
  235.     energy*=-0.5;
  236.     fprintf(fp,"%f\n\n",energy);
  237.     for (i=0; i<neurons; i++) {
  238.         u[i]=v[i];
  239.     }
  240.     energy=0;
  241.     }
  242.     
  243. }
  244. ------------------------------- cut here for example one -----------------------
  245. Seed = 16000
  246. Threshold = 1
  247. Information = 1
  248. T matrix:
  249. 0001010000100000
  250. 0000100010000000
  251. 0000000000000000
  252. 0110000000010000
  253. 0110010010000000
  254. 1000000011001000
  255. 0000000000000000
  256. 0010100001000001
  257. 0000000000000000
  258. 0000101000000001
  259. 1001000000000000
  260. 0010000000001010
  261. 0010000100000100
  262. 0010000101000010
  263. 0000000000000100
  264. 0000001000110000
  265.  
  266.  
  267. Iteration 0:
  268. Input vector U:
  269. 1101010011110001
  270. Output vector V:
  271. 1101110101100101
  272.             Energy: -3.500000
  273.  
  274. Iteration 1:
  275. Input vector U:
  276. 1101110101100101
  277. Output vector V:
  278. 1101110101101111
  279.             Energy: -5.000000
  280.  
  281. Iteration 2:
  282. Input vector U:
  283. 1101110101101111
  284. Output vector V:
  285. 1101110101111111
  286.             Energy: -6.000000
  287.  
  288. Iteration 3:
  289. Input vector U:
  290. 1101110101111111
  291. Output vector V:
  292. 1101110101111111
  293.             Energy: -6.500000
  294.  
  295. Iteration 4:
  296. Input vector U:
  297. 1101110101111111
  298. Output vector V:
  299. 1101110101111111
  300.             Energy: -6.500000
  301.  
  302. Iteration 5:
  303. Input vector U:
  304. 1101110101111111
  305. Output vector V:
  306. 1101110101111111
  307.             Energy: -6.500000
  308.  
  309. Iteration 6:
  310. Input vector U:
  311. 1101110101111111
  312. Output vector V:
  313. 1101110101111111
  314.             Energy: -6.500000
  315.  
  316. Iteration 7:
  317. Input vector U:
  318. 1101110101111111
  319. Output vector V:
  320. 1101110101111111
  321.             Energy: -6.500000
  322.  
  323. ---------------------------------- cut here for example 2 ----------------------
  324. Seed = 10
  325. Threshold = 1
  326. Information = 1
  327. T matrix:
  328. 0001001000110001
  329. 0010000010000000
  330. 0000100000000100
  331. 0000010010000010
  332. 0101000000000000
  333. 0000001000001001
  334. 0010000110000001
  335. 0000001000010000
  336. 1000101000000010
  337. 0001000100010000
  338. 1110001001000000
  339. 1001100000000000
  340. 0010011001000000
  341. 0000000000001010
  342. 1011010010100000
  343. 0000000010000000
  344.  
  345.  
  346. Iteration 0:
  347. Input vector U:
  348. 0000010101101000
  349. Output vector V:
  350. 1001011001101110
  351.             Energy: -2.000000
  352.  
  353. Iteration 1:
  354. Input vector U:
  355. 1001011001101110
  356. Output vector V:
  357. 1011110111111110
  358.             Energy: -4.000000
  359.  
  360. Iteration 2:
  361. Input vector U:
  362. 1011110111111110
  363. Output vector V:
  364. 1111111111111111
  365.             Energy: -6.500000
  366.  
  367. Iteration 3:
  368. Input vector U:
  369. 1111111111111111
  370. Output vector V:
  371. 1111111111111111
  372.             Energy: -8.000000
  373.  
  374. Iteration 4:
  375. Input vector U:
  376. 1111111111111111
  377. Output vector V:
  378. 1111111111111111
  379.             Energy: -8.000000
  380.  
  381. Iteration 5:
  382. Input vector U:
  383. 1111111111111111
  384. Output vector V:
  385. 1111111111111111
  386.             Energy: -8.000000
  387.  
  388. Iteration 6:
  389. Input vector U:
  390. 1111111111111111
  391. Output vector V:
  392. 1111111111111111
  393.             Energy: -8.000000
  394.  
  395. Iteration 7:
  396. Input vector U:
  397. 1111111111111111
  398. Output vector V:
  399. 1111111111111111
  400.             Energy: -8.000000
  401.  
  402. ---------------------------------- End of File ---------------------------------
  403.