echo on
%% Machine learning 2
% Neural network to solve xor problem. 
% Two of the many variants are possible, the first is from
% machinethink.net, the second uses an 'optimal' surface
echo off;clf
% A linear function is also interesting for the output i.e. opfn=@(x) x;
l2fn=@(x) 1./(1+exp(-x)); % sigmoid function for hidden layer
opfn=@(x) 1./(1+exp(-x)); % sigmoid function for output layer
%% optimal weights. 
% Can set s t0 values between 1 and 100 to get an idea of how the surface changes.
%s=10;
s=1+rand*30;
w2=[-s s -s/2;s -s -s/2];
w1=7*[1 1 -0.5];
%% Machinething.net weights
%w2=[54 14 -8;17 14 -20];
%w1=[92 -98 -48];

%% Visualise as a 2D surface
figure(1)
[Xm,Ym] = meshgrid(-.1:.03:1.1,-.1:.03:1.1);
[lenx,leny]=size(Xm);

H1=l2fn(w2*[Xm(:) Ym(:) ones(lenx*leny,1)]');
Zm=opfn(w1*[H1;ones(1,lenx*leny)]);
Zm=reshape(Zm,leny,lenx);
gh1=surf(Xm,Ym,Zm);
gh1.FaceAlpha=.8;gh1.FaceColor='interp';gh1.EdgeColor='none';
text([0 0 1 1],[0 1 0 1],[0 1 1 0],{'0','1','1','0'})
%% Visualise as a contour map
figure(2);contour(Xm,Ym,Zm)
l=text([0 0 1 1],[0 1 0 1],{'0','1','1','0'});
for jj=1:4;l(jj).FontSize=16;l(jj).Color='blue';end
xlabel('Input 1');ylabel('Input 2');
