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.You are not allowed to attach a file to this page.