Guest User

Untitled

a guest
Feb 21st, 2011
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.50 KB | None | 0 0
  1. %!PS
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. %%%
  4. %%% This is a postscript implementation of the classic
  5. %%% Back-Propagation learning algorithm. As postscript
  6. %%% comments must start at the beginning of a line,
  7. %%% inline comments will be limited for readability,
  8. %%% but I will attempt to highlight meaningful blocks
  9. %%% wherever possible.
  10. %%%
  11. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  12.  
  13. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  14. %%% GLOBAL NETWORK CONSTANTS AND PARAMETERS:
  15. %%%
  16. %%% L is the number of layers in the network
  17. %%% N is the number of neurons per layer
  18. %%% C is the learning constant
  19. %%% ITER is the number of training iterations
  20. %%% e is the mathematical constant 'e'
  21. %%%
  22. %%% iarr contains input values for each neuron (2d)
  23. %%% oarr contains output values for each neuron (2d)
  24. %%% darr contains delta values for each neuron (2d)
  25. %%% warr contains weights for earch neuron (3d)
  26. %%% tiarr contains test case input vectors (2d)
  27. %%% toarr contains test case output vectors (1d)
  28. %%%
  29. %%% title the title of the results printout
  30. %%%
  31. %%% additionally, we define convenience functions for
  32. %%% generating random numbers, accessing and modifying
  33. %%% 2d and 3d arrays and our sigmoid function.
  34. %%%
  35. %%% finally, we initialize all our arrays, filling
  36. %%% iarr, oarr, and darr with zeroes and filling
  37. %%% our weight array with random numbers.
  38. %%%
  39. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  40.  
  41. /L 3 def
  42. /N 2 def
  43. /C .5 def
  44. /ITER 200 def
  45. /e 2.7183 def
  46.  
  47. /iarr L array def
  48. /oarr L array def
  49. /darr L array def
  50. /warr L array def
  51.  
  52. /tiarr [[0 0][1 0][0 1][1 1]] def
  53. /toarr [0 1 1 0] def
  54.  
  55. /title (XOR Function) def
  56.  
  57. % random number generation
  58. 80651 srand
  59. /randreal {rand 2 31 exp div} def
  60.  
  61. % utility nested array accessor/mutators
  62. /get2 {exch get exch get} def
  63. /put2 {exch get 3 1 roll exch put} def
  64. /get3 {exch get exch get exch get} def
  65. /put3 {exch get exch get 3 1 roll exch put} def
  66.  
  67. % sigmoid function
  68. /g {
  69. /q exch def e q exp e -1 q mul exp
  70. 2 copy sub 3 1 roll add div
  71. } def
  72.  
  73. % sigmoid derivative
  74. /gprime {
  75. /q exch def 4 e 2 q mul exp
  76. e -2 q mul exp 1 add add div
  77. } def
  78.  
  79. % threshold function
  80. /threshold {
  81. .5 ge {1} {0} ifelse
  82. } def
  83.  
  84. % create subarrays for i, o, d and w
  85. 0 1 L 1 sub {iarr exch N 1 add array put} for
  86. 0 1 L 1 sub {oarr exch N 1 add array put} for
  87. 0 1 L 1 sub {darr exch N 1 add array put} for
  88. 0 1 L 1 sub {warr exch N 1 add array put} for
  89.  
  90. % initialize arrays for i, o, and d (fill with 0)
  91. 0 1 L 1 sub {
  92. /q exch def
  93. 0 1 N {
  94. /r exch def
  95. 0 r q iarr put2
  96. 0 r q oarr put2
  97. 0 r q darr put2
  98. } for
  99. } for
  100.  
  101. % create final subarray for w
  102. 0 1 L 1 sub {
  103. /q exch def
  104. 0 1 N {
  105. /r exch def
  106. N 1 add array r q warr put2
  107. } for
  108. } for
  109.  
  110. % initialize final subarray for w (fill with random weights)
  111. 0 1 L 1 sub {
  112. /q exch def
  113. 0 1 N {
  114. /r exch def
  115. 0 1 N {
  116. /s exch def
  117. randreal s r q warr put3
  118. } for
  119. } for
  120. } for
  121.  
  122. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  123. %%% GRAPHICS-RELATED INITIALIZATION AND UTILITIES
  124. %%%
  125. %%% These functions make it easy to convert various
  126. %%% primitive types to strings, concatenate them
  127. %%% together, and control a "cursor" as if we were
  128. %%% printing to a console.
  129. %%%
  130. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  131.  
  132. /lm 30 def
  133. /tm 750 def
  134. /bm 40 def
  135. /textheight 16 def
  136. /cursor tm def
  137. /Courier findfont textheight scalefont setfont
  138.  
  139. % A routine for converting mixed types into strings
  140. /tostr {
  141. dup type /stringtype eq not {10 string cvs} if
  142. } def
  143. % A string concatenation function
  144. /concat {
  145. tostr exch tostr exch 2 copy length exch
  146. length add string dup dup 4 3 roll 4 index
  147. length exch putinterval 3 1 roll exch
  148. 0 exch putinterval
  149. } def
  150. % Print a line to the "console"
  151. /println {
  152. lm cursor moveto show stroke
  153. /cursor cursor textheight sub def
  154. } def
  155. % Print a list of items on the stack to the "console"
  156. /printlst {
  157. 1 sub {concat} repeat println
  158. } def
  159.  
  160. /width 300 def
  161. /height 300 def
  162.  
  163. /xmin 0 def
  164. /xmax 1 def
  165. /ymin 0 def
  166. /ymax 1 def
  167. /step .1 def
  168.  
  169. /xstep width xmax xmin sub abs div step mul def
  170. /ystep height ymax ymin sub abs div step mul def
  171.  
  172. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  173. %%% FEED-FORWARD
  174. %%%
  175. %%% loadexample loads a set of example inputs, and
  176. %%% sets any remaining input neurons with a bias value
  177. %%%
  178. %%% forward feeds forward through the network,
  179. %%% calculating input values based on weighted inputs
  180. %%% and then producing outputs by running these
  181. %%% values through a sigmoid function.
  182. %%%
  183. %%% test is a convenience wrapper taking two inputs
  184. %%% and producing the output value of our network.
  185. %%%
  186. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  187.  
  188. /loadexample {
  189. /q exch def
  190. 0 1 tiarr 0 get length 1 sub {
  191. /x exch def
  192. x q tiarr get2 x 0 oarr put2
  193. } for
  194. tiarr 0 get length 1 N {
  195. /x exch def
  196. -1 x 0 oarr put2
  197. } for
  198. } def
  199.  
  200. /forward {
  201. 1 1 L 1 sub {
  202. /x exch def
  203. 0 1 N 1 sub {
  204. /y exch def
  205. 0 y x iarr put2
  206. 0 1 N {
  207. /z exch def
  208. z y x warr get3
  209. z x 1 sub oarr get2 mul
  210. y x iarr get2 add
  211. y x iarr put2
  212. } for
  213. y x iarr get2 g y x oarr put2
  214. } for
  215. -1 N x oarr put2
  216. } for
  217. } def
  218.  
  219. /test {
  220. 0 1 N {
  221. /x exch def
  222. -1 x 0 oarr put2
  223. } for
  224. 1 0 oarr put2
  225. 0 0 oarr put2
  226. forward
  227. 0 L 1 sub iarr get2
  228. } def
  229.  
  230. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  231. %%% BACK-PROPAGATION
  232. %%%
  233. %%% First, backprop calculates the output error based
  234. %%% on a provided example index. Unused outputs are
  235. %%% initialized as having 0 error for uniformity
  236. %%% (Outputs with 0 error will not modify any weights)
  237. %%%
  238. %%% Then we step backwards layer by layer, adjusting
  239. %%% weights to account for each neuron's
  240. %%% "responsibility" for a given error.
  241. %%%
  242. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  243.  
  244. /backprop {
  245. /q exch def
  246. toarr q get
  247. 0 L 1 sub oarr get2 sub
  248. 0 L 1 sub iarr get2 gprime mul
  249. 0 L 1 sub darr put2
  250. 1 1 N 1 sub {
  251. /x exch def
  252. 0 x L 1 sub darr put2
  253. } for
  254. L 2 sub -1 0 {
  255. /x exch def
  256. 0 1 N {
  257. /y exch def
  258. /sum 0 def
  259. 0 1 N {
  260. /z exch def
  261. y z x 1 add warr get3
  262. z x 1 add darr get2 mul
  263. sum add /sum exch def
  264. } for
  265. y x iarr get2 gprime sum mul
  266. y x darr put2
  267. 0 1 N {
  268. /z exch def
  269. y x oarr get2
  270. z x 1 add darr get2 mul
  271. C mul y z x 1 add warr get3 add
  272. y z x 1 add warr put3
  273. } for
  274. } for
  275. } for
  276. } def
  277.  
  278. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  279. %%% MAIN
  280. %%%
  281. %%% Train the neural network by loading examples,
  282. %%% feeding forward, back propagating and repeating.
  283. %%%
  284. %%% Then, test each example and print out the results.
  285. %%%
  286. %%% Finally, produce a graphical representation of
  287. %%% the produced network's function.
  288. %%%
  289. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  290.  
  291. lm tm moveto
  292.  
  293. % train network
  294. ITER {
  295. 0 1 tiarr length 1 sub {
  296. /example exch def
  297. example loadexample forward
  298. example backprop
  299. } for
  300. } repeat
  301.  
  302. % print results
  303. title println
  304. (-----------------------------) println
  305. 0 1 tiarr length 1 sub {
  306. /example exch def
  307.  
  308. example (: { )
  309. 0 1 tiarr 0 get length 1 sub {
  310. /x exch def
  311. x example tiarr get2 tostr
  312. ( ) concat
  313. } for
  314. (} -> { )
  315. example loadexample forward
  316. 0 L 1 sub iarr get2 threshold tostr
  317. ( }, should be { ) concat concat
  318. toarr example get tostr
  319. ( }) concat concat
  320. tiarr 0 get length 3 add printlst
  321. } for
  322.  
  323. % graph output
  324. /posx lm 30 add def
  325. /posy cursor height sub def
  326. 1 setlinewidth
  327. ymin step ymax {
  328. /curry exch def
  329. xmin step xmax {
  330. /currx exch def
  331. currx curry test /output exch def
  332. output 0 lt {/output 0 def} if
  333. output 1 gt {/output 1 def} if
  334. output threshold
  335. 0 eq {1 0 0 setrgbcolor} {0 0 1 setrgbcolor} ifelse
  336. newpath
  337. posx posy moveto
  338. xstep 0 rlineto
  339. 0 -1 ystep mul rlineto
  340. closepath
  341. fill
  342. 1 output sub 0 output setrgbcolor
  343. newpath
  344. posx posy moveto
  345. 0 -1 ystep mul rlineto
  346. xstep 0 rlineto
  347. closepath
  348. fill
  349. /posx posx xstep add def
  350. } for
  351. /posx lm 30 add def
  352. /posy posy ystep add def
  353. } for
  354. 0 0 0 setrgbcolor
  355. lm cursor 30 sub textheight sub moveto ymax tostr show
  356. lm cursor 30 sub height sub moveto ymin tostr show
  357. lm cursor 30 sub height 2 div sub moveto (Y) show
  358. lm 30 add cursor 50 sub height sub textheight sub moveto xmin tostr show
  359. lm 30 add width add cursor 50 sub height sub textheight sub moveto xmax tostr show
  360. lm 30 add width 2 div add cursor 50 sub height sub textheight sub moveto (X) show
  361.  
  362. showpage
Advertisement
Add Comment
Please, Sign In to add comment