home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / ai / neuraln / 3425 < prev    next >
Encoding:
Text File  |  1992-09-08  |  5.2 KB  |  169 lines

  1. Newsgroups: comp.ai.neural-nets
  2. Path: sparky!uunet!rosevax!flower!scotbri
  3. From: scotbri@flower.rosemount.com (Scott Brigham)
  4. Subject: Matlab program for Backpropagation
  5. Message-ID: <1992Sep8.150748.22368@rosevax.rosemount.com>
  6. Originator: scotbri@flower
  7. Sender: news@rosevax.rosemount.com (USENET News administrator)
  8. Nntp-Posting-Host: flower
  9. Organization: Rosemount, Inc.
  10. Date: Tue, 8 Sep 1992 15:07:48 GMT
  11. Lines: 156
  12.  
  13. Some folks were interested in a Matlab program for backprop.  Here it is.
  14.  
  15. ------------------------------------------------------------------------------
  16.  
  17. NOTES ON USING bp2.m
  18.  
  19. This is a matlab program to train a feedforward network with one hidden layer.
  20. It uses the the standard backprop algorithm with momentum.
  21. The input vector file and output vector file should be in ascii, with
  22. each vector being on a separate line (see the matlab user's manual for
  23. a more complete description of matlab ascii data files).  Obviously, the
  24. number of vectors in the input file should be the same as the number in the
  25. output file.
  26.  
  27. The output range of a neuron is -1 to +1 and it uses the hyperbolic tangent
  28. squashing function.
  29.  
  30. When the program asks for a file name you enter it without the suffix.
  31. For example, if your input vector file is called invec1.dat you would do this:
  32.  
  33. Enter filename of input vectors:  invec1
  34.  
  35. The output error tolerance should be entered in absolute error value; for
  36. example if you want all training vectors to be within .018 of the target
  37. values (this would represent 1.0 % of the range if you use +/- .9 for your
  38. useable range) you would enter .018 when asked.  This is what determines
  39. when the program stops.  If you want it to do something different then
  40. by all means get in there and hack!
  41.  
  42. This is a pretty bare-bones program.  It's a good starting point for you
  43. to customize it for your own needs, add cool graphing stuff, etc.
  44. You'll have to write your own version for actually running the
  45. network after it's trained; it's pretty easy to modify a copy of this 
  46. program to do that -- just delete the learning section and clean up
  47. the loose ends.
  48.  
  49. If you want linear neurons in the output layer delete the tanh() function
  50. from the out2 expression and delete the (1 - out2) .* (1 + out2) from the
  51. delta2 expression.
  52.  
  53. Scott Brigham
  54. Rosemount, Inc.
  55. 12001 Technology Drive
  56. Eden Prairie, MN  55344
  57. scotbri@rosemount.com
  58.  
  59.  
  60. ---------------------------- cut here --------------------------------------
  61. %
  62. %  bp2 -- backpropagation training algorithm for a
  63. %  two-layer neural network.
  64. %
  65. %  written by Scott Brigham
  66. %  copyright 1991, 1992 by Rosemount, Inc. 
  67. %
  68. %  27 Feb 91  --  created
  69. %   5 Mar 91  --  added trainable bias
  70. %   8 Mar 91  --  added error tracking
  71. %  14 Mar 91  --  added momentum
  72. %   1 Apr 91  --  changed output range to +/- 1 
  73. %  17 Apr 91  --  changed from sigmoid to tanh outputs
  74. %  19 Apr 91  --  added load matrices option
  75. %  29 Aug 91  --  added K factor to change limits of tanh function
  76. %  21 Feb 92  --  replace 'for' loop with vectorized version
  77. %
  78. fprintf('\n\nTwo-layer Backprop Network Creator and Trainer\n')
  79. fprintf('Version 2.0  written by Scott Brigham\n\n')
  80. fprintf('Copyright 1991,1992 by Rosemount, Inc.\n')
  81. rand('uniform');
  82. rrange = .5;    % range for random weight initialization
  83. epoch_disp = 100;
  84.  
  85. mc = menu('Initialization Method','Manual','Execute M-file');
  86.  
  87. if mc == 2
  88.  
  89.     initfile = input('Enter M-file name: ','s');
  90.     eval(initfile);
  91.  
  92. elseif mc == 1
  93.  
  94. vecfile = input('Enter filename of input vectors: ','s');
  95. targfile = input('Enter filename of target vectors: ','s');
  96. eval(['load ',vecfile,'.dat'])
  97. eval(['load ',targfile,'.dat'])
  98. eval(['in=',vecfile,';']);
  99. eval(['target=',targfile,';']);
  100. eval(['clear ',vecfile,' ',targfile])
  101. [nov, b] = size(in);
  102. [junk,d] = size(target);
  103.  
  104. fprintf('\nThis network has %.0f inputs and %.0f output neurons.\n\n' ...
  105.        ,b,d);
  106.  
  107. mc = menu('Choice for Starting Weights','Random','Load from file');
  108. if mc == 1
  109.  c = input('How many neurons in the hidden layer? ');
  110.  W1 = rrange * 2 * (rand(b+1,c) - .5);
  111.  W2 = rrange * 2 * (rand(c+1,d) - .5);
  112. elseif mc == 2
  113.  wfile = input('Enter name of weight file: ','s');
  114.  eval(['load ',wfile])
  115.  [junk,c] = size(W1);
  116.  fprintf('This network has %0.f neurons in the hidden layer.\n',c);
  117. end
  118. tolerance = input('Output error tolerance: ');
  119. eta = input('Learning rate:  ');
  120. alpha = input('Momentum (0 - .95): ');
  121. weightfile = input('Filename to save weight matrices in: ','s');
  122.  
  123. end
  124.  
  125. dW1old = zeros(W1);
  126. dW2old = zeros(W2);
  127. bias = ones(nov,1);
  128. error = zeros(target);
  129. done = 0;
  130. epoch = 0;
  131. n = 1;
  132. mse=zeros(20,1);
  133. while done == 0
  134. out1 = tanh([in bias] * W1);
  135. out2 = tanh([out1 bias] * W2);
  136. %
  137. %  output layer
  138. %
  139. delta2 = (target - out2) .* (1 - out2) .* (1 + out2);
  140. error = target - out2;
  141. dW2 = eta * [out1 bias]' * delta2 + alpha * dW2old;
  142. dW2old = dW2;
  143. W2 = W2 + dW2;
  144. %
  145. %  input layer
  146. %
  147. delta1 = delta2 * W2(1:c,:)' .* (1 - out1) .* (1 + out1);
  148. dW1 = eta * [in bias]' * delta1 + alpha * dW1old;
  149. dW1old =dW1;
  150. W1 = W1 + dW1;
  151.  
  152. if max(max(abs(error))) < tolerance
  153.   done = 1;
  154. end
  155.  
  156. epoch = epoch + 1;
  157. if rem(epoch,epoch_disp) == 0
  158.  mse(n) = mean(sum(error .* error));
  159.  fprintf('epoch %.0f:  mse = %.5f  rmse = %.3f\n',epoch,mse(n),sqrt(mse(n)))
  160.  n = n + 1;
  161. end
  162. end
  163. eval(['save ',weightfile,' W1 W2'])
  164. fprintf('The weights converged!  How lucky can you get?\n')
  165. epoch
  166. %
  167. %  the end
  168. %
  169.