Skip to content

Instantly share code, notes, and snippets.

@Wolfy42
Created June 9, 2011 09:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Wolfy42/1016446 to your computer and use it in GitHub Desktop.
Save Wolfy42/1016446 to your computer and use it in GitHub Desktop.
CI 4_3
clear all;
function y_i = ForwardProp(W, T, X)
n = length(T);
y_i = [X, zeros(1,n-length(X))];
for j= 3:n
s = 0;
for i = 1:n
if (T(i,j) == 1)
s = s + W(i,j)*y_i(i);
end;
end;
y_i(j) = ActivationFunc(s);
end;
endfunction;
function x = ActivationFunc(s)
lambda = 1;
x = 1/(1+exp(-lambda*s));
endfunction;
function e = ErrorFunction(y_i, y)
e = (y_i - y)^2;
endfunction;
function f = F_(y)
f = y*(1-y);
endfunction;
function k = deltaOutput(y, d)
k = 2*(y-d)*F_(y);
endfunction;
function k = deltaHidden(T, W, k, y, n)
sum = 0;
for j = 1:length(T)
if (T(n,j) == 1)
sum = sum + W(n, j)*k(j);
end;
end;
k = F_(y)*sum;
endfunction;
X = [ 0 0 ;
0 1 ;
1 0 ;
1 1];
Y = [0; 1; 1; 0];
y = [0 0 0 0 0 0 0];
T =[0 0 1 1 1 1 0;
0 0 1 1 1 1 0;
0 0 0 0 0 0 1;
0 0 0 0 0 0 1;
0 0 0 0 0 0 1;
0 0 0 0 0 0 1;
0 0 0 0 0 0 0 ];
W = randn(7,7);
nn = 0.9;
Ev = [];
out = [];
for limit = 1:1000000
E = 0;
for i=1:length(Y)
n = length(T);
y_i = ForwardProp(W, T, X(i,:));
out(i) = y_i(n);
e = ErrorFunction(y_i(n), Y(i));
E = E + e;
w = [];
k(7) = deltaOutput(y_i(7), Y(i));
w(:,7) = k(7)*y_i;
for i=6:-1:1
k(i) = deltaHidden(T, W, k, y_i(i), i);
w(:,i) = k(i)*y_i;
end;
W = W - nn*w;
end;
Ev = [Ev, E];
if (mod(limit, 100) == 0)
semilogy(Ev);
drawnow();
printf("\nIteration %d\n", limit);
printf("\t%d -> %f\n", Y(1), out(1));
printf("\t%d -> %f\n", Y(2), out(2));
printf("\t%d -> %f\n", Y(3), out(3));
printf("\t%d -> %f\n", Y(4), out(4));
fflush(stdout);
end;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment