Created
July 22, 2016 16:11
-
-
Save veeenu/d94d97494b1ba0ba1ef60acb9bfbce9a to your computer and use it in GitHub Desktop.
Neural network learning XOR function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
clear; | |
clc; | |
sigmoid = @(x) 1 ./ (1 + exp(-x)); | |
tset = [ | |
0, 0, 0 ; % 0 xor 0 = 0 | |
0, 1, 1 ; % 0 xor 1 = 1 | |
1, 0, 1 ; % 1 xor 0 = 1 | |
1, 1, 0 % 1 xor 1 = 1 | |
]; | |
th1 = rand(2,3); | |
th2 = rand(1,3); | |
J = 0; | |
prevCOD = [ Inf, Inf, Inf, Inf ]; | |
err = [ Inf, Inf, Inf, Inf ]; | |
for m = 1 : 100000 | |
for i = 1:size(tset, 1) | |
y = tset(i, 3); | |
% Forward propagation - i-th training sample | |
A1 = [ 1 ; tset(i, 1:2)' ]; | |
Z2 = th1 * A1; | |
A2 = [ 1 ; sigmoid(Z2) ]; | |
Z3 = th2 * A2; | |
% Back propagation | |
d3 = Z3 - y; | |
d2 = (th2' * d3) .* (A2 .* (1 - A2)); | |
d2 = d2(2:end); | |
th2 = th2 - (0.01 * d3 * A2'); | |
th1 = th1 - (0.01 * d2 * A1'); | |
J = -y * log(Z3) -(1 - y) * log(1 - Z3); | |
end | |
if mod(m, 1000) == 0 | |
clc; | |
COD = [ 10, 10, 10, 10 ]; | |
A1 = [ 1 ; 0 ; 0 ]; | |
Z2 = th1 * A1; | |
A2 = [ 1 ; sigmoid(Z2) ]; | |
COD(1) = th2 * A2; | |
A1 = [ 1 ; 0 ; 1 ]; | |
Z2 = th1 * A1; | |
A2 = [ 1 ; sigmoid(Z2) ]; | |
COD(2) = th2 * A2; | |
A1 = [ 1 ; 1 ; 0 ]; | |
Z2 = th1 * A1; | |
A2 = [ 1 ; sigmoid(Z2) ]; | |
COD(3) = th2 * A2; | |
A1 = [ 1 ; 1 ; 1 ]; | |
Z2 = th1 * A1; | |
A2 = [ 1 ; sigmoid(Z2) ]; | |
COD(4) = th2 * A2; | |
err = COD - prevCOD; | |
prevCOD = COD; | |
disp([ COD, sum(err), m / 1000 ]); | |
end | |
if abs(sum(err)) < 0.000001 | |
break; | |
end; | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment