Attachment 'sheet12.m'

Download

   1 function sheet12
   2 
   3 % Learning rate
   4 rate = 0.05;
   5 
   6 % Iterations of learning
   7 iterations = 1000;
   8 
   9 % Create XOR problem (2 dimensions + 1 bias dimension)
  10 [X,Y] = create_data();
  11 
  12 % Init network with 3 input, 5 hidden and 1 output neuron
  13 N = net_init(3, 5, 1);
  14 
  15 % Stochastic learning
  16 for i = 1:iterations
  17     % Select random point
  18     j = ceil(rand() * length(Y));
  19 
  20     % Forward/backward pass
  21     N = net_forward(N, X(:,j));
  22     N = net_backward(N, Y(j));
  23     N = net_update(N, rate);
  24 
  25     % Store error at point j
  26     e(i) = mean((N.Y2 - Y(j)).^2);
  27 end
  28 
  29 clf; plot(e);
  30 ylabel('Iterations');
  31 xlabel('Mean squared error');
  32 
  33 function [X,Y] = create_data()
  34 X = [ 1 1; -1 -1; 1 -1; -1 1]';
  35 Y = [ 1  1  -1 -1];
  36 X = [X ; ones(size(Y))];
  37 
  38 %% 
  39 % Initialize neural network 
  40 % Input:
  41 %    n     Number of input neurons
  42 %    k     Number of hidden neurons
  43 %    m     Number of output neurons
  44 % Output:
  45 %    N.W1  Weight matrix (n x k)
  46 %    N.W2  Weight matrix (k x m)
  47 function N = net_init(n,k,m)
  48 ...
  49 
  50 %%
  51 % Forward pass through network
  52 % Input:
  53 %    N     Network structure
  54 %    x     Input vector (1 x n)
  55 % Output:
  56 %    N.Y0  Input vector (1 x n)  
  57 %    N.Y1  Output at hidden layer (1 x k)
  58 %    N.Y2  Output at output layer (1 x m)
  59 function N = net_forward(N, x)
  60 ...
  61 
  62 %%
  63 % Backward pass through network
  64 % Input:
  65 %    N     Network structure
  66 %    y     Target vector (1 x m)
  67 % Output:
  68 %    N.D2  Partial derivative at output layer (1 x m)
  69 %    N.D1  Partial derivative at hidden layer (1 x k)
  70 function N = net_backward(N, y)
  71 ...
  72 
  73 %%
  74 % Update the network weights
  75 % Input:
  76 %    N     Network structure
  77 %    r     Learning rate
  78 % Output:
  79 %    N.W1  Updated weights
  80 %    N.W2  Updated weights
  81 function N = net_update(N, r)
  82 ...

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2010-12-03 11:00:13, 73.5 KB) [[attachment:FDA and LSR.pdf]]
  • [get | view] (2010-11-18 14:22:56, 1407.4 KB) [[attachment:clustering_slides.pdf]]
  • [get | view] (2011-01-25 08:30:15, 499.5 KB) [[attachment:efficient_backprop.pdf]]
  • [get | view] (2010-10-28 10:55:22, 123.9 KB) [[attachment:full_sheet01.pdf]]
  • [get | view] (2010-11-04 14:04:25, 118.5 KB) [[attachment:full_sheet02.pdf]]
  • [get | view] (2010-11-11 10:52:24, 60.2 KB) [[attachment:full_sheet03.pdf]]
  • [get | view] (2010-11-18 15:23:26, 97.5 KB) [[attachment:full_sheet04.pdf]]
  • [get | view] (2010-11-25 15:14:31, 92.0 KB) [[attachment:full_sheet05.pdf]]
  • [get | view] (2010-12-02 08:50:14, 108.9 KB) [[attachment:full_sheet06.pdf]]
  • [get | view] (2010-12-09 13:30:45, 161.5 KB) [[attachment:full_sheet07.pdf]]
  • [get | view] (2010-12-16 13:33:01, 84.4 KB) [[attachment:full_sheet08.pdf]]
  • [get | view] (2011-01-05 17:12:33, 127.5 KB) [[attachment:full_sheet09.pdf]]
  • [get | view] (2011-01-12 18:08:23, 136.1 KB) [[attachment:full_sheet10.pdf]]
  • [get | view] (2011-01-19 19:10:49, 143.6 KB) [[attachment:full_sheet11.pdf]]
  • [get | view] (2011-01-26 11:33:22, 136.7 KB) [[attachment:full_sheet12.pdf]]
  • [get | view] (2011-02-02 20:35:04, 134.9 KB) [[attachment:full_sheet13.pdf]]
  • [get | view] (2010-11-25 15:08:04, 1505.5 KB) [[attachment:ica_slides.pdf]]
  • [get | view] (2011-01-08 10:49:41, 504.7 KB) [[attachment:kernel_intro.pdf]]
  • [get | view] (2011-01-25 08:30:11, 1221.6 KB) [[attachment:lect_kernels.pdf]]
  • [get | view] (2011-02-13 09:07:55, 1391.7 KB) [[attachment:rde-tutorial.pdf]]
  • [get | view] (2010-11-18 15:23:34, 1.4 KB) [[attachment:sheet04.m]]
  • [get | view] (2010-11-25 15:14:53, 0.8 KB) [[attachment:sheet05.m]]
  • [get | view] (2010-11-30 21:18:03, 1.1 KB) [[attachment:sheet06.m]]
  • [get | view] (2010-12-09 13:31:05, 1.9 KB) [[attachment:sheet07.m]]
  • [get | view] (2010-12-16 13:33:09, 2.7 KB) [[attachment:sheet08.m]]
  • [get | view] (2011-01-12 17:38:01, 2.0 KB) [[attachment:sheet10.m]]
  • [get | view] (2011-01-12 17:38:14, 4.8 KB) [[attachment:sheet10_data.m]]
  • [get | view] (2011-01-19 19:11:02, 1.5 KB) [[attachment:sheet11.m]]
  • [get | view] (2011-01-26 11:33:28, 1.7 KB) [[attachment:sheet12.m]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.