%Neural network program for classifying points inside/outside parabola.
clear;a=7;mu=0.01;K=1000;L=10;N=100;
%K=#iterations. L=#neurons in hidden layer. N=#training.
X=20*rand(2,N)-10;D(X(1,:).^2<10*X(2,:))=1;D(X(1,:).^2>10*X(2,:))=0;
figure,plot(X(1,:),X(2,:),'+')
%figure,plot(X(1,D==0),X(2,D==0),'+')
%hold on,plot(X(1,D==1),X(2,D==1),'o')
hold on,t=[-10:0.1:10];plot(t,t.^2/10,'.g')
%Training signals created. Now start neural network.
W(1:L,1:3)=0.5*rand(L,3);Wo(1:L+1)=0.5*rand(1,L+1);%Initialize weights.
%W(i,j) is weight from input j=1,2 and constant input to hidden layer input i=1...L.
%Wo(i) is weight from hidden layer output Y(i) i=1...L and constant input to output yo.
for k=1:K;%Iteration number
%Run NN forward:
for I=1:N;V=W*[1;X(:,I)];Y(:,I)=1./(1+exp(-a*V));
vo=Wo*[1;Y(:,I)];yo(I)=1/(1+exp(-a*vo));
%Backpropagation: Output layer: delta0.
deltaL(I)=-a*(D(I)-yo(I))*yo(I)*(1-yo(I));%scalar
%deltaI(:,I)=a*[1;Y(:,I)].*(1-[1;Y(:,I)])*deltaL(I).*Wo';%11X1
deltaI(:,I)=a*Y(:,I).*(1-Y(:,I))*deltaL(I).*Wo(2:11)';%10X1
%Update weights using steepest descent:
W=W-mu*deltaI(:,I)*[1;X(:,I)]';Wo=Wo-mu*deltaL(I)*[1;Y(:,I)]';end;end;
%Check neural network on given training:
figure,plot(X(1,yo<0.5),X(2,yo<0.5),'+')
hold on,plot(X(1,yo>0.5),X(2,yo>0.5),'or')
hold on,t=[-10:0.1:10];plot(t,t.^2/10,'.g')
%Test neural network on new points:
Z=20*rand(2,N)-10;for I=1:N;
V=W*[1;Z(:,I)];Y(:,I)=1./(1+exp(-a*V));vo=Wo*[1;Y(:,I)];yo(I)=1/(1+exp(-a*vo));end;
figure,plot(Z(1,yo<0.5),Z(2,yo<0.5),'+')
hold on,plot(Z(1,yo>0.5),Z(2,yo>0.5),'or')
hold on,t=[-10:0.1:10];plot(t,t.^2/10,'.g')

%Wo=[-1.2614,0.4616,0.6011,-0.3146,0.3725,-0.2316,0.6040,0.7132,-0.4583,0.5104,-0.7111]

%W=[0.6782    0.4186    0.4581
%  -0.2614    0.1819    0.6225
%  -0.1775    0.7973   -0.7938
%   0.4655    0.3475    0.3949
%   0.3669    0.4457    0.2024
%   0.5507    0.5692    0.7200
%   1.6030   -1.3600    1.2208
%   0.2293    0.1958   -0.1817
%   0.0012    0.3730    0.5174
%   0.4509    0.0102    0.0106