home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / basic / bmag / neuralnt.bas < prev    next >
Encoding:
BASIC Source File  |  1994-04-13  |  6.9 KB  |  203 lines

  1. '─ Area: F-QUICKBASIC ─────────────────────────────────────────────────────────
  2. '  Msg#: 283                                          Date: 06 Apr 94  23:33:01
  3. '  From: Eric B. Ford                                 Read: Yes    Replied: No 
  4. '    To: All                                          Mark:                     
  5. '  Subj: 1/3 Neural Network Source Code
  6. '──────────────────────────────────────────────────────────────────────────────
  7. 'Here's a small old Basic program to perform a simple back propagation neural
  8. 'network.  Note:  This isn't my code!  Whoever wrote it, I can't critize,
  9. 'because I never got around to writing one in Basic, but it's obviously old.
  10.  
  11. 'If you start playing with this, here are a few suggestions...
  12.  
  13. '    1.  Use lots of trainging interations...  I typically use 5000 for my
  14. 'scientific work, just to see if a network is promising.  Also change the
  15. 'iteration counter to a long, so it can train alot longer.  I've removed the
  16. 'print statement for the "round", but I didn't want to post altered code, since
  17. 'it isn't mine.
  18.  
  19. '    2.  Feel free to modify it to use more hidden nodes.  I typically use
  20. 'between 10 and 100, but it depends on what your doing.
  21.  
  22.  
  23. '    3.  Neural network scientists are famous for leaving computers on all
  24. 'weekend to train networks.  Once you've typed in a good bit of data and have a
  25. 'reasonable sized network (approx 25-75 hidden nodes), let it run all night.  I
  26. 'frequently do that all night. I even have Cray Y-MPs work on these for several
  27. 'hours!
  28.  
  29. '    4.  Add a feature to save a network, so you could pick up where you left
  30. 'off.  This way you could come back to promising nets. Also add something to
  31. 'load and save the training patterns, so you don't have to type them over and
  32. 'over.
  33.  
  34. '    5.  At this point email me about getting either C code or a few EXEs to
  35. 'work with.  If you've gotten this far, you may want to apply it to anything
  36. 'from handwriting recognition to stock market prediction.  NNs are currently
  37. 'being used for both and many other interesting things.
  38.  
  39. 'If anyone's interested I have much more advanced C and C++ code.
  40. 'This progrma should be seen as a toy and learning tool, and not as something to
  41. 'do meaningful work with.  If you want to apply this, my C implementations are
  42. 'much more powerful and a lot faster.  I'm pretty experienced at this kind of
  43. 'stuff, so I should be able to help anyone interested in neural networks. I'm
  44. 'currently using similar techniques for:
  45.  
  46. '    1.  Long range navigation of planetary rovers in virtual reality
  47. '    2.  Direct mail - predicting which households are likely to respond to a
  48. 'direct mail package 3.  Image processing - getting more detail of images from
  49. 'astronomical observatories
  50. '    3.  Intel - Automating microchip manufacture.  Confidential
  51.  
  52.  
  53. 'As you old-timers know, I always was a Post-It advocate, but I don't know what
  54. 'the official view of Postit is now, so I'll just post the code.  Let me know if
  55. 'you have any questions!
  56.  
  57. 1 DIM wout(10, 10), outa(10), win(10, 10), hidnet(10), OUTNET(10), DELTAOUT(10)
  58. 5 DIM INA(10), TARG(10), OEXAM(10, 10), IEXAM(10, 10), HIDA(10)
  59. 10 CLS
  60. 11 thrval = .4: REM default threshold value
  61. 14 PRINT "Enter # of INPUT neurons "; : GOSUB 950: nin = c
  62. 20 PRINT "Enter # of OUTPUT neurons "; : GOSUB 950: nout = c
  63. 30 PRINT "Enter # of training CASES "; : GOSUB 950: ncase = c
  64. 35 PRINT "Enter # of HIDDEN neurons "; : GOSUB 950: nhid = c
  65. 40 REM
  66. 110 GOSUB 6000
  67. 120 PRINT
  68. 130 PRINT "0. Exit"
  69. 140 PRINT "1. Set rate of learning (Default ="; : PRINT rate; : PRINT " )"
  70. 160 PRINT "2. re-enter pattern"
  71. 170 PRINT "3. Make a Prediction"
  72. 175 PRINT "4. START AGAIN"
  73. 177 PRINT "5. Train Network"
  74. 178 PRINT "6. New Threshold value (Default = "; : PRINT thrval; : PRINT " )"
  75. 180 PRINT
  76. 190 PRINT "What is your choice ";
  77. 200 GOSUB 950
  78. 211 IF c = 0 THEN 220
  79. 212 IF c = 1 THEN 270
  80. 213 IF c = 2 THEN 350
  81. 214 IF c = 3 THEN 430
  82. 215 IF c = 4 THEN 10
  83. 216 IF c = 5 THEN 360
  84. 217 IF c = 6 THEN 900
  85. 219 GOTO 120
  86. 220 PRINT
  87. 230 PRINT "Are you sure (0 to exit)  "
  88. 240 INPUT c
  89. 250 IF c = 0 THEN END ELSE 120
  90. 270 PRINT
  91. 280 PRINT "What is the new rate?"
  92. 290 INPUT rate
  93. 330 GOTO 120
  94. 350 PRINT
  95. 355 GOSUB 5000
  96. 360 PRINT "How many rounds of teaching?  "
  97. 370 INPUT c
  98. 380 FOR l = 1 TO c
  99. 385 PRINT "ROUND", l
  100. 390 GOSUB 1000
  101. 400 NEXT l
  102. 410 GOTO 120
  103. 430 GOSUB 4000
  104. 440 GOTO 120
  105. 900 INPUT "New threshold value ="; thrval: GOTO 120
  106. 950 REM keyboard input routine
  107. 960 c$ = INKEY$: IF c$ = "" THEN 960 ELSE 970
  108. 970 PRINT c$: c = VAL(c$): RETURN
  109. 1000 REM
  110. 1010 FOR I1 = 1 TO ncase
  111. 1015 FOR J1 = 1 TO nin
  112. 1020 INA(J1) = IEXAM(I1, J1)
  113. 1030 NEXT J1
  114. 1040 FOR K1 = 1 TO nout
  115. 1050 TARG(K1) = OEXAM(I1, K1)
  116. 1060 NEXT K1
  117. 1065 GOSUB 8000
  118. 1070 NEXT I1
  119. 1990 RETURN
  120. 4000 REM
  121. 4010 FOR i = 1 TO nin: PRINT "Enter value of neuron  ", i: GOSUB 950: INA(i) =_
  122. c: NEXT i
  123. 4020 GOSUB 7000
  124. 4030 REM ouput neuron values
  125. 4050 FOR j = 1 TO nout
  126. 4060 PRINT " Output neuron  "; j; " has a value of  "; outa(j)
  127. 4070 IF outa(j) > thrval THEN PRINT "==>1<==" ELSE PRINT "==>0<=="
  128. 4090 NEXT j
  129. 4130 PRINT
  130. 4140 PRINT
  131. 4145 FOR l = 1 TO nout: FOR m = 1 TO nin
  132. 4147 PRINT "WIN"; : PRINT l; : PRINT m; : PRINT "="; : PRINT win(l, m)
  133. 4148 NEXT: NEXT
  134. 4150 PRINT "Another PREDICTION (1 = yes, 0 = Exit to main menu) ";
  135. 4160 GOSUB 950
  136. 4170 IF c <> 0 THEN GOTO 4010
  137. 4180 RETURN
  138. 5000 REM
  139. 5005 FOR K = 1 TO ncase
  140. 5010 PRINT " value of neurons: INPUT"
  141. 5012 FOR i = 1 TO nin
  142. 5015 PRINT "Neuron  "; i; "="; : GOSUB 950: INA(i) = c
  143. 5025 IEXAM(K, i) = INA(i)
  144. 5030 NEXT i
  145. 5035 PRINT " Value of neurons: OUTPUT"
  146. 5040 FOR j = 1 TO nout
  147. 5050 PRINT "Neuron  "; j; "="; : GOSUB 950: TARG(j) = c
  148. 5055 OEXAM(K, j) = TARG(j)
  149. 5060 NEXT j
  150. 5080 PRINT
  151. 5090 NEXT K
  152. 5320 RETURN
  153. 6000 REM
  154. 6010 FOR h = 1 TO nhid
  155. 6020 FOR i = 1 TO nin
  156. 6090 win(h, i) = .1 * (h - i)
  157. 6100 NEXT i
  158. 6110 NEXT h
  159. 6120 FOR j = 1 TO nout
  160. 6130 FOR h = 1 TO nhid
  161. 6140 wout(j, h) = .1 * (j - h)
  162. 6150 NEXT h
  163. 6160 NEXT j
  164. 6170 rate = .5
  165. 6180 RETURN
  166. 7000 REM  **** sigmoid output *****
  167. 7010 FOR h = 1 TO nhid
  168. 7020 hidnet(h) = 0
  169. 7030 FOR i = 1 TO nin
  170. 7040 hidnet(h) = hidnet(h) + win(h, i) * INA(i)
  171. 7050 NEXT i
  172. 7060 HIDA(h) = 1 / (1 + EXP(-hidnet(h)))
  173. 7070 NEXT h
  174. 7080 FOR j = 1 TO nout
  175. 7090 OUTNET(j) = 0
  176. 7100 FOR h = 1 TO nhid
  177. 7110 OUTNET(j) = OUTNET(j) + wout(j, h) * HIDA(h)
  178. 7120 NEXT h
  179. 7130 outa(j) = 1 / (1 + EXP(-OUTNET(j)))
  180. 7140 NEXT j
  181. 7150 RETURN
  182. 8000 REM *** sigmoid matrix update ****
  183. 8010 GOSUB 7000
  184. 8020 FOR j = 1 TO nout
  185. 8030 e = EXP(OUTNET(j))
  186. 8040 DELTAOUT(j) = (TARG(j) - outa(j)) * e / ((1 + e) ^ 2)
  187. 8050 FOR h = 1 TO nhid
  188. 8060 wout(j, h) = wout(j, h) + rate * DELTAOUT(j) * HIDA(h)
  189. 8070 NEXT h
  190. 8080 NEXT j
  191. 8090 FOR h = 1 TO nhid
  192. 8100 dum = 0
  193. 8110 FOR j = 1 TO nout
  194. 8120 dum = dum + DELTAOUT(j) * wout(j, h)
  195. 8130 NEXT j
  196. 8140 e = EXP(hidnet(h))
  197. 8150 deltahid = (e / ((1 + e) ^ 2)) * dum
  198. 8160 FOR i = 1 TO nin
  199. 8170 win(h, i) = win(h, i) + rate * deltahid * INA(i)
  200. 8180 NEXT i
  201. 8190 NEXT h
  202. 8200 RETURN
  203.